Skip to content

Commit

Permalink
Add GitHub API rate limit alert (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
TiagoDanin and sindresorhus committed Jun 15, 2019
1 parent e213768 commit 0aafda8
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 7 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Add it as a `test` script in package.json and activate Travis CI to lint on new

**Note:** [Travis CI only clones repositories to a maximum of 50 commits by default](https://docs.travis-ci.com/user/customizing-the-build/#git-clone-depth), which may result in a false positive of `awesome/git-repo-age`, and so you should set `depth` to `false` in `.travis.yml` if needed.

**Note:** Avoid rate limit problems on Travis CI by defining a [GitHub personal access token](https://github.com/settings/tokens/new) in an environment variable named `github_token`. See [defining variables in repository settings](https://docs.travis-ci.com/user/environment-variables/#defining-variables-in-repository-settings).

###### package.json

```json
Expand Down
26 changes: 22 additions & 4 deletions rules/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,28 @@ module.exports = rule('remark-lint:awesome/github', async (ast, file) => {
headers.Authorization = `token ${process.env.github_token}`;
}

const res = await got.get(githubUrls.api_url, {
headers,
json: true
});
let res;
try {
res = await got.get(githubUrls.api_url, {
headers,
json: true
});
} catch (error) {
if (error.statusCode === 401) {
file.message('Unauthorized access or token is invalid');
} else if (error.statusCode === 403) {
let errorMessage = `API rate limit of ${error.headers['x-ratelimit-limit']} requests per hour exceeded`;
if (!headers.Authorization) {
errorMessage += '. Use a personal token to increase the number of requests';
}

file.message(errorMessage);
} else {
file.message(`There was a problem trying to connect to GitHub: ${error.message}`);
}

return;
}

const data = res.body;
if (!data.description) {
Expand Down
109 changes: 106 additions & 3 deletions test/rules/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test.serial('github - repo without description and license', async t => {

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.returns({
.resolves({
body: {
description: null,
topics: ['awesome', 'awesome-list'],
Expand Down Expand Up @@ -75,7 +75,7 @@ test.serial('github - missing topic awesome-list', async t => {

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.returns({
.resolves({
body: {
description: 'Awesome lint',
topics: ['awesome'],
Expand Down Expand Up @@ -104,7 +104,7 @@ test.serial('github - missing topic awesome', async t => {

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.returns({
.resolves({
body: {
description: 'Awesome lint',
topics: ['awesome-list'],
Expand Down Expand Up @@ -138,3 +138,106 @@ test.serial('github - remote origin is an GitLab repo', async t => {
}
]);
});

test.serial('github - invalid token', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.rejects({
statusCode: 401
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'Unauthorized access or token is invalid'
}
]);
});

test.serial('github - API rate limit exceeded with token', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');
// eslint-disable-next-line camelcase
process.env.github_token = 'abcd';

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.rejects({
statusCode: 403,
headers: {
'x-ratelimit-limit': 5000
}
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'API rate limit of 5000 requests per hour exceeded'
}
]);

delete process.env.github_token;
});

test.serial('github - API rate limit exceeded without token', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.rejects({
statusCode: 403,
headers: {
'x-ratelimit-limit': 60
}
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'API rate limit of 60 requests per hour exceeded. Use a personal token to increase the number of requests'
}
]);
});

test.serial('github - API offline', async t => {
const execaStub = sandbox.stub(github.execa, 'stdout');
const gotStub = sandbox.stub(github.got, 'get');

execaStub
.withArgs('git', ['config', '--get', 'remote.origin.url'])
.returns('[email protected]:sindresorhus/awesome-lint-test.git');

gotStub
.withArgs('https://api.github.com/repos/sindresorhus/awesome-lint-test')
.rejects({
message: 'getaddrinfo ENOTFOUND api.github.com api.github.com:443',
code: 'ENOTFOUND'
});

const messages = await lint({config, filename: 'test/fixtures/github/0.md'});
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'There was a problem trying to connect to GitHub: getaddrinfo ENOTFOUND api.github.com api.github.com:443'
}
]);
});

0 comments on commit 0aafda8

Please sign in to comment.