/
index.ts
264 lines (243 loc) 路 7.42 KB
/
index.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// @ts-ignore
import fetch from "node-fetch";
import DataLoader from "dataloader";
const validRepoNameRegex = /^[\w.-]+\/[\w.-]+$/;
type RequestData =
| { kind: "commit"; repo: string; commit: string }
| { kind: "pull"; repo: string; pull: number };
type ReposWithCommitsAndPRsToFetch = Record<
string,
({ kind: "commit"; commit: string } | { kind: "pull"; pull: number })[]
>;
function makeQuery(repos: ReposWithCommitsAndPRsToFetch) {
return `
query {
${Object.keys(repos)
.map(
(repo, i) =>
`a${i}: repository(
owner: ${JSON.stringify(repo.split("/")[0])}
name: ${JSON.stringify(repo.split("/")[1])}
) {
${repos[repo]
.map((data) =>
data.kind === "commit"
? `a${data.commit}: object(expression: ${JSON.stringify(
data.commit
)}) {
... on Commit {
commitUrl
associatedPullRequests(first: 50) {
nodes {
number
url
mergedAt
author {
login
url
}
}
}
author {
user {
login
url
}
}
}}`
: `pr__${data.pull}: pullRequest(number: ${data.pull}) {
url
author {
login
url
}
mergeCommit {
commitUrl
abbreviatedOid
}
}`
)
.join("\n")}
}`
)
.join("\n")}
}
`;
}
// why are we using dataloader?
// it provides use with two things
// 1. caching
// since getInfo will be called inside of changeset's getReleaseLine
// and there could be a lot of release lines for a single commit
// caching is important so we don't do a bunch of requests for the same commit
// 2. batching
// getReleaseLine will be called a large number of times but it'll be called at the same time
// so instead of doing a bunch of network requests, we can do a single one.
const GHDataLoader = new DataLoader(async (requests: RequestData[]) => {
if (!process.env.GITHUB_TOKEN) {
throw new Error(
"Please create a GitHub personal access token at https://github.com/settings/tokens/new with `read:user` and `repo:status` permissions and add it as the GITHUB_TOKEN environment variable"
);
}
let repos: ReposWithCommitsAndPRsToFetch = {};
requests.forEach(({ repo, ...data }) => {
if (repos[repo] === undefined) {
repos[repo] = [];
}
repos[repo].push(data);
});
const data = await fetch("https://api.github.com/graphql", {
method: "POST",
headers: {
Authorization: `Token ${process.env.GITHUB_TOKEN}`,
},
body: JSON.stringify({ query: makeQuery(repos) }),
}).then((x: any) => x.json());
if (data.errors) {
throw new Error(
`An error occurred when fetching data from GitHub\n${JSON.stringify(
data.errors,
null,
2
)}`
);
}
// this is mainly for the case where there's an authentication problem
if (!data.data) {
throw new Error(
`An error occurred when fetching data from GitHub\n${JSON.stringify(
data
)}`
);
}
let cleanedData: Record<
string,
{ commit: Record<string, any>; pull: Record<string, any> }
> = {};
Object.keys(repos).forEach((repo, index) => {
let output: { commit: Record<string, any>; pull: Record<string, any> } = {
commit: {},
pull: {},
};
cleanedData[repo] = output;
Object.entries(data.data[`a${index}`]).forEach(([field, value]) => {
// this is "a" because that's how it was when it was first written, "a" means it's a commit not a pr
// we could change it to commit__ but then we have to get new GraphQL results from the GH API to put in the tests
if (field[0] === "a") {
output.commit[field.substring(1)] = value;
} else {
output.pull[field.replace("pr__", "")] = value;
}
});
});
return requests.map(
({ repo, ...data }) =>
cleanedData[repo][data.kind][
data.kind === "pull" ? data.pull : data.commit
]
);
});
export async function getInfo(request: {
commit: string;
repo: string;
}): Promise<{
user: string | null;
pull: number | null;
links: {
commit: string;
pull: string | null;
user: string | null;
};
}> {
if (!request.commit) {
throw new Error("Please pass a commit SHA to getInfo");
}
if (!request.repo) {
throw new Error(
"Please pass a GitHub repository in the form of userOrOrg/repoName to getInfo"
);
}
if (!validRepoNameRegex.test(request.repo)) {
throw new Error(
`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)`
);
}
const data = await GHDataLoader.load({ kind: "commit", ...request });
let user = null;
if (data.author && data.author.user) {
user = data.author.user;
}
let associatedPullRequest =
data.associatedPullRequests &&
data.associatedPullRequests.nodes &&
data.associatedPullRequests.nodes.length
? (data.associatedPullRequests.nodes as any[]).sort((a, b) => {
if (a.mergedAt === null && b.mergedAt === null) {
return 0;
}
if (a.mergedAt === null) {
return 1;
}
if (b.mergedAt === null) {
return -1;
}
a = new Date(a.mergedAt);
b = new Date(b.mergedAt);
return a > b ? 1 : a < b ? -1 : 0;
})[0]
: null;
if (associatedPullRequest) {
user = associatedPullRequest.author;
}
return {
user: user ? user.login : null,
pull: associatedPullRequest ? associatedPullRequest.number : null,
links: {
commit: `[\`${request.commit}\`](${data.commitUrl})`,
pull: associatedPullRequest
? `[#${associatedPullRequest.number}](${associatedPullRequest.url})`
: null,
user: user ? `[@${user.login}](${user.url})` : null,
},
};
}
export async function getInfoFromPullRequest(request: {
pull: number;
repo: string;
}): Promise<{
user: string | null;
commit: string | null;
links: {
commit: string | null;
pull: string;
user: string | null;
};
}> {
if (request.pull === undefined) {
throw new Error("Please pass a pull request number");
}
if (!request.repo) {
throw new Error(
"Please pass a GitHub repository in the form of userOrOrg/repoName to getInfo"
);
}
if (!validRepoNameRegex.test(request.repo)) {
throw new Error(
`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)`
);
}
const data = await GHDataLoader.load({ kind: "pull", ...request });
let user = data?.author;
let commit = data?.mergeCommit;
return {
user: user ? user.login : null,
commit: commit ? commit.abbreviatedOid : null,
links: {
commit: commit
? `[\`${commit.abbreviatedOid}\`](${commit.commitUrl})`
: null,
pull: `[#${request.pull}](https://github.com/${request.repo}/pull/${request.pull})`,
user: user ? `[@${user.login}](${user.url})` : null,
},
};
}