-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
805f047
commit 81bf2e6
Showing
4 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Octokit } from '@octokit/core'; | ||
import { throttling } from '@octokit/plugin-throttling'; | ||
|
||
import { GitHub, getOctokitOptions } from '@actions/github/lib/utils'; | ||
|
||
const OctokitConstructor = GitHub.plugin(throttling); | ||
|
||
const maxRetryCount = 1; | ||
|
||
interface RequestOptions { | ||
method: string; | ||
url: string; | ||
request: RequestInfo; | ||
} | ||
|
||
interface RequestInfo { | ||
retryCount: number; | ||
} | ||
|
||
export function createOctokit( | ||
accessToken: string | ||
): InstanceType<typeof GitHub> { | ||
const octokit = new OctokitConstructor( | ||
getOctokitOptions(accessToken, { | ||
throttle: { onRateLimit, onAbuseLimit }, | ||
}) | ||
); | ||
|
||
return octokit; | ||
} | ||
|
||
function onRateLimit( | ||
retryAfter: number, | ||
options: RequestOptions, | ||
octokit: Octokit | ||
): boolean { | ||
octokit.log.warn( | ||
`Request quota exhausted for request ${options.method} ${options.url}` | ||
); | ||
if (options.request.retryCount < maxRetryCount) { | ||
octokit.log.info(`Retrying after ${retryAfter} seconds!`); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function onAbuseLimit( | ||
_: number, | ||
options: RequestOptions, | ||
octokit: Octokit | ||
): void { | ||
octokit.log.warn( | ||
`Abuse detected for request ${options.method} ${options.url}` | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters