Skip to content

Commit 952bc19

Browse files
committed
(wip) Adds create pr urls for other providers
Refs #4142
1 parent fef314b commit 952bc19

File tree

13 files changed

+156
-27
lines changed

13 files changed

+156
-27
lines changed

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,6 +3903,14 @@
39033903
"type": "string",
39043904
"markdownDescription": "Specifies the format of a commit URL for the custom remote service\n\nAvailable tokens\\\n`${repo}` — repository path\\\n`${id}` — commit SHA"
39053905
},
3906+
"comparison": {
3907+
"type": "string",
3908+
"markdownDescription": "Specifies the format of a comparison URL for the custom remote service\n\nAvailable tokens\\\n`${repo}` — repository path\\\n`${ref1}` — ref 1\\\n`${ref2}` — ref 2\\\n`${notation}` — notation"
3909+
},
3910+
"createPullRequest": {
3911+
"type": "string",
3912+
"markdownDescription": "Specifies the format of a create pull request URL for the custom remote service\n\nAvailable tokens\\\n`${repo}` — repository path\\\n`${base}` — base branch\\\n`${compare}` — compare branch"
3913+
},
39063914
"file": {
39073915
"type": "string",
39083916
"markdownDescription": "Specifies the format of a file URL for the custom remote service\n\nAvailable tokens\\\n`${repo}` — repository path\\\n`${file}` — file name\\\n`${line}` — formatted line information"

src/commands/createPullRequestOnRemote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { GlCommandBase } from './commandBase';
1212
import type { OpenOnRemoteCommandArgs } from './openOnRemote';
1313

1414
export interface CreatePullRequestOnRemoteCommandArgs {
15-
base?: string;
15+
base: string | undefined;
1616
compare: string;
1717
remote: string;
1818
repoPath: string;

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ export interface RemotesUrlsConfig {
638638
readonly branch: string;
639639
readonly commit: string;
640640
readonly comparison?: string;
641+
readonly createPullRequest?: string;
641642
readonly file: string;
642643
readonly fileInBranch: string;
643644
readonly fileInCommit: string;

src/git/models/remoteResource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export type RemoteResource =
3434
| {
3535
type: RemoteResourceType.CreatePullRequest;
3636
base: {
37-
branch?: string;
37+
branch: string | undefined;
3838
remote: { path: string; url: string };
3939
};
4040
compare: {

src/git/remotes/azure-devops.ts

Lines changed: 14 additions & 2 deletions
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

Lines changed: 20 additions & 2 deletions
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

Lines changed: 14 additions & 3 deletions
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

Lines changed: 15 additions & 2 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ export class GerritRemote extends RemoteProvider {
189189
return this.encodeUrl(`${this.baseReviewUrl}/q/${sha}`);
190190
}
191191

192+
protected override getUrlForComparison(base: string, head: string, notation: '..' | '...'): string | undefined {
193+
return this.encodeUrl(`${this.baseReviewUrl}/q/${base}${notation}${head}`);
194+
}
195+
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+
192206
protected getUrlForFile(fileName: string, branch?: string, sha?: string, range?: Range): string {
193207
const line = range != null ? `#${range.start.line}` : '';
194208

src/git/remotes/gitea.ts

Lines changed: 17 additions & 2 deletions
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 {

0 commit comments

Comments
 (0)