Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GitHub token optional #268

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ All commands are run from the root of the project, from a terminal:

### Local Development Setup

1. Run `cp .env.example .env` to create a `.env` file for environment variables. (Don’t check this in!)
2. Create a [classic GitHub access token](https://github.com/settings/tokens) with these scopes: `repo`, `read:org`, `read:user`, and `read:project`.
3. Paste the GitHub token after `.env`’s `GITHUB_TOKEN=`.

#### DDEV setup

DDEV already has all the dependencies included.
Expand All @@ -83,11 +79,22 @@ Check out the project in your favorite Node.js environment, ideally running [`nv

To generate a static copy of the site, run `npm run build`. The contents of the `dist/` folder are exactly what get [deployed to Cloudflare Pages](#build--deployment). You can preview locally by running `npm run preview` or using a tool like [`serve`](https://www.npmjs.com/package/serve).


#### Switching from Without DDEV to with DDEV

Make sure to delete your `node_modules/` directory and run `ddev npm install`. The change in architecture can create odd issues otherwise.

#### GitHub Token

This step is not required if you just want to contribute a blog post to ddev.com.

Contributors, sponsors, releases and more data about DDEV is retrieved dynamically from the GitHub API. To test this, please follow these steps:

1. Run `cp .env.example .env` to create a `.env` file for environment variables. (Don’t check this in!)
2. Create a [classic GitHub access token](https://github.com/settings/tokens) with these scopes: `repo`, `read:org`, `read:user`, and `read:project`.
3. Paste the GitHub token after `.env`’s `GITHUB_TOKEN=`.

There is a local `cache/` to reduce API calls.

## Managing Content

The site’s content lives in either `.astro` components that resemble souped-up HTML, or Markdown files organized into schema-validated [content collections](https://docs.astro.build/en/guides/content-collections/).
Expand Down
37 changes: 37 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ dotenv.config()
const DEVELOPMENT_CACHE_DIR = 'cache'
let octokitInstance: Octokit;

// Define variable if GITHUB_TOKEN is set and not empty
const githubTokenIsSet: boolean = (() => {
if(process.env.hasOwnProperty('GITHUB_TOKEN') === false || process.env.GITHUB_TOKEN === ''){
// add warning for production builds
if(import.meta.env.MODE === 'production'){
console.warn('GITHUB_TOKEN not set or empty. You can ignore this warning for local development.');
}
return false;
}
return true;
})();

/**
* Returns an instance of Octokit, which uses the `GITHUB_TOKEN` environment
* variable for authentication.
Expand Down Expand Up @@ -56,6 +68,11 @@ export function getCategoryUrl(name: string) {
* @returns response data
*/
export async function getSponsors() {

if(!githubTokenIsSet){
return [];
}

const cacheFilename = 'sponsors.json'
const cachedData = getCache(cacheFilename);

Expand Down Expand Up @@ -121,6 +138,11 @@ export async function getSponsors() {
* @returns response data
*/
export async function getContributors(includeAnonymous = false) {

if(!githubTokenIsSet){
return [];
}

const cacheFilename = 'contributors.json'
const cachedData = getCache(cacheFilename);

Expand Down Expand Up @@ -155,6 +177,11 @@ export async function getContributors(includeAnonymous = false) {
* @returns response data
*/
export async function getRepoDetails(name: string) {

if(!githubTokenIsSet){
return [];
}

const slug = name.replace('/', '-')
const cacheFilename = `repository-${slug}.json`;
const cachedData = getCache(cacheFilename);
Expand All @@ -178,6 +205,11 @@ export async function getRepoDetails(name: string) {
* @returns tag name
*/
export async function getLatestReleaseVersion(stable = true) {

if(!githubTokenIsSet){
return [];
}

let data = await getReleases()

if (stable) {
Expand All @@ -190,6 +222,11 @@ export async function getLatestReleaseVersion(stable = true) {
}

export async function getReleases() {

if(!githubTokenIsSet){
return [];
}

const cacheFilename = 'releases.json'
const cachedData = getCache(cacheFilename);

Expand Down