Skip to content

Commit 58181b2

Browse files
authored
Merge pull request #15 from kitagry/gitlab-pull-request-url
✨ Open GitLab merge request page
2 parents a09fd3e + 2339c8d commit 58181b2

File tree

5 files changed

+78
-43
lines changed

5 files changed

+78
-43
lines changed

hosting_service/__snapshots__/mod_test.ts.snap

+40-26
Original file line numberDiff line numberDiff line change
@@ -204,188 +204,202 @@ snapshot[`getHostingService 29`] = `
204204
`;
205205

206206
snapshot[`getHostingService 30`] = `
207+
{
208+
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/merge_requests/1",
209+
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
210+
}
211+
`;
212+
213+
snapshot[`getHostingService 31`] = `
207214
{
208215
result: "https://gitlab.com/lambdalisue/deno-git-browse",
209216
url: "https://gitlab.com/lambdalisue/deno-git-browse",
210217
}
211218
`;
212219

213-
snapshot[`getHostingService 31`] = `
220+
snapshot[`getHostingService 32`] = `
214221
{
215222
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/commit/2120c87633f4059656e8aada8eebe62768892b46",
216223
url: "https://gitlab.com/lambdalisue/deno-git-browse",
217224
}
218225
`;
219226

220-
snapshot[`getHostingService 32`] = `
227+
snapshot[`getHostingService 33`] = `
221228
{
222229
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/tree/v0.1.0/bin",
223230
url: "https://gitlab.com/lambdalisue/deno-git-browse",
224231
}
225232
`;
226233

227-
snapshot[`getHostingService 33`] = `
234+
snapshot[`getHostingService 34`] = `
228235
{
229236
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/blob/v0.1.0/README.md",
230237
url: "https://gitlab.com/lambdalisue/deno-git-browse",
231238
}
232239
`;
233240

234-
snapshot[`getHostingService 34`] = `
241+
snapshot[`getHostingService 35`] = `
235242
{
236243
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/blob/v0.1.0/README.md?plain=1#L10",
237244
url: "https://gitlab.com/lambdalisue/deno-git-browse",
238245
}
239246
`;
240247

241-
snapshot[`getHostingService 35`] = `
248+
snapshot[`getHostingService 36`] = `
242249
{
243250
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/blob/v0.1.0/README.md?plain=1#L10-20",
244251
url: "https://gitlab.com/lambdalisue/deno-git-browse",
245252
}
246253
`;
247254

248-
snapshot[`getHostingService 36`] = `
255+
snapshot[`getHostingService 37`] = `
249256
{
250257
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/blob/v0.1.0/bin/browse.ts",
251258
url: "https://gitlab.com/lambdalisue/deno-git-browse",
252259
}
253260
`;
254261

255-
snapshot[`getHostingService 37`] = `
262+
snapshot[`getHostingService 38`] = `
256263
{
257264
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/blob/v0.1.0/bin/browse.ts?plain=1#L10",
258265
url: "https://gitlab.com/lambdalisue/deno-git-browse",
259266
}
260267
`;
261268

262-
snapshot[`getHostingService 38`] = `
269+
snapshot[`getHostingService 39`] = `
263270
{
264271
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/blob/v0.1.0/bin/browse.ts?plain=1#L10-20",
265272
url: "https://gitlab.com/lambdalisue/deno-git-browse",
266273
}
267274
`;
268275

269-
snapshot[`getHostingService 39`] = `
276+
snapshot[`getHostingService 40`] = `
277+
{
278+
result: "https://gitlab.com/lambdalisue/deno-git-browse/-/merge_requests/1",
279+
url: "https://gitlab.com/lambdalisue/deno-git-browse",
280+
}
281+
`;
282+
283+
snapshot[`getHostingService 41`] = `
270284
{
271285
result: "https://bitbucket.org/lambdalisue/deno-git-browse",
272286
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
273287
}
274288
`;
275289

276-
snapshot[`getHostingService 40`] = `
290+
snapshot[`getHostingService 42`] = `
277291
{
278292
result: "https://bitbucket.org/lambdalisue/deno-git-browse/commits/2120c87633f4059656e8aada8eebe62768892b46",
279293
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
280294
}
281295
`;
282296

283-
snapshot[`getHostingService 41`] = `
297+
snapshot[`getHostingService 43`] = `
284298
{
285299
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin",
286300
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
287301
}
288302
`;
289303

290-
snapshot[`getHostingService 42`] = `
304+
snapshot[`getHostingService 44`] = `
291305
{
292306
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/README.md",
293307
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
294308
}
295309
`;
296310

297-
snapshot[`getHostingService 43`] = `
311+
snapshot[`getHostingService 45`] = `
298312
{
299313
result: "https://bitbucket.org/lambdalisue/deno-git-browse/annotate/v0.1.0/README.md#lines-10",
300314
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
301315
}
302316
`;
303317

304-
snapshot[`getHostingService 44`] = `
318+
snapshot[`getHostingService 46`] = `
305319
{
306320
result: "https://bitbucket.org/lambdalisue/deno-git-browse/annotate/v0.1.0/README.md#lines-10:20",
307321
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
308322
}
309323
`;
310324

311-
snapshot[`getHostingService 45`] = `
325+
snapshot[`getHostingService 47`] = `
312326
{
313327
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin/browse.ts",
314328
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
315329
}
316330
`;
317331

318-
snapshot[`getHostingService 46`] = `
332+
snapshot[`getHostingService 48`] = `
319333
{
320334
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin/browse.ts#lines-10",
321335
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
322336
}
323337
`;
324338

325-
snapshot[`getHostingService 47`] = `
339+
snapshot[`getHostingService 49`] = `
326340
{
327341
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin/browse.ts#lines-10:20",
328342
url: "ssh://[email protected]/lambdalisue/deno-git-browse",
329343
}
330344
`;
331345

332-
snapshot[`getHostingService 48`] = `
346+
snapshot[`getHostingService 50`] = `
333347
{
334348
result: "https://bitbucket.org/lambdalisue/deno-git-browse",
335349
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
336350
}
337351
`;
338352

339-
snapshot[`getHostingService 49`] = `
353+
snapshot[`getHostingService 51`] = `
340354
{
341355
result: "https://bitbucket.org/lambdalisue/deno-git-browse/commits/2120c87633f4059656e8aada8eebe62768892b46",
342356
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
343357
}
344358
`;
345359

346-
snapshot[`getHostingService 50`] = `
360+
snapshot[`getHostingService 52`] = `
347361
{
348362
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin",
349363
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
350364
}
351365
`;
352366

353-
snapshot[`getHostingService 51`] = `
367+
snapshot[`getHostingService 53`] = `
354368
{
355369
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/README.md",
356370
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
357371
}
358372
`;
359373

360-
snapshot[`getHostingService 52`] = `
374+
snapshot[`getHostingService 54`] = `
361375
{
362376
result: "https://bitbucket.org/lambdalisue/deno-git-browse/annotate/v0.1.0/README.md#lines-10",
363377
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
364378
}
365379
`;
366380

367-
snapshot[`getHostingService 53`] = `
381+
snapshot[`getHostingService 55`] = `
368382
{
369383
result: "https://bitbucket.org/lambdalisue/deno-git-browse/annotate/v0.1.0/README.md#lines-10:20",
370384
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
371385
}
372386
`;
373387

374-
snapshot[`getHostingService 54`] = `
388+
snapshot[`getHostingService 56`] = `
375389
{
376390
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin/browse.ts",
377391
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
378392
}
379393
`;
380394

381-
snapshot[`getHostingService 55`] = `
395+
snapshot[`getHostingService 57`] = `
382396
{
383397
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin/browse.ts#lines-10",
384398
url: "https://bitbucket.org/lambdalisue/deno-git-browse",
385399
}
386400
`;
387401

388-
snapshot[`getHostingService 56`] = `
402+
snapshot[`getHostingService 58`] = `
389403
{
390404
result: "https://bitbucket.org/lambdalisue/deno-git-browse/src/v0.1.0/bin/browse.ts#lines-10:20",
391405
url: "https://bitbucket.org/lambdalisue/deno-git-browse",

hosting_service/mod.ts

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export type HostingService = {
3030
number: number,
3131
options?: ExecuteOptions,
3232
): Promise<URL>;
33+
34+
extractPullRequestID?(
35+
commit: string,
36+
): number | undefined;
3337
};
3438

3539
/**

hosting_service/services/github_com.ts

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export const service: HostingService = {
5050
const pathname = `pull/${n}`;
5151
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
5252
},
53+
54+
extractPullRequestID(commit: string): number | undefined {
55+
const m = commit.match(/Merge pull request #(\d+)/);
56+
if (m) {
57+
return Number(m[1]);
58+
}
59+
return undefined;
60+
},
5361
};
5462

5563
function formatURLBase(fetchURL: URL): string {

hosting_service/services/gitlab_com.ts

+18
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ export const service: HostingService = {
4040
const pathname = `-/blob/${commitish}/${path}${suffix}`;
4141
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
4242
},
43+
44+
getPullRequestURL(
45+
fetchURL: URL,
46+
n: number,
47+
_options?: ExecuteOptions,
48+
): Promise<URL> {
49+
const urlBase = formatURLBase(fetchURL);
50+
const pathname = `-/merge_requests/${n}`;
51+
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
52+
},
53+
54+
extractPullRequestID(commit: string): number | undefined {
55+
const m = commit.match(/See merge request (?:.*)!(\d+)/);
56+
if (m) {
57+
return Number(m[1]);
58+
}
59+
return undefined;
60+
},
4361
};
4462

4563
function formatURLBase(fetchURL: URL): string {

pull_request_url.ts

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { execute, ExecuteOptions } from "./process.ts";
2-
import { getHostingService } from "./hosting_service/mod.ts";
2+
import { getHostingService, HostingService } from "./hosting_service/mod.ts";
33
import {
44
__throw,
55
getCommitSHA1,
@@ -30,6 +30,7 @@ export async function getPullRequestURL(
3030
);
3131
}
3232
const pr = await getPullRequestContains(
33+
hostingService,
3334
commitish,
3435
remote,
3536
options,
@@ -41,10 +42,14 @@ export async function getPullRequestURL(
4142
}
4243

4344
async function getPullRequestContains(
45+
hostingService: HostingService,
4446
commitish: string,
4547
remote: string,
4648
options: ExecuteOptions = {},
4749
): Promise<number | undefined> {
50+
if (!hostingService.extractPullRequestID) {
51+
throw new Error("Hosting service has no pull request ID extractor");
52+
}
4853
const branch = await getRemoteDefaultBranch(remote, options) ?? "main";
4954
const sha = await getCommitSHA1(commitish, options) ?? __throw(
5055
new Error(`No commit found for ${commitish}`),
@@ -54,13 +59,12 @@ async function getPullRequestContains(
5459
stdout = await execute(
5560
[
5661
"log",
57-
"--oneline",
5862
"-1",
5963
sha,
6064
],
6165
options,
6266
);
63-
const pr = parseMerges(stdout);
67+
const pr = hostingService.extractPullRequestID(stdout);
6468
if (pr) {
6569
return pr;
6670
}
@@ -70,14 +74,13 @@ async function getPullRequestContains(
7074
[
7175
"log",
7276
"--merges",
73-
"--oneline",
7477
"--reverse",
7578
"--ancestry-path",
7679
ancestraryPath,
7780
],
7881
options,
7982
);
80-
return parseMerges(stdout);
83+
return hostingService.extractPullRequestID(stdout);
8184
}
8285

8386
async function getRemoteDefaultBranch(
@@ -94,15 +97,3 @@ async function getRemoteDefaultBranch(
9497
}
9598
return m[1].trim();
9699
}
97-
98-
function parseMerges(stdout: string): number | undefined {
99-
const record = stdout.trim().split("\n").at(0);
100-
if (!record) {
101-
return undefined;
102-
}
103-
const m = record.match(/Merge pull request #(\d+)/);
104-
if (!m) {
105-
return undefined;
106-
}
107-
return Number(m[1]);
108-
}

0 commit comments

Comments
 (0)