11import { Octokit } from "@octokit/rest" ;
2- import { githubAPIToken } from "./constants.js" ;
2+ import { githubAPIToken , githubHourlyRateLimit , opsRepo , stage } from "./constants.js" ;
33
44// github api docs: https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#create-an-issue
55export const _gitHub = {
66 Octokit
77} ;
88
99let octokit ;
10+ const ONE_HOUR = 1000 * 60 * 60 ,
11+ ONE_DAY = ONE_HOUR * 24 ;
12+ let requestsInThisHour = 0 ,
13+ githubOpsIssueForTheDay , issueUpdatedInThisHour = false ;
14+
15+ /* c8 ignore start */
16+ // not testing this as no time and is manually tested. If you are touching this code, manual test thoroughly
17+ function _resetTPH ( ) {
18+ requestsInThisHour = 0 ;
19+ issueUpdatedInThisHour = false ;
20+ }
21+ function _resetGithubOpsIssue ( ) {
22+ githubOpsIssueForTheDay = null ;
23+ }
24+ export function setupGitHubOpsMonitoring ( ) {
25+ setInterval ( _resetTPH , ONE_HOUR ) ;
26+ setInterval ( _resetGithubOpsIssue , ONE_DAY ) ;
27+ }
28+ /* c8 ignore stop */
29+
30+ async function _newRequestMetric ( ) {
31+ requestsInThisHour ++ ;
32+ if ( requestsInThisHour > githubHourlyRateLimit / 2 ) {
33+ let opsRepoSplit = opsRepo . split ( "/" ) ; //Eg. "phcode-dev/extensionService"
34+ if ( issueUpdatedInThisHour ) {
35+ return ;
36+ }
37+ issueUpdatedInThisHour = true ;
38+ const message = `Github API requests for the hour is at ${ requestsInThisHour } , Max allowed is ${ githubHourlyRateLimit } ` ;
39+ if ( ! githubOpsIssueForTheDay ) {
40+ githubOpsIssueForTheDay = await createIssue ( opsRepoSplit [ 0 ] , opsRepoSplit [ 1 ] ,
41+ `[OPS-${ stage } ] Github Rate Limit above threshold.` , message ) ;
42+ } else {
43+ await commentOnIssue ( opsRepoSplit [ 0 ] , opsRepoSplit [ 1 ] , githubOpsIssueForTheDay . number , message ) ;
44+ }
45+ }
46+ }
1047
1148export function initGitHubClient ( ) {
1249 if ( octokit ) {
@@ -37,6 +74,7 @@ export async function createIssue(owner, repo, title, body) {
3774 // https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#create-an-issue
3875 // {... "html_url": "https://github.com/octocat/Hello-World/issues/1347", ...}
3976 console . log ( "create Github issue: " , arguments ) ;
77+ _newRequestMetric ( ) ;
4078 let response = await octokit . request ( `POST /repos/${ owner } /${ repo } /issues` , {
4179 owner,
4280 repo,
@@ -62,6 +100,7 @@ export async function createIssue(owner, repo, title, body) {
62100export async function commentOnIssue ( owner , repo , issueNumber , commentString ) {
63101 // https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28
64102 console . log ( "Comment on Github issue: " , arguments ) ;
103+ _newRequestMetric ( ) ;
65104 let response = await octokit . request ( `POST /repos/${ owner } /${ repo } /issues/${ issueNumber } /comments` , {
66105 owner,
67106 repo,
@@ -86,6 +125,7 @@ export async function getOrgDetails(org) {
86125 // https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#get-an-organization
87126 console . log ( "Get Org details: " , arguments ) ;
88127 try {
128+ _newRequestMetric ( ) ;
89129 let response = await octokit . request ( `GET /orgs/${ org } ` , {
90130 org
91131 } ) ;
@@ -117,10 +157,11 @@ export async function getOrgDetails(org) {
117157 * @param {string } repo
118158 * @return {Promise<{html_url:string, stargazers_count:number}> | null }
119159 */
120- export async function getRepoDetails ( owner , repo ) {
160+ export async function getRepoDetails ( owner , repo , log = true ) {
121161 // https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#get-a-repository
122- console . log ( "Get Repo details: " , arguments ) ;
162+ log && console . log ( "Get Repo details: " , arguments ) ;
123163 try {
164+ _newRequestMetric ( ) ;
124165 let response = await octokit . request ( `GET /repos/${ owner } /${ repo } ` , {
125166 owner, repo
126167 } ) ;
@@ -130,7 +171,7 @@ export async function getRepoDetails(owner, repo) {
130171 stargazers_count : response . data . stargazers_count
131172 } ;
132173
133- console . log ( "GitHub repo details: " , repoDetails ) ;
174+ log && console . log ( "GitHub repo details: " , repoDetails ) ;
134175 return repoDetails ;
135176 } catch ( e ) {
136177 if ( e . status === 404 ) {
@@ -155,6 +196,7 @@ export async function getReleaseDetails(owner, repo, tag) {
155196 // https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#get-a-repository
156197 console . log ( "Get Release details: " , arguments ) ;
157198 try {
199+ _newRequestMetric ( ) ;
158200 let response = await octokit . request ( `GET /repos/${ owner } /${ repo } /releases/tags/${ tag } ` , {
159201 owner, repo, tag
160202 } ) ;
0 commit comments