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

Add GitHub API rate limit alert #82

Merged
merged 5 commits into from
Jun 15, 2019
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
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:** Set your [GitHub access token](https://github.com/settings/tokens/new) in environment variables `github_token` to avoid problem of rate limit on Travis CI.
TiagoDanin marked this conversation as resolved.
Show resolved Hide resolved

###### package.json

```json
Expand Down
16 changes: 16 additions & 0 deletions rules/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@ module.exports = rule('remark-lint:awesome/github', async (ast, file) => {
const res = await got.get(githubUrls.api_url, {
headers,
json: true
}).catch(error => {
TiagoDanin marked this conversation as resolved.
Show resolved Hide resolved
if (error.statusCode === 401) {
file.message('Unauthorized access or token is invalid');
} else if (error.statusCode === 403) {
let errorMessage = `API rate limit exceeded max of ${error.headers['x-ratelimit-limit']} requests per hour`;
if (!headers.Authorization) {
errorMessage += ', use a token to increment the number of requests';
}

file.message(errorMessage);
} else {
file.message('There was a problem trying to connect with GitHub');
TiagoDanin marked this conversation as resolved.
Show resolved Hide resolved
}
});
if (!res) {
return;
}

const data = res.body;
if (!data.description) {
Expand Down
108 changes: 105 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,105 @@ 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 with token', async t => {
TiagoDanin marked this conversation as resolved.
Show resolved Hide resolved
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 exceeded max of 5000 requests per hour'
}
]);

delete process.env.github_token;
});

test.serial('github - API rate limit 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 exceeded max of 60 requests per hour, use a token to increment the number of requests'
TiagoDanin marked this conversation as resolved.
Show resolved Hide resolved
}
]);
});

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({
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 with GitHub'
}
]);
});