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:
secustor
2024-01-15 15:39:13 +01:00
parent 2d59a19fd3
commit e27b7f3207
4 changed files with 49 additions and 5 deletions
@@ -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')
);
}
}
/**