fix(Github): use x-ratelimit-remaining and 429 status code for rate limit detection
Signed-off-by: secustor <sebastian@poxhofer.at>
This commit is contained in:
@@ -78,6 +78,31 @@ describe('GithubIntegration', () => {
|
||||
),
|
||||
).toBe('https://github.com/backstage/backstage/edit/master/README.md');
|
||||
});
|
||||
|
||||
describe('isRateLimited', () => {
|
||||
const integration = new GithubIntegration({ host: 'h.com' });
|
||||
|
||||
it.each`
|
||||
status | ratelimitRemaining | expected
|
||||
${404} | ${100} | ${false}
|
||||
${429} | ${undefined} | ${true}
|
||||
${429} | ${100} | ${true}
|
||||
${403} | ${100} | ${false}
|
||||
${403} | ${0} | ${true}
|
||||
`(
|
||||
'(statusCode: $status, header: $ratelimitRemaining) === $expected',
|
||||
({ status, ratelimitRemaining, expected }) => {
|
||||
const headers = new Headers({
|
||||
'x-ratelimit-remaining': ratelimitRemaining,
|
||||
});
|
||||
const result = integration.isRateLimited({
|
||||
status,
|
||||
headers,
|
||||
} as Response);
|
||||
expect(expected).toBe(result);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('replaceGithubUrlType', () => {
|
||||
|
||||
@@ -65,6 +65,14 @@ export class GithubIntegration implements ScmIntegration {
|
||||
resolveEditUrl(url: string): string {
|
||||
return replaceGithubUrlType(url, 'edit');
|
||||
}
|
||||
|
||||
isRateLimited(response: Response): boolean {
|
||||
return (
|
||||
response.status === 429 ||
|
||||
(response.status === 403 &&
|
||||
response.headers.get('x-ratelimit-remaining') === '0')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user