generated from aicore/template-nodejs-server
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub.js
More file actions
234 lines (216 loc) · 7.72 KB
/
github.js
File metadata and controls
234 lines (216 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Octokit } from "@octokit/rest";
import {githubAPIToken, githubHourlyRateLimit, opsRepo, stage} from "./constants.js";
// github api docs: https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#create-an-issue
export const _gitHub = {
Octokit
};
let octokit;
const ONE_HOUR = 1000*60*60,
ONE_DAY = ONE_HOUR * 24;
let requestsInThisHour = 0,
githubOpsIssueForTheDay, issueUpdatedInThisHour = false;
/* c8 ignore start */
// not testing this as no time and is manually tested. If you are touching this code, manual test thoroughly
function _resetTPH() {
requestsInThisHour = 0;
issueUpdatedInThisHour = false;
}
function _resetGithubOpsIssue() {
githubOpsIssueForTheDay = null;
}
export function setupGitHubOpsMonitoring() {
if(process.env.TEST_ENV){
console.log("test environment detected, disabling setupGitHubOpsMonitoring flow");
return;
}
setInterval(_resetTPH, ONE_HOUR);
setInterval(_resetGithubOpsIssue, ONE_DAY);
}
/* c8 ignore stop */
async function _newRequestMetric() {
requestsInThisHour++;
if(requestsInThisHour > githubHourlyRateLimit/2) {
let opsRepoSplit = opsRepo.split("/"); //Eg. "phcode-dev/extensionService"
if(issueUpdatedInThisHour){
return;
}
issueUpdatedInThisHour = true;
const message = `Github API requests for the hour is at ${requestsInThisHour}, Max allowed is ${githubHourlyRateLimit}`;
if(!githubOpsIssueForTheDay) {
githubOpsIssueForTheDay = await createIssue(opsRepoSplit[0], opsRepoSplit[1],
`[OPS-${stage}] Github Rate Limit above threshold.`, message);
} else {
await commentOnIssue(opsRepoSplit[0], opsRepoSplit[1], githubOpsIssueForTheDay.number, message);
}
}
}
export function initGitHubClient() {
if(octokit){
console.warn("GitHub Client already initialized.");
return;
}
octokit = new _gitHub.Octokit({
auth: githubAPIToken,
userAgent: 'phcode.dev extensions service',
log: {
debug: () => {},
info: () => {},
warn: console.warn,
error: console.error
}
});
}
/**
* Creates a new issue.
* @param {string} owner
* @param {string} repo
* @param {string} title
* @param {string} body
* @return {Promise<{number:number, html_url:string}>}
*/
export async function createIssue(owner, repo, title, body) {
// https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#create-an-issue
// {... "html_url": "https://github.com/octocat/Hello-World/issues/1347", ...}
console.log("create Github issue: ", arguments);
_newRequestMetric();
let response = await octokit.request(`POST /repos/${owner}/${repo}/issues`, {
owner,
repo,
title,
body
});
console.log("created issue: ", response.data.html_url);
return {
number: response.data.number,
html_url: response.data.html_url
};
}
/**
* comments on an issue
* @param {string} owner
* @param {string} repo
* @param {string|number} issueNumber
* @param {string} commentString
* @return {Promise<{html_url:string}>}
*/
export async function commentOnIssue(owner, repo, issueNumber, commentString) {
// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28
console.log("Comment on Github issue: ", arguments);
_newRequestMetric();
let response = await octokit.request(`POST /repos/${owner}/${repo}/issues/${issueNumber}/comments`, {
owner,
repo,
issue_number: `${issueNumber}`,
body: commentString
});
console.log("commented on issue: ", response.data.html_url);
return {
html_url: response.data.html_url
};
}
/**
* Get the org details
* or null if org doesn't exist/ it is just a plain user and not an org.
* @param {string} org
* @return {Promise<{is_verified:boolean, html_url:string, blog:string,
* name:string, company:string}> | null} blog is the verified url for the org. null if org doesnt exist
*/
export async function getOrgDetails(org) {
// https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#get-an-organization
console.log("Get Org details: ", arguments);
try{
_newRequestMetric();
let response = await octokit.request(`GET /orgs/${org}`, {
org
});
let orgDetails = {
name: response.data.name,
company: response.data.company,
blog: response.data.blog,
is_verified: response.data.is_verified,
html_url: response.data.html_url
};
console.log("GitHub org details: ", orgDetails);
return orgDetails;
} catch (e) {
if(e.status === 404){
console.log("no such org: ", org);
return null;
}
console.error("error getting github org: ", e);
throw e;
}
}
/**
* Get the repo details
* or null if repo doesn't exist.
* @param {string} owner
* @param {string} repo
* @return {Promise<{html_url:string, stargazers_count:number}> | null}
*/
export async function getRepoDetails(owner, repo, log = true) {
// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#get-a-repository
log && console.log("Get Repo details: ", arguments);
try{
_newRequestMetric();
let response = await octokit.request(`GET /repos/${owner}/${repo}`, {
owner, repo
});
let repoDetails = {
html_url: response.data.html_url,
stargazers_count: response.data.stargazers_count
};
log && console.log("GitHub repo details: ", repoDetails);
return repoDetails;
} catch (e) {
if(e.status === 404){
console.log("no such repo: ", owner, repo);
return null;
}
console.error("error getting github repo: ", e);
throw e;
}
}
/**
* Get the release details
* or null if release doesn't exist.
* @param {string} owner
* @param {string} repo
* @param {string} tag
* @return {Promise<{html_url:string, draft:boolean, prerelease:boolean,
* assets:Array<{browser_download_url:string, name:string, size: number, content_type:string}>}> | null}
*/
export async function getReleaseDetails(owner, repo, tag) {
// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#get-a-repository
console.log("Get Release details: ", arguments);
try{
_newRequestMetric();
let response = await octokit.request(`GET /repos/${owner}/${repo}/releases/tags/${tag}`, {
owner, repo, tag
});
let releaseDetails = {
html_url: response.data.html_url,
draft: response.data.draft,
prerelease: response.data.prerelease,
assets: []
};
for(let asset of response.data.assets){
releaseDetails.assets.push({
//Eg. "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
browser_download_url: asset.browser_download_url,
name: asset.name, //"example.zip",
size: asset.size, // 1024
content_type: asset.content_type // Eg. application/zip
});
}
console.log("GitHub release details: ", releaseDetails);
return releaseDetails;
} catch (e) {
if(e.status === 404){
console.log("no such release: ", owner, repo);
return null;
}
console.error("error getting github release: ", e);
throw e;
}
}