Skip to content

Commit

Permalink
Improve alert text
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoDanin committed May 30, 2019
1 parent bbaa042 commit a18623d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ 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.
**Note:** Avoid problem of rate limit on Travis CI defining your [GitHub access token](https://github.com/settings/tokens/new) in variable `github_token`. See [defining variables in repository settings](https://docs.travis-ci.com/user/environment-variables/#defining-variables-in-repository-settings).

![image](https://user-images.githubusercontent.com/5731176/58260447-d2416b80-7d4c-11e9-8477-845f187257ac.png)

###### package.json

Expand Down
20 changes: 11 additions & 9 deletions rules/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ 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
}).catch(error => {
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 exceeded max of ${error.headers['x-ratelimit-limit']} requests per hour`;
let errorMessage = `API rate limit of ${error.headers['x-ratelimit-limit']} requests per hour exceeded`;
if (!headers.Authorization) {
errorMessage += ', use a token to increment the number of requests';
errorMessage += '. Use a personal token to increase the number of requests';
}

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

return;
}

Expand Down
11 changes: 6 additions & 5 deletions test/rules/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test.serial('github - invalid token', async t => {
]);
});

test.serial('github - API rate limit with token', async t => {
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
Expand All @@ -185,14 +185,14 @@ test.serial('github - API rate limit with token', async t => {
t.deepEqual(messages, [
{
ruleId: 'awesome/github',
message: 'API rate limit exceeded max of 5000 requests per hour'
message: 'API rate limit of 5000 requests per hour exceeded'
}
]);

delete process.env.github_token;
});

test.serial('github - API rate limit without token', async t => {
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');

Expand All @@ -213,7 +213,7 @@ test.serial('github - API rate limit without token', async t => {
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'
message: 'API rate limit of 60 requests per hour exceeded. Use a personal token to increase the number of requests'
}
]);
});
Expand All @@ -229,14 +229,15 @@ test.serial('github - API offline', async t => {
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 with GitHub'
message: 'There was a problem trying to connect to GitHub: getaddrinfo ENOTFOUND api.github.com api.github.com:443'
}
]);
});

0 comments on commit a18623d

Please sign in to comment.