-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathgithub-client.ts
More file actions
221 lines (187 loc) · 5.17 KB
/
github-client.ts
File metadata and controls
221 lines (187 loc) · 5.17 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
export {};
import request, { RequestResponse } from "request";
import parseLinkHeader from "parse-link-header";
interface Config {
owner?: string;
repo?: string;
token?: string;
head?: string;
base?: string;
endpoint?: string;
}
export interface PullRequest {
title: string;
body: string;
}
export default class GithubClient {
private owner: string;
private repo: string;
private token: string;
private head: string;
private base: string;
private endpoint: string;
constructor(config: Config) {
this.owner = config.owner || process.env.GITHUB_PR_RELEASE_OWNER;
this.repo = config.repo || process.env.GITHUB_PR_RELEASE_REPO;
this.token = config.token || process.env.GITHUB_PR_RELEASE_TOKEN;
this.head = config.head || process.env.GITHUB_PR_RELEASE_HEAD || "master";
this.base =
config.base || process.env.GITHUB_PR_RELEASE_BASE || "production";
this.endpoint =
config.endpoint ||
process.env.GITHUB_PR_RELEASE_ENDPOINT ||
"https://api.github.com";
}
private pullRequestEndpoint() {
return this.endpoint + "/repos/" + this.owner + "/" + this.repo + "/pulls";
}
private headers() {
return {
Authorization: "token " + this.token,
"User-Agent": "uiur/github-pr-release",
};
}
private get(url: string, query: object) {
query = query || {};
return new Promise((resolve, reject) => {
request.get(
{
url: url,
qs: query,
headers: this.headers(),
json: true,
},
(err, res) => {
if (err) return reject(err);
resolve(res);
}
);
});
}
private post(url: string, body: object) {
body = body || {};
return new Promise((resolve, reject) => {
request.post(
{
url: url,
body: body,
json: true,
headers: this.headers(),
},
function (err, res, body) {
if (err) return reject(err);
resolve(res);
}
);
});
}
private patch(url: string, body: object) {
body = body || {};
return new Promise((resolve, reject) => {
request.patch(
{
url: url,
body: body,
json: true,
headers: this.headers(),
},
function (err, res, body) {
if (err) return reject(err);
resolve(res);
}
);
});
}
async prepareReleasePR() {
const res: any = await this.post(this.pullRequestEndpoint(), {
title: "Preparing release pull request...",
head: this.head,
base: this.base,
});
if (res.statusCode === 201) {
return res.body;
} else if (res.statusCode === 422) {
const errMessage = res.body.errors[0].message;
if (!errMessage.match(/pull request already exists/)) {
return Promise.reject(new Error(errMessage));
}
const res2: any = await this.get(this.pullRequestEndpoint(), {
base: this.base,
head: this.head,
state: "open",
});
return res2.body[0];
} else {
return Promise.reject(new Error(res.body.message));
}
}
getPRCommits(pr) {
let result = [];
const getCommits = (page) => {
page = page || 1;
return this.get(
this.pullRequestEndpoint() + "/" + pr.number + "/commits",
{
per_page: 100,
page: page,
}
).then(function (res: any) {
const commits = res.body;
result = result.concat(commits);
const link = parseLinkHeader(res.headers.link);
if (link && link.next) {
return getCommits(page + 1);
} else {
return result;
}
});
};
return getCommits(null).catch(console.error.bind(console));
}
async collectReleasePRs(releasePR) {
const commits = await this.getPRCommits(releasePR);
const shas = commits.map((commit) => commit.sha);
return await this.get(this.pullRequestEndpoint(), {
state: "closed",
base: this.head.split(":").at(-1),
per_page: 100,
sort: "updated",
direction: "desc",
}).then(function (res: any) {
const prs = res.body;
const mergedPRs = prs.filter(function (pr) {
return pr.merged_at !== null;
});
const prsToRelease = mergedPRs.reduce(function (result, pr) {
if (
shas.indexOf(pr.head.sha) > -1 ||
shas.indexOf(pr.merge_commit_sha) > -1
) {
result.push(pr);
}
return result;
}, []);
prsToRelease.sort(function (a, b) {
return Number(new Date(a.merged_at)) - Number(new Date(b.merged_at));
});
return prsToRelease;
});
}
assignReviewers(pr, prs) {
const reviewers = prs
.map((pr) => (pr.assignee ? pr.assignee : pr.user))
.filter((user) => user.type === "User")
.map((user) => user.login);
return this.post(
this.pullRequestEndpoint() + "/" + pr.number + "/requested_reviewers",
{ reviewers }
).then(function (res: any) {
return res.body;
});
}
updatePR(pr, data): Promise<PullRequest> {
return this.patch(this.pullRequestEndpoint() + "/" + pr.number, data).then(
(res: any) => res.body
);
}
}