-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPullRequestService.ts
More file actions
293 lines (279 loc) · 8.86 KB
/
Copy pathPullRequestService.ts
File metadata and controls
293 lines (279 loc) · 8.86 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { GithubClient } from "../../client/GithubClient";
import { GithubApiError } from "../../shared/errors/GithubApiError";
import { Commit } from "../commits/commit.types";
import { CommitDTO } from "../commits/commit.dto";
import {
CreatePullRequestParams,
MergePullRequestParams,
PullRequest,
PullRequestFile,
UpdatePullRequestParams,
MergePullRequestResponse,
UpdatePullRequestBranchResponse,
PullRequestReview,
PullRequestCycleTime,
} from "./pull-request.types";
import {
PullRequestDTO,
PullRequestFileDTO,
PullRequestReviewDTO,
} from "./pull-request.dto";
import {
mapCreatePullRequestParams,
mapMergePullRequestParams,
mapPullRequest,
mapPullRequestFiles,
mapPullRequestReviews,
mapPullRequests,
mapUpdatePullRequestParams,
} from "./pull-request.mapper";
import { mapCommits } from "../commits/commit.mapper";
import { assertConfig } from "../../shared/utils/config.utils";
export class PullRequestService {
private readonly path: string;
constructor(private readonly client: GithubClient) {
assertConfig(this.client, ["owner", "repo"]);
this.path = `/repos/${this.client.config.owner}/${this.client.config.repo}/pulls`;
}
/**
* List pull requests of a repository
*
* @returns Array of pull requests
*
* @example
* ```ts
* const pullRequests = await github.pullRequests.list();
* ```
*/
public async list(): Promise<PullRequest[]> {
const response = await this.client.request<PullRequestDTO[]>(this.path);
return mapPullRequests(response.data);
}
/**
* Create a pull request
*
* @param params Configuration for the pull request
* @returns Data of the created pull request
*
* @example
* ```ts
* github.pullRequests.create({
* title: 'Cool new feature',
* body: 'This feature is very helpful and needs pulling',
* head: 'LewieJ08:dev',
* base: 'main'
* });
* ```
*/
public async create(params: CreatePullRequestParams): Promise<PullRequest> {
const body = mapCreatePullRequestParams(params);
const response = await this.client.request<PullRequestDTO>(this.path, {
method: "POST",
body: JSON.stringify(body),
});
return mapPullRequest(response.data);
}
/**
* List details of a pull request by providing its number
*
* @param pullNumber The number that identifies the pull request
* @returns Data of a pull request
*
* @example
* ```ts
* const pullRequest = await github.pullRequests.get(8);
* ```
*/
public async get(pullNumber: number): Promise<PullRequest> {
const response = await this.client.request<PullRequestDTO>(
`${this.path}/${pullNumber}`,
);
return mapPullRequest(response.data);
}
/**
* Update a pull request
*
* @param params Configuration for the pull request to update
* @returns Data of the updated pull request
*
* @example
* ```ts
* github.pullRequests.update({
* pullNumber: 8,
* title: 'new title',
* body: 'updated body',
* state: 'open',
* base: 'main'
* });
* ```
*/
public async update(params: UpdatePullRequestParams): Promise<PullRequest> {
const body = mapUpdatePullRequestParams(params);
const response = await this.client.request<PullRequestDTO>(
`${this.path}/${params.pullNumber}`,
{
method: "PATCH",
body: JSON.stringify(body),
},
);
return mapPullRequest(response.data);
}
/**
* List commits on a pull request
*
* @param pullNumber The number that identifies the pull request
* @returns Array of commits
*
* @example
* ```ts
* const commits = await github.pullRequests.listCommits(8);
* ```
*/
public async listCommits(pullNumber: number): Promise<Commit[]> {
const response = await this.client.request<CommitDTO[]>(
`${this.path}/${pullNumber}/commits`,
);
return mapCommits(response.data);
}
/**
* List the files in a specified pull request
*
* @param pullNumber The number that identifies the pull request
* @returns Array of pull request files
*
* @example
* ```ts
* const pullRequestFiles = await github.pullRequests.listFiles(8);
* ```
*/
public async listFiles(pullNumber: number): Promise<PullRequestFile[]> {
const response = await this.client.request<PullRequestFileDTO[]>(
`${this.path}/${pullNumber}/files`,
);
return mapPullRequestFiles(response.data);
}
/**
* Check if a pull request has been merged
*
* @param pullNumber The number that identifies the pull request
* @returns boolean value that determines if the pull request is merged
*
* @example
* ```ts
* const isMerged = await github.pullRequests.isMerged(8);
* ```
*/
public async isMerged(pullNumber: number): Promise<boolean> {
try {
const response = await this.client.request<null>(
`${this.path}/${pullNumber}/merge`,
);
return response.status === 204;
} catch (error) {
if (error instanceof GithubApiError && error.status === 404) {
return false;
}
throw error;
}
}
/**
* Merge a pull request into the base branch
*
* @param params Configuration for the pull request to merge
* @returns Confirmation of merge with SHA of merge commit
*
* @example
* ```ts
* github.pullRequests.merge({
* pullNumber: 8,
* commitTitle: 'Expand docs',
* commitMessage: 'Add docs for new methods'
* });
* ```
*/
public async merge(
params: MergePullRequestParams,
): Promise<MergePullRequestResponse> {
const body = mapMergePullRequestParams(params);
const response = await this.client.request<MergePullRequestResponse>(
`${this.path}/${params.pullNumber}/merge`,
{
method: "PUT",
body: JSON.stringify(body),
},
);
return response.data;
}
/**
* Updates the pull request with the latest upstream changes
*
* @param pullNumber The number that identifies the pull request
* @param expectedHeadSha The expected SHA of the pull requests HEAD ref.
* This is the most recent commit of the pull request's branch
* @returns Message and URL of pull request
*
* @example
* ```ts
* github.pullRequests.updateBranch(8, '6dcb09b5b57875f334f61aebed695e2e4193db5e');
* ```
*/
public async updateBranch(
pullNumber: number,
expectedHeadSha?: string,
): Promise<UpdatePullRequestBranchResponse> {
const response =
await this.client.request<UpdatePullRequestBranchResponse>(
`${this.path}/${pullNumber}/update-branch`,
{
method: "PUT",
body: JSON.stringify({
expected_head_sha: expectedHeadSha,
}),
},
);
return response.data;
}
/**
* List reviews on a pull request
*
* @param pullNumber The number that identifies the pull request
* @returns Array of pull request reviews
*
* @example
* ```ts
* const reviews = await github.pullRequests.reviews(8);
* ```
*/
public async reviews(pullNumber: number): Promise<PullRequestReview[]> {
const response = await this.client.request<PullRequestReviewDTO[]>(
`${this.path}/${pullNumber}/reviews`,
);
return mapPullRequestReviews(response.data);
}
/**
* Get the cycle time of a pull request — the duration from when it was
* opened to when it was merged
*
* @param pullNumber The number that identifies the pull request
* @returns Cycle time data. `mergedAt`, `totalMs`, and `totalHours` are
* `null` if the pull request has not been merged
*
* @example
* ```ts
* const cycleTime = await github.pullRequests.cycleTime(8);
* ```
*/
public async cycleTime(pullNumber: number): Promise<PullRequestCycleTime> {
const pullRequest = await this.get(pullNumber);
const totalMs = pullRequest.mergedAt
? new Date(pullRequest.mergedAt).getTime() -
new Date(pullRequest.createdAt).getTime()
: null;
return {
openedAt: pullRequest.createdAt,
mergedAt: pullRequest.mergedAt,
totalMs,
totalHours: totalMs !== null ? totalMs / (1000 * 60 * 60) : null,
};
}
}