Skip to content

Commit 5932178

Browse files
committed
Adds default branch autolink
1 parent adb4725 commit 5932178

File tree

11 files changed

+89
-16
lines changed

11 files changed

+89
-16
lines changed

src/annotations/autolinks.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,34 @@ export class Autolinks implements Disposable {
234234
continue;
235235
}
236236

237+
if (ref.referenceType) {
238+
if (ref.referenceType !== 'branchName' && options?.isBranchName) {
239+
continue;
240+
}
241+
if (ref.referenceType !== 'message' && !options?.isBranchName) {
242+
continue;
243+
}
244+
}
245+
237246
ensureCachedRegex(ref, 'plaintext');
238247

239248
do {
240249
match = matchRef(ref);
241250
if (!match?.groups) break;
242251

243252
num = match.groups.issueKeyNumber;
244-
const key = ref.prefix + num;
253+
let key = num;
254+
if (autolinks.has(key)) {
255+
const prevAutolink = autolinks.get(key)!;
256+
if (!ref.prefix) {
257+
continue;
258+
} else if (!prevAutolink.prefix && ref.prefix) {
259+
/** override */
260+
} else {
261+
// add more autolinks
262+
key = ref.prefix + num;
263+
}
264+
}
245265

246266
autolinks.set(key, {
247267
provider: provider,
@@ -633,24 +653,24 @@ function ensureCachedRegex(ref: CacheableAutolinkReference, outputFormat: 'html'
633653
if (outputFormat === 'markdown' && ref.messageMarkdownRegex == null) {
634654
// Extra `\\\\` in `\\\\\\[` is because the markdown is escaped
635655
ref.messageMarkdownRegex = new RegExp(
636-
`(^|\\s|\\(|\\[|\\{)(?<issueKeyNumber>${escapeRegex(encodeHtmlWeak(escapeMarkdown(ref.prefix)))}(${
637-
ref.alphanumeric ? '\\w' : '\\d'
638-
}+))\\b`,
656+
`(^|\\s|\\(|\\[|\\{)(?<issueKeyNumber>${escapeRegex(
657+
encodeHtmlWeak(escapeMarkdown(ref.prefix)),
658+
)}(?<issueKeyNumber>${ref.alphanumeric ? '\\w' : '\\d'}+))\\b`,
639659
ref.ignoreCase ? 'gi' : 'g',
640660
);
641661
} else if (outputFormat === 'html' && ref.messageHtmlRegex == null) {
642662
ref.messageHtmlRegex = new RegExp(
643-
`(^|\\s|\\(|\\[|\\{)(?<issueKeyNumber>${escapeRegex(encodeHtmlWeak(ref.prefix))}(${
663+
`(^|\\s|\\(|\\[|\\{)(${escapeRegex(encodeHtmlWeak(ref.prefix))}(?<issueKeyNumber>${
644664
ref.alphanumeric ? '\\w' : '\\d'
645665
}+))\\b`,
646666
ref.ignoreCase ? 'gi' : 'g',
647667
);
648668
} else if (ref.messageRegex == null) {
649669
ref.messageRegex = new RegExp(
650-
`(^|\\s|\\(|\\[|\\{)(?<issueKeyNumber>${escapeRegex(ref.prefix)}(${ref.alphanumeric ? '\\w' : '\\d'}+))\\b`,
670+
`(^|\\s|\\(|\\[|\\{)(${escapeRegex(ref.prefix)}(?<issueKeyNumber>${ref.alphanumeric ? '\\w' : '\\d'}+))\\b`,
651671
ref.ignoreCase ? 'gi' : 'g',
652672
);
653-
ref.branchNameRegex = new RegExp(`^\\D*(?<issueKeyNumber>(${escapeRegex(ref.prefix)})?\\d*)\\D*$`, 'gi');
673+
ref.branchNameRegex = new RegExp(`(^|\\-|_)(?<prefix>${ref.prefix})(?<issueKeyNumber>\\d+)`, 'gi');
654674
}
655675

656676
return true;

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export interface Config {
253253

254254
export type AnnotationsToggleMode = 'file' | 'window';
255255
export type AutolinkType = 'issue' | 'pullrequest';
256+
export type AutolinkReferenceType = 'message' | 'branchName';
256257

257258
export interface AutolinkReference {
258259
readonly prefix: string;
@@ -261,6 +262,9 @@ export interface AutolinkReference {
261262
readonly alphanumeric?: boolean;
262263
readonly ignoreCase?: boolean;
263264

265+
/** used to split some autolinks logic, consider that default undefined value means that the autolink is applicable for all reference types */
266+
readonly referenceType?: AutolinkReferenceType;
267+
264268
readonly type?: AutolinkType;
265269
readonly description?: string;
266270
readonly descriptor?: ResourceDescriptor;

src/git/remotes/azure-devops.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@ export class AzureDevOpsRemote extends RemoteProvider {
5656
this.project = repoProject;
5757
}
5858

59+
protected override get issueLinkPattern(): string {
60+
const workUrl = this.baseUrl.replace(gitRegex, '/');
61+
return `${workUrl}/_workitems/edit/<num>`;
62+
}
63+
5964
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
6065
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
6166
if (this._autolinks === undefined) {
6267
// Strip off any `_git` part from the repo url
63-
const workUrl = this.baseUrl.replace(gitRegex, '/');
6468
this._autolinks = [
69+
...super.autolinks,
6570
{
6671
prefix: '#',
67-
url: `${workUrl}/_workitems/edit/<num>`,
72+
url: this.issueLinkPattern,
6873
title: `Open Work Item #<num> on ${this.name}`,
6974

7075
type: 'issue',

src/git/remotes/bitbucket-server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export class BitbucketServerRemote extends RemoteProvider {
1616
super(domain, path, protocol, name, custom);
1717
}
1818

19+
protected override get issueLinkPattern(): string {
20+
return `${this.baseUrl}/issues/<num>`;
21+
}
22+
1923
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
2024
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
2125
if (this._autolinks === undefined) {
2226
this._autolinks = [
27+
...super.autolinks,
2328
{
2429
prefix: 'issue #',
25-
url: `${this.baseUrl}/issues/<num>`,
30+
url: this.issueLinkPattern,
2631
title: `Open Issue #<num> on ${this.name}`,
2732

2833
type: 'issue',

src/git/remotes/bitbucket.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export class BitbucketRemote extends RemoteProvider {
1616
super(domain, path, protocol, name, custom);
1717
}
1818

19+
protected override get issueLinkPattern(): string {
20+
return `${this.baseUrl}/issues/<num>`;
21+
}
22+
1923
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
2024
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
2125
if (this._autolinks === undefined) {
2226
this._autolinks = [
27+
...super.autolinks,
2328
{
2429
prefix: 'issue #',
25-
url: `${this.baseUrl}/issues/<num>`,
30+
url: this.issueLinkPattern,
2631
title: `Open Issue #<num> on ${this.name}`,
2732

2833
type: 'issue',

src/git/remotes/custom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export class CustomRemote extends RemoteProvider {
1414
this.urls = urls;
1515
}
1616

17+
protected override get issueLinkPattern(): string {
18+
// TODO: if it's ok, think about passing issue link to cfg.urls or using optional
19+
throw new Error('Method not implemented.');
20+
}
21+
1722
get id(): RemoteProviderId {
1823
return 'custom';
1924
}

src/git/remotes/gerrit.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export class GerritRemote extends RemoteProvider {
3434
super(domain, path, protocol, name, custom);
3535
}
3636

37+
protected override get issueLinkPattern(): string {
38+
// TODO: if it's ok, think about passing issue link to cfg.urls or using optional
39+
throw new Error('Method not implemented.');
40+
}
41+
3742
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
3843
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
3944
if (this._autolinks === undefined) {

src/git/remotes/gitea.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ export class GiteaRemote extends RemoteProvider {
1515
super(domain, path, protocol, name, custom);
1616
}
1717

18+
protected override get issueLinkPattern(): string {
19+
return `${this.baseUrl}/issues/<num>`;
20+
}
21+
1822
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
1923
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
2024
if (this._autolinks === undefined) {
2125
this._autolinks = [
26+
...super.autolinks,
2227
{
2328
prefix: '#',
24-
url: `${this.baseUrl}/issues/<num>`,
29+
url: this.issueLinkPattern,
2530
title: `Open Issue #<num> on ${this.name}`,
2631

2732
type: 'issue',

src/git/remotes/github.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,25 @@ export class GitHubRemote extends RemoteProvider<GitHubRepositoryDescriptor> {
3333
return this.custom ? `${this.protocol}://${this.domain}/api/v3` : `https://api.${this.domain}`;
3434
}
3535

36+
protected override get issueLinkPattern(): string {
37+
return `${this.baseUrl}/issues/<num>`;
38+
}
39+
3640
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
3741
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
3842
if (this._autolinks === undefined) {
3943
this._autolinks = [
44+
...super.autolinks,
4045
{
4146
prefix: '#',
42-
url: `${this.baseUrl}/issues/<num>`,
47+
url: this.issueLinkPattern,
4348
title: `Open Issue or Pull Request #<num> on ${this.name}`,
4449

4550
description: `${this.name} Issue or Pull Request #<num>`,
4651
},
4752
{
4853
prefix: 'gh-',
49-
url: `${this.baseUrl}/issues/<num>`,
54+
url: this.issueLinkPattern,
5055
title: `Open Issue or Pull Request #<num> on ${this.name}`,
5156
ignoreCase: true,
5257

src/git/remotes/gitlab.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ export class GitLabRemote extends RemoteProvider<GitLabRepositoryDescriptor> {
3333
return this.custom ? `${this.protocol}://${this.domain}/api` : `https://${this.domain}/api`;
3434
}
3535

36+
protected override get issueLinkPattern(): string {
37+
return `${this.baseUrl}/-/issues/<num>`;
38+
}
39+
3640
private _autolinks: (AutolinkReference | DynamicAutolinkReference)[] | undefined;
3741
override get autolinks(): (AutolinkReference | DynamicAutolinkReference)[] {
3842
if (this._autolinks === undefined) {
3943
this._autolinks = [
44+
...super.autolinks,
4045
{
4146
prefix: '#',
42-
url: `${this.baseUrl}/-/issues/<num>`,
47+
url: this.issueLinkPattern,
4348
title: `Open Issue #<num> on ${this.name}`,
4449

4550
type: 'issue',

0 commit comments

Comments
 (0)