Skip to content

Commit 53ed8bd

Browse files
committed
Implement GitLab source linking
Closes #1728
1 parent 8c44bb3 commit 53ed8bd

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/lib/converter/plugins/SourceLinkPlugin.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export class Repository {
8989
match = /(bitbucket.org)[:/]([^/]+)\/(.*)/.exec(repoLinks[i]);
9090
}
9191

92+
if (!match) {
93+
match = /(gitlab.com)[:/]([^/]+)\/(.*)/.exec(repoLinks[i]);
94+
}
95+
9296
if (match) {
9397
this.hostname = match[1];
9498
this.user = match[2];
@@ -103,9 +107,13 @@ export class Repository {
103107
}
104108
}
105109

106-
if (this.hostname.includes("bitbucket.org"))
110+
if (this.hostname.includes("bitbucket.org")) {
107111
this.type = RepositoryType.Bitbucket;
108-
else this.type = RepositoryType.GitHub;
112+
} else if (this.hostname.includes("gitlab.com")) {
113+
this.type = RepositoryType.GitLab;
114+
} else {
115+
this.type = RepositoryType.GitHub;
116+
}
109117

110118
let out = git("-C", path, "ls-files");
111119
if (out.status === 0) {
@@ -149,10 +157,13 @@ export class Repository {
149157
`https://${this.hostname}`,
150158
this.user,
151159
this.project,
152-
this.type === "github" ? "blob" : "src",
160+
this.type === RepositoryType.GitLab ? "-" : undefined,
161+
this.type === RepositoryType.Bitbucket ? "src" : "blob",
153162
this.branch,
154163
fileName.substr(this.path.length + 1),
155-
].join("/");
164+
]
165+
.filter((s) => !!s)
166+
.join("/");
156167
}
157168

158169
/**
@@ -190,6 +201,7 @@ export class Repository {
190201
switch (repositoryType) {
191202
default:
192203
case RepositoryType.GitHub:
204+
case RepositoryType.GitLab:
193205
return "L" + lineNumber;
194206
case RepositoryType.Bitbucket:
195207
return "lines-" + lineNumber;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum RepositoryType {
22
GitHub = "github",
33
Bitbucket = "bitbucket",
4+
GitLab = "gitlab",
45
}

src/test/SourceLinkPlugin.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ describe("Repository", function () {
6969
equal(repository.project, "foobar");
7070
equal(repository.type, RepositoryType.Bitbucket);
7171
});
72+
73+
it("handles a GitLab HTTPS URL", function () {
74+
const mockRemotes = ["https://gitlab.com/joebloggs/foobar.git"];
75+
76+
const repository = new Repository("", "", mockRemotes);
77+
78+
equal(repository.hostname, "gitlab.com");
79+
equal(repository.user, "joebloggs");
80+
equal(repository.project, "foobar");
81+
equal(repository.type, RepositoryType.GitLab);
82+
});
83+
84+
it("handles a GitLab SSH URL", function () {
85+
const mockRemotes = ["[email protected]:joebloggs/foobar.git"];
86+
87+
const repository = new Repository("", "", mockRemotes);
88+
89+
equal(repository.hostname, "gitlab.com");
90+
equal(repository.user, "joebloggs");
91+
equal(repository.project, "foobar");
92+
equal(repository.type, RepositoryType.GitLab);
93+
});
7294
});
7395

7496
describe("getURL", () => {
@@ -108,5 +130,21 @@ describe("Repository", function () {
108130
"https://bitbucket.org/joebloggs/foobar/src/main/src/index.ts"
109131
);
110132
});
133+
134+
it("returns a GitLab URL", function () {
135+
const mockRemotes = ["https://gitlab.com/joebloggs/foobar.git"];
136+
137+
const repository = new Repository(
138+
repositoryPath,
139+
"main",
140+
mockRemotes
141+
);
142+
repository.files = [filePath];
143+
144+
equal(
145+
repository.getURL(filePath),
146+
"https://gitlab.com/joebloggs/foobar/-/blob/main/src/index.ts"
147+
);
148+
});
111149
});
112150
});

0 commit comments

Comments
 (0)