Use custom access tokens for GitHub API

This commit is contained in:
Yuri Sizov
2022-07-14 13:51:06 +03:00
parent cc23da142a
commit c06d844c10
4 changed files with 20 additions and 2 deletions

View File

@@ -36,6 +36,7 @@ jobs:
- name: Fetch pull request data
run: npm run compose-db
env:
GRAPHQL_TOKEN: ${{ secrets.GRAPHQL_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Archive production artifacts

5
.gitignore vendored
View File

@@ -1,3 +1,8 @@
# Project folders.
node_modules/
out/
logs/
# Development environments.
.idea/
.vscode/

View File

@@ -21,7 +21,7 @@ should work just fine).
This project uses GitHub's GraphQL API. To fetch live data you need to generate
a [personal OAuth token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token).
You can supply your token to the scripts using the `GITHUB_TOKEN` environment
You can supply your token to the scripts using the `GRAPHQL_TOKEN` environment
variable. Note, that if you don't have member access to the organization, you
may not be able to access all the information used when generating the database.

View File

@@ -36,7 +36,9 @@ async function fetchGithub(query) {
init.headers = {};
init.headers["Content-Type"] = "application/json";
init.headers["Accept"] = "application/vnd.github.merge-info-preview+json";
if (process.env.GITHUB_TOKEN) {
if (process.env.GRAPHQL_TOKEN) {
init.headers["Authorization"] = `token ${process.env.GRAPHQL_TOKEN}`;
} else if (process.env.GITHUB_TOKEN) {
init.headers["Authorization"] = `token ${process.env.GITHUB_TOKEN}`;
}
@@ -47,6 +49,14 @@ async function fetchGithub(query) {
return await fetch("https://api.github.com/graphql", init);
}
async function logResponse(data, name) {
try {
await fs.writeFile(`logs/${name}.json`, JSON.stringify(data, null, 4), {encoding: "utf-8"});
} catch (err) {
console.error("Error saving log file: " + err);
}
}
function handleErrors(data) {
if (typeof data["errors"] === "undefined") {
return;
@@ -88,6 +98,7 @@ async function checkRates() {
}
const data = await res.json();
logResponse(data, "_rate_limit");
handleErrors(data);
const rate_limit = data.data["rateLimit"];
@@ -201,6 +212,7 @@ async function fetchPulls(page) {
}
const data = await res.json();
logResponse(data, `data_page_${page}`);
handleErrors(data);
const rate_limit = data.data["rateLimit"];