Skip to content

Commit

Permalink
Implement GitLab source linking
Browse files Browse the repository at this point in the history
  • Loading branch information
srmagura committed Oct 24, 2021
1 parent 8c44bb3 commit 53ed8bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/lib/converter/plugins/SourceLinkPlugin.ts
Expand Up @@ -89,6 +89,10 @@ export class Repository {
match = /(bitbucket.org)[:/]([^/]+)\/(.*)/.exec(repoLinks[i]);
}

if (!match) {
match = /(gitlab.com)[:/]([^/]+)\/(.*)/.exec(repoLinks[i]);
}

if (match) {
this.hostname = match[1];
this.user = match[2];
Expand All @@ -103,9 +107,13 @@ export class Repository {
}
}

if (this.hostname.includes("bitbucket.org"))
if (this.hostname.includes("bitbucket.org")) {
this.type = RepositoryType.Bitbucket;
else this.type = RepositoryType.GitHub;
} else if (this.hostname.includes("gitlab.com")) {
this.type = RepositoryType.GitLab;
} else {
this.type = RepositoryType.GitHub;
}

let out = git("-C", path, "ls-files");
if (out.status === 0) {
Expand Down Expand Up @@ -149,10 +157,13 @@ export class Repository {
`https://${this.hostname}`,
this.user,
this.project,
this.type === "github" ? "blob" : "src",
this.type === RepositoryType.GitLab ? "-" : undefined,
this.type === RepositoryType.Bitbucket ? "src" : "blob",
this.branch,
fileName.substr(this.path.length + 1),
].join("/");
]
.filter((s) => !!s)
.join("/");
}

/**
Expand Down Expand Up @@ -190,6 +201,7 @@ export class Repository {
switch (repositoryType) {
default:
case RepositoryType.GitHub:
case RepositoryType.GitLab:
return "L" + lineNumber;
case RepositoryType.Bitbucket:
return "lines-" + lineNumber;
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/sources/repository.ts
@@ -1,4 +1,5 @@
export enum RepositoryType {
GitHub = "github",
Bitbucket = "bitbucket",
GitLab = "gitlab",
}
38 changes: 38 additions & 0 deletions src/test/SourceLinkPlugin.test.ts
Expand Up @@ -69,6 +69,28 @@ describe("Repository", function () {
equal(repository.project, "foobar");
equal(repository.type, RepositoryType.Bitbucket);
});

it("handles a GitLab HTTPS URL", function () {
const mockRemotes = ["https://gitlab.com/joebloggs/foobar.git"];

const repository = new Repository("", "", mockRemotes);

equal(repository.hostname, "gitlab.com");
equal(repository.user, "joebloggs");
equal(repository.project, "foobar");
equal(repository.type, RepositoryType.GitLab);
});

it("handles a GitLab SSH URL", function () {
const mockRemotes = ["git@gitlab.com:joebloggs/foobar.git"];

const repository = new Repository("", "", mockRemotes);

equal(repository.hostname, "gitlab.com");
equal(repository.user, "joebloggs");
equal(repository.project, "foobar");
equal(repository.type, RepositoryType.GitLab);
});
});

describe("getURL", () => {
Expand Down Expand Up @@ -108,5 +130,21 @@ describe("Repository", function () {
"https://bitbucket.org/joebloggs/foobar/src/main/src/index.ts"
);
});

it("returns a GitLab URL", function () {
const mockRemotes = ["https://gitlab.com/joebloggs/foobar.git"];

const repository = new Repository(
repositoryPath,
"main",
mockRemotes
);
repository.files = [filePath];

equal(
repository.getURL(filePath),
"https://gitlab.com/joebloggs/foobar/-/blob/main/src/index.ts"
);
});
});
});

0 comments on commit 53ed8bd

Please sign in to comment.