Skip to content

Commit 107dc57

Browse files
eamodiosergeibbb
authored andcommitted
(wip) Adds create pr urls for other providers
Refs #4142
1 parent 9877724 commit 107dc57

File tree

9 files changed

+137
-22
lines changed

9 files changed

+137
-22
lines changed

src/git/remotes/azure-devops.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,20 @@ export class AzureDevOpsRemote extends RemoteProvider {
182182
return this.encodeUrl(`${this.baseUrl}/commit/${sha}`);
183183
}
184184

185-
protected override getUrlForComparison(base: string, compare: string, _notation: '..' | '...'): string {
186-
return this.encodeUrl(`${this.baseUrl}/branchCompare?baseVersion=GB${base}&targetVersion=GB${compare}`);
185+
protected override getUrlForComparison(base: string, head: string, _notation: '..' | '...'): string {
186+
return this.encodeUrl(`${this.baseUrl}/branchCompare?baseVersion=GB${base}&targetVersion=GB${head}`);
187+
}
188+
189+
protected override getUrlForCreatePullRequest(
190+
base: { branch?: string; remote: { path: string; url: string } },
191+
head: { branch: string; remote: { path: string; url: string } },
192+
): string | undefined {
193+
const query = new URLSearchParams({ sourceRef: head.branch, targetRef: base.branch ?? '' });
194+
// TODO: figure this out
195+
// query.set('sourceRepositoryId', compare.repoId);
196+
// query.set('targetRepositoryId', base.repoId);
197+
198+
return `${this.encodeUrl(`${this.baseUrl}/pullrequestcreate`)}?${query.toString()}`;
187199
}
188200

189201
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/bitbucket-server.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,26 @@ export class BitbucketServerRemote extends RemoteProvider {
157157
return this.encodeUrl(`${this.baseUrl}/commits/${sha}`);
158158
}
159159

160-
protected override getUrlForComparison(base: string, compare: string, _notation: '..' | '...'): string {
161-
return this.encodeUrl(`${this.baseUrl}/branches/compare/${base}%0D${compare}`).replace('%250D', '%0D');
160+
protected override getUrlForComparison(base: string, head: string, _notation: '..' | '...'): string {
161+
return this.encodeUrl(`${this.baseUrl}/branches/compare/${base}%0D${head}`).replace('%250D', '%0D');
162+
}
163+
164+
protected override getUrlForCreatePullRequest(
165+
base: { branch?: string; remote: { path: string; url: string } },
166+
head: { branch: string; remote: { path: string; url: string } },
167+
options?: { title?: string; description?: string },
168+
): string | undefined {
169+
const query = new URLSearchParams({ sourceBranch: head.branch, targetBranch: base.branch ?? '' });
170+
// TODO: figure this out
171+
// query.set('targetRepoId', base.repoId);
172+
if (options?.title) {
173+
query.set('title', options.title);
174+
}
175+
if (options?.description) {
176+
query.set('description', options.description);
177+
}
178+
179+
return `${this.encodeUrl(`${this.baseUrl}/pull-requests?create`)}&${query.toString()}`;
162180
}
163181

164182
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/bitbucket.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Range, Uri } from 'vscode';
22
import type { AutolinkReference, DynamicAutolinkReference } from '../../autolinks/models/autolinks';
3+
import type { RepositoryDescriptor } from '../../plus/integrations/integration';
34
import type { Brand, Unbrand } from '../../system/brand';
45
import type { Repository } from '../models/repository';
56
import type { GkProviderId } from '../models/repositoryIdentities';
@@ -10,7 +11,7 @@ import { RemoteProvider } from './remoteProvider';
1011
const fileRegex = /^\/([^/]+)\/([^/]+?)\/src(.+)$/i;
1112
const rangeRegex = /^lines-(\d+)(?::(\d+))?$/;
1213

13-
export class BitbucketRemote extends RemoteProvider {
14+
export class BitbucketRemote extends RemoteProvider<RepositoryDescriptor> {
1415
constructor(domain: string, path: string, protocol?: string, name?: string, custom: boolean = false) {
1516
super(domain, path, protocol, name, custom);
1617
}
@@ -142,8 +143,18 @@ export class BitbucketRemote extends RemoteProvider {
142143
return this.encodeUrl(`${this.baseUrl}/commits/${sha}`);
143144
}
144145

145-
protected override getUrlForComparison(base: string, compare: string, _notation: '..' | '...'): string {
146-
return this.encodeUrl(`${this.baseUrl}/branches/compare/${base}%0D${compare}`).replace('%250D', '%0D');
146+
protected override getUrlForComparison(base: string, head: string, _notation: '..' | '...'): string {
147+
return this.encodeUrl(`${this.baseUrl}/branches/compare/${base}%0D${head}`).replace('%250D', '%0D');
148+
}
149+
150+
protected override getUrlForCreatePullRequest(
151+
base: { branch?: string; remote: { path: string; url: string } },
152+
head: { branch: string; remote: { path: string; url: string } },
153+
_options?: { title?: string; description?: string },
154+
): string | undefined {
155+
const { owner, name } = this.repoDesc;
156+
const query = new URLSearchParams({ source: head.branch, dest: `${owner}/${name}::${base.branch ?? ''}` });
157+
return `${this.encodeUrl(`${this.baseUrl}/pull-requests/new`)}?${query.toString()}`;
147158
}
148159

149160
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/custom.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,23 @@ export class CustomRemote extends RemoteProvider {
5858
return this.getUrl(this.urls.commit, this.getContext({ id: sha }));
5959
}
6060

61-
protected override getUrlForComparison(base: string, compare: string, notation: '..' | '...'): string | undefined {
61+
protected override getUrlForComparison(base: string, head: string, notation: '..' | '...'): string | undefined {
6262
if (this.urls.comparison == null) return undefined;
6363

64-
return this.getUrl(this.urls.comparison, this.getContext({ ref1: base, ref2: compare, notation: notation }));
64+
return this.getUrl(this.urls.comparison, this.getContext({ ref1: base, ref2: head, notation: notation }));
65+
}
66+
67+
protected override getUrlForCreatePullRequest(
68+
base: { branch?: string; remote: { path: string; url: string } },
69+
compare: { branch: string; remote: { path: string; url: string } },
70+
_options?: { title?: string; description?: string },
71+
): string | undefined {
72+
if (this.urls.createPullRequest == null) return undefined;
73+
74+
return this.getUrl(
75+
this.urls.createPullRequest,
76+
this.getContext({ base: base.branch ?? '', head: compare.branch }),
77+
);
6578
}
6679

6780
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/gerrit.ts

+10
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ export class GerritRemote extends RemoteProvider {
193193
return this.encodeUrl(`${this.baseReviewUrl}/q/${base}${notation}${head}`);
194194
}
195195

196+
protected override getUrlForCreatePullRequest(
197+
base: { branch?: string; remote: { path: string; url: string } },
198+
head: { branch: string; remote: { path: string; url: string } },
199+
_options?: { title?: string; description?: string },
200+
): string | undefined {
201+
const query = new URLSearchParams({ sourceBranch: head.branch, targetBranch: base.branch ?? '' });
202+
203+
return this.encodeUrl(`${this.baseReviewUrl}/createPullRequest?${query.toString()}`);
204+
}
205+
196206
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {
197207
const line = range != null ? `#${range.start.line}` : '';
198208

src/git/remotes/gitea.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,23 @@ export class GiteaRemote extends RemoteProvider {
139139
return this.encodeUrl(`${this.baseUrl}/commit/${sha}`);
140140
}
141141

142-
protected override getUrlForComparison(ref1: string, ref2: string, _notation: '..' | '...'): string {
143-
return this.encodeUrl(`${this.baseUrl}/compare/${ref1}...${ref2}`);
142+
protected override getUrlForComparison(base: string, head: string, _notation: '..' | '...'): string {
143+
return this.encodeUrl(`${this.baseUrl}/compare/${base}...${head}`);
144+
}
145+
146+
protected override getUrlForCreatePullRequest(
147+
base: { branch?: string; remote: { path: string; url: string } },
148+
head: { branch: string; remote: { path: string; url: string } },
149+
options?: { title?: string; description?: string },
150+
): string | undefined {
151+
const query = new URLSearchParams({ head: head.branch, base: base.branch ?? '' });
152+
if (options?.title) {
153+
query.set('title', options.title);
154+
}
155+
if (options?.description) {
156+
query.set('body', options.description);
157+
}
158+
return `${this.encodeUrl(`${this.baseUrl}/pulls/new`)}?${query.toString()}`;
144159
}
145160

146161
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/github.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,33 @@ export class GitHubRemote extends RemoteProvider<GitHubRepositoryDescriptor> {
276276
return this.encodeUrl(`${this.baseUrl}/commit/${sha}`);
277277
}
278278

279-
protected override getUrlForComparison(base: string, compare: string, notation: '..' | '...'): string {
280-
return this.encodeUrl(`${this.baseUrl}/compare/${base}${notation}${compare}`);
279+
protected override getUrlForComparison(base: string, head: string, notation: '..' | '...'): string {
280+
return this.encodeUrl(`${this.baseUrl}/compare/${base}${notation}${head}`);
281281
}
282282

283283
protected override getUrlForCreatePullRequest(
284284
base: { branch?: string; remote: { path: string; url: string } },
285-
compare: { branch: string; remote: { path: string; url: string } },
285+
head: { branch: string; remote: { path: string; url: string } },
286+
options?: { title?: string; description?: string },
286287
): string | undefined {
287-
if (base.remote.url === compare.remote.url) {
288-
return this.encodeUrl(`${this.baseUrl}/pull/new/${base.branch ?? 'HEAD'}...${compare.branch}`);
288+
const query = new URLSearchParams();
289+
if (options?.title) {
290+
query.set('title', options.title);
291+
}
292+
if (options?.description) {
293+
query.set('body', options.description);
294+
}
295+
296+
if (base.remote.url === head.remote.url) {
297+
return `${this.encodeUrl(
298+
`${this.baseUrl}/pull/new/${base.branch ?? 'HEAD'}...${head.branch}`,
299+
)}?${query.toString()}`;
289300
}
290301

291-
const [owner] = compare.remote.path.split('/', 1);
292-
return this.encodeUrl(`${this.baseUrl}/pull/new/${base.branch ?? 'HEAD'}...${owner}:${compare.branch}`);
302+
const [owner] = head.remote.path.split('/', 1);
303+
return `${this.encodeUrl(
304+
`${this.baseUrl}/pull/new/${base.branch ?? 'HEAD'}...${owner}:${head.branch}`,
305+
)}?${query.toString()}`;
293306
}
294307

295308
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/gitlab.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,31 @@ export class GitLabRemote extends RemoteProvider<GitLabRepositoryDescriptor> {
372372
return this.encodeUrl(`${this.baseUrl}/-/commit/${sha}`);
373373
}
374374

375-
protected override getUrlForComparison(base: string, compare: string, notation: '..' | '...'): string {
376-
return this.encodeUrl(`${this.baseUrl}/-/compare/${base}${notation}${compare}`);
375+
protected override getUrlForComparison(base: string, head: string, notation: '..' | '...'): string {
376+
return this.encodeUrl(`${this.baseUrl}/-/compare/${base}${notation}${head}`);
377+
}
378+
379+
protected override getUrlForCreatePullRequest(
380+
base: { branch?: string; remote: { path: string; url: string } },
381+
head: { branch: string; remote: { path: string; url: string } },
382+
options?: { title?: string; description?: string },
383+
): string | undefined {
384+
const query = new URLSearchParams({
385+
utf8: '✓',
386+
'merge_request["source_branch"]': head.branch,
387+
'merge_request["target_branch"]': base.branch ?? '',
388+
// TODO: figure this out
389+
// 'merge_request["source_project_id"]': this.path,
390+
// 'merge_request["target_project_id"]': this.path,
391+
});
392+
if (options?.title) {
393+
query.set('merge_request[title]', options.title);
394+
}
395+
if (options?.description) {
396+
query.set('merge_request[description]', options.description);
397+
}
398+
399+
return `${this.encodeUrl(`${this.baseUrl}/-/merge_requests/new`)}?${query.toString()}`;
377400
}
378401

379402
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {

src/git/remotes/remoteProvider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export abstract class RemoteProvider<T extends ResourceDescriptor = ResourceDesc
132132
return this.getUrlForComparison(resource.base, resource.compare, resource.notation ?? '...');
133133
}
134134
case RemoteResourceType.CreatePullRequest: {
135-
return this.getUrlForCreatePullRequest?.(resource.base, resource.compare);
135+
return this.getUrlForCreatePullRequest(resource.base, resource.compare);
136136
}
137137
case RemoteResourceType.File:
138138
return this.getUrlForFile(
@@ -182,7 +182,7 @@ export abstract class RemoteProvider<T extends ResourceDescriptor = ResourceDesc
182182

183183
protected abstract getUrlForComparison(base: string, head: string, notation: '..' | '...'): string | undefined;
184184

185-
protected getUrlForCreatePullRequest?(
185+
protected abstract getUrlForCreatePullRequest(
186186
base: { branch?: string; remote: { path: string; url: string } },
187187
head: { branch: string; remote: { path: string; url: string } },
188188
options?: { title?: string; description?: string },

0 commit comments

Comments
 (0)