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

Replace node-fetch with @octokit/graphql #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ trim_trailing_whitespace = true
[*.{css}]
indent_style = space
indent_size = 2

[package{,-lock}.json]
indent_style = space
indent_size = 2
77 changes: 16 additions & 61 deletions build/utils/compose-fetcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs').promises;
const fetch = require('node-fetch');
const { graphql } = require('@octokit/graphql');
const nodeUtil = require('util');
const exec = nodeUtil.promisify(require('child_process').exec);

Expand All @@ -24,6 +24,14 @@ const API_RATE_LIMIT = `

const EXEC_MAX_BUFFER = 1024 * 1024 * 32;

const graphqlWithAuth = graphql.defaults({
headers: {
authorization: process.env.GRAPHQL_TOKEN
? `token ${process.env.GRAPHQL_TOKEN}`
: `token ${process.env.GITHUB_TOKEN}`,
},
});

class DataFetcher {
constructor(data_owner, data_repo) {
this.data_owner = data_owner;
Expand Down Expand Up @@ -153,46 +161,7 @@ class DataFetcher {
}

async fetchGithub(query, retries = 0) {
const init = {};
init.method = "POST";
init.headers = {};
init.headers["Content-Type"] = "application/json";
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}`;
}

init.body = JSON.stringify({
query,
});

let res = await fetch("https://api.github.com/graphql", init);
let attempt = 0;
while (res.status !== 200 && attempt < retries) {
attempt += 1;
console.log(` Failed with status ${res.status}, retrying (${attempt}/${retries})...`);

// GitHub API is flaky, so we add an extra delay to let it calm down a bit.
await this.delay(API_DELAY_MSEC);
res = await fetch("https://api.github.com/graphql", init);
}

return res;
}

async fetchGithubRest(query) {
const init = {};
init.method = "GET";
init.headers = {};
init.headers["Content-Type"] = "application/json";
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}`;
}

return await fetch(`${this.api_rest_path}${query}`, init);
return await graphqlWithAuth(query);
}

async checkRates() {
Expand All @@ -203,18 +172,11 @@ class DataFetcher {
}
`;

const res = await this.fetchGithub(query);
if (res.status !== 200) {
this._handleResponseErrors(this.api_repository_id, res);
process.exitCode = buildCommon.ExitCodes.RequestFailure;
return;
}

const data = await res.json();
const data = await this.fetchGithub(query);
await this._logResponse(data, "_rate_limit");
this._handleDataErrors(data);

const rate_limit = data.data["rateLimit"];
const rate_limit = data["rateLimit"];
console.log(` [$${rate_limit.cost}][${rate_limit.nodeCount}] Available API calls: ${rate_limit.remaining}/${rate_limit.limit}; resets at ${rate_limit.resetAt}`);
} catch (err) {
console.error(" Error checking the API rate limits: " + err);
Expand Down Expand Up @@ -309,26 +271,19 @@ class DataFetcher {

console.log(` Requesting batch ${page}/${totalPages} of commit and pull request data.`);

const res = await this.fetchGithub(query, API_MAX_RETRIES);
if (res.status !== 200) {
this._handleResponseErrors(this.api_repository_id, res);
process.exitCode = buildCommon.ExitCodes.RequestFailure;
return [];
}

const data = await res.json();
const data = await this.fetchGithub(query);
await this._logResponse(data, `data_commits`);
this._handleDataErrors(data);

let commit_data = {};
for (let dataKey in data.data) {
for (let dataKey in data) {
if (!dataKey.startsWith("commit_")) {
continue;
}
commit_data[dataKey.substring(7)] = data.data[dataKey].object;
commit_data[dataKey.substring(7)] = data[dataKey].object;
}

const rate_limit = data.data["rateLimit"];
const rate_limit = data["rateLimit"];
console.log(` [$${rate_limit.cost}][${rate_limit.nodeCount}] Retrieved ${Object.keys(commit_data).length} commits.`);
console.log(` --`);
return commit_data;
Expand Down
137 changes: 94 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"@babel/core": "^7.6.4",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-proposal-decorators": "^7.6.0",
"@octokit/graphql": "^8.2.0",
"dompurify": "^2.0.7",
"lit-element": "^2.2.1",
"marked": "^0.7.0",
"node-fetch": "^2.7.0",
"posthtml": "^0.12.0",
"rollup": "^1.24.0",
"rollup-plugin-babel": "^4.3.3",
Expand Down