This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
/
github.ts
159 lines (134 loc) · 4.06 KB
/
github.ts
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
import retry from "@octokit/plugin-retry";
import throttling from "@octokit/plugin-throttling";
import GitHubApi from "@octokit/rest";
import { URL } from "url";
import { safelyFetchEnvs } from "../lib/envs";
const repo = ({ owner, repo }: Partial<IssueOptions>) =>
`https://github.com/${owner}/${repo}`;
// @ts-ignore
const GitHubWithPlugins = GitHubApi.plugin(throttling).plugin(retry);
const { GH_TOKEN } = safelyFetchEnvs(["GH_TOKEN"]);
interface IssueOptions {
owner: string;
repo: string;
number: number;
}
type GitHubOptions = Partial<IssueOptions> & {
dryRun: boolean;
owner: string;
repo: string;
pullRequest: string;
};
export class GitHub {
private readonly _github: GitHubApi;
private readonly _issueOpts: IssueOptions;
constructor({ dryRun, owner, repo, pullRequest }: GitHubOptions) {
this._github = new GitHubWithPlugins({
auth: `token ${GH_TOKEN}`,
throttle: {
onRateLimit: (retryAfter: number, options: any) => {
this._github.log.warn(
`Request quota exhausted for request ${options.method} ${
options.url
}`
);
// retry three times
if (options.request.retryCount < 3) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (_retryAfter: number, options: any) => {
// does not retry, only logs an error
console.error(
`Abuse detected for request ${options.method} ${options.url}`
);
}
}
});
this._issueOpts = {
owner,
repo,
number: parseInt(pullRequest.split("/").slice(-1)[0])
};
if (dryRun) {
this._github.hook.wrap("request", (req, opts) => {
if (opts.method === "GET") {
return req(opts);
}
// tslint:disable-next-line:no-unused
const { headers, request, body, method, ...otherOptions } = opts;
console.log(`\nAttempting ${method} with options`, otherOptions);
if (body) {
console.log("body present but omitted for brevity");
}
return {
data: []
};
});
}
}
public async getBaseCommitBuild(buildName: string) {
const { data: pr } = await this._github.pulls.get({ ...this._issueOpts });
if (pr.base.ref !== "master") {
console.warn(
`${pr.html_url} might not have been branched off of master...`
);
}
console.log(
`Attempting to get build for ${repo(this._issueOpts)}/commit/${
pr.base.sha
}`
);
const { data: statuses } = await this._github.repos.listStatusesForRef({
owner: this._issueOpts.owner,
repo: this._issueOpts.repo,
ref: pr.base.sha
});
const build = statuses.find(
status =>
status.context === `ci/circleci: ${buildName}` &&
status.state === "success"
);
if (!build) {
console.error("No successful build found for base");
throw new Error();
}
console.log(`Build found: ${build.target_url}`);
return parseInt(new URL(build.target_url).pathname
.split("/")
.pop() as string);
}
public async checkForPRCommentWithString(match: string) {
const comments = await this._github.issues.listComments({
...this._issueOpts
});
return comments.data.find(comment => comment.body.includes(match));
}
public updateComment(comment_id: number, body: string) {
return this._github.issues.updateComment({
...this._issueOpts,
comment_id,
body
});
}
public createComment(body: string) {
return this._github.issues.createComment({
...this._issueOpts,
body
});
}
public deleteComment(comment_id: number) {
return this._github.issues.deleteComment({
...this._issueOpts,
comment_id
});
}
public async getAssignees() {
let issue = await this._github.issues.get({
...this._issueOpts
});
let assignees = issue.data.assignees.map(assignee => `@${assignee.login}`);
return Array.from(new Set([...assignees, "@zephraph"]));
}
}