Skip to content

Commit d1979fc

Browse files
rapphilbryan-aguilarAneurysm9
authored
Chore: Allow to develop locally without a GitHub API key (#365)
Fixes #57. A new environment variable NO_GH_API_KEY was introduced and is used as flag to show that we don't want to provide GitHub API keys Co-authored-by: bryan-aguilar <[email protected]> Co-authored-by: Anthony Mirabella <[email protected]>
1 parent d29e5cc commit d1979fc

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

DEV_GUIDE.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,35 @@
1414
```
1515
npm install
1616
```
17-
5. [Generate a GitHub Personal Access Token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token)
18-
6. Create a new file in the root called `.env.development` (See [Project Structure](#project-structure) for more information)
19-
7. In the `.env.development` file, add the following line:
17+
5. Disable GitHub API access (Used to fetch the list of contributors)
18+
Create a new file in the root called `.env.development` (See [Project Structure](#project-structure) for more information)
19+
Write the following line to it:
20+
```
21+
NO_GH_API_KEY=true
22+
```
23+
4. Start up the Gatsby site
24+
```
25+
npm start
26+
```
27+
5. Open http://localhost:8000 to check the site
28+
29+
**Obs**: The `NO_GH_API_KEY` environment variable can also be passed through command line. E.g.: `NO_GH_API_KEY=true npm run start`.
30+
31+
### Optional - Test the GitHub API integration
32+
33+
This step is optional and allows you to test that the list of contributors is being fetched from GitHub.
34+
35+
1. [Generate a GitHub Personal Access Token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token)
36+
2. Create a new file in the root called `.env.development` (See [Project Structure](#project-structure) for more information)
37+
3. In the `.env.development` file, add the following line:
2038
```
2139
GH_API_KEY=<INSERT YOUR ACCESS TOKEN>
2240
```
23-
8. Start up the Gatsby site
41+
4. Start up the Gatsby site
2442
```
2543
npm start
2644
```
27-
9. Open http://localhost:8000 to check the site
45+
5. Open http://localhost:8000 to check the site
2846
2947
## Making Changes to the Site
3048

gatsby-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
"aws-observability/aws-otel-test-framework"
3131
],
3232
token: process.env.GH_API_KEY,
33+
useMockData: process.env.NO_GH_API_KEY,
3334
},
3435
},
3536
{

src/plugins/contributorsPlugin/gatsby-node.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,57 @@
2323
const GitHub = require("github-base")
2424
const camelcaseKeys = require("camelcase-keys")
2525

26+
function octocatContributor() {
27+
// Using example provided by the docs https://docs.github.com/en/rest/repos/repos#list-repository-contributors
28+
const githubContributor = {
29+
login: "octocat",
30+
id: 1,
31+
node_id: "MDQ6VXNlcjE=",
32+
avatar_url: "https://github.githubassets.com/images/modules/logos_page/Octocat.png",
33+
gravatar_id: "",
34+
url: "https://github.com/octocat",
35+
html_url: "https://github.com/octocat",
36+
followers_url: "https://api.github.com/users/octocat/followers",
37+
following_url: "https://api.github.com/users/octocat/following{/other_user}",
38+
gists_url: "https://api.github.com/users/octocat/gists{/gist_id}",
39+
starred_url: "https://api.github.com/users/octocat/starred{/owner}{/repo}",
40+
subscriptions_url: "https://api.github.com/users/octocat/subscriptions",
41+
organizations_url: "https://api.github.com/users/octocat/orgs",
42+
repos_url: "https://api.github.com/users/octocat/repos",
43+
events_url: "https://api.github.com/users/octocat/events{/privacy}",
44+
received_events_url: "https://api.github.com/users/octocat/received_events",
45+
type: "User",
46+
site_admin: false,
47+
contributions: 32
48+
};
49+
50+
return camelcaseKeys({...githubContributor, name: "octocat"})
51+
}
52+
2653
exports.sourceNodes = async (
2754
{ actions, createNodeId, createContentDigest },
2855
options
2956
) => {
30-
const github = new GitHub(options);
57+
58+
// Used in Development mode so that we don't try to hit the GitHub API
59+
if (options.useMockData) {
60+
actions.createNode({
61+
...octocatContributor(),
62+
id: createNodeId(1),
63+
internal: {
64+
type: "GitHubContributor",
65+
contentDigest: createContentDigest('1')
66+
}
67+
});
68+
69+
return
70+
}
71+
72+
const github = new GitHub({token: options.token});
3173

3274
const loginsSet = new Set()
3375

34-
const responses = await Promise.all(options.repos.map((repo) => github.paged(`/repos/:${repo}/contributors`, options, null)));
76+
const responses = await Promise.all(options.repos.map((repo) => github.paged(`/repos/:${repo}/contributors`)));
3577

3678
const contributors = responses.map((res) => res.pages.flat().map((page) => page.body).flat()).flat();
3779

@@ -40,7 +82,7 @@ exports.sourceNodes = async (
4082

4183
const profiles = await Promise.all(
4284
[...loginsSet].map(login =>
43-
github.get(`/users/${login}`, options).then((res) => res.body)
85+
github.get(`/users/${login}`).then((res) => res.body)
4486
)
4587
);
4688

0 commit comments

Comments
 (0)