Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/data/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getBugURL,
getBugLinkTitle,
getSourceUrlTitle,
getSourceUrl,
} from "../formatters/links";
import { getCodeSearchLink } from "../formatters/codesearch";
import { getPingReasons } from "../formatters/text";
Expand Down Expand Up @@ -76,6 +77,7 @@ export const METRIC_DEFINITION_SCHEMA = [
type: "link",
helpText:
"Where the source definition of the metric may be found (referencing the first commit in which it was introduced).",
linkFormatter: getSourceUrl,
valueFormatter: getSourceUrlTitle,
},
{
Expand Down
20 changes: 18 additions & 2 deletions src/formatters/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,28 @@ export function getBugLinkTitle(ref) {
return url.replace(/^http(s?):\/\//, "");
}

export function getSourceUrl(ref) {
if (ref.includes("github.com/mozilla-firefox/firefox")) {
return ref
.replace(
"github.com/mozilla-firefox/firefox/blob/",
"searchfox.org/firefox-main/rev/"
)
.replace("#L", "#");
}
return ref;
}

export function getSourceUrlTitle(url) {
if (url.includes("github.com")) {
return url.replace(
/[^\d]+\/([^\d]+)\/([^\d]+)\/([^\d]+)\/([^/]+)\/(.*)/,
(_, orgName, repoName, _blob, _hash, path) =>
`${orgName}/${repoName}/${path}`
(_, orgName, repoName, _blob, _hash, path) => {
if (orgName === "mozilla-firefox" && repoName === "firefox") {
return path.replace("#L", "#");
}
return `${orgName}/${repoName}/${path}`;
}
);
}
return url;
Expand Down
26 changes: 25 additions & 1 deletion tests/formatters.links.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getBugLinkTitle, getSourceUrlTitle } from "../src/formatters/links";
import {
getBugLinkTitle,
getSourceUrl,
getSourceUrlTitle,
} from "../src/formatters/links";

describe("Titles for bugzilla URLs", () => {
it("works as expected", () => {
Expand Down Expand Up @@ -34,6 +38,18 @@ describe("Titles for other issue tracker URLs", () => {
});
});

describe("Links for source URL", () => {
it("converts github.com/mozilla-firefox/firefox links to searchfox.org links", () => {
expect(
getSourceUrl(
"https://github.com/mozilla-firefox/firefox/blob/b52296d542b89092ffd707ca478a85ebf0f89ff5/accessible/metrics.yaml#L14"
)
).toBe(
"https://searchfox.org/firefox-main/rev/b52296d542b89092ffd707ca478a85ebf0f89ff5/accessible/metrics.yaml#14"
);
});
});

describe("Titles for source definition", () => {
it("works as expected", () => {
expect(
Expand All @@ -42,4 +58,12 @@ describe("Titles for source definition", () => {
)
).toBe("mozilla-mobile/fenix/app/metrics.yaml#L1234");
});

it("only shows path and changes line hash for mozilla-firefox/firefox", () => {
expect(
getSourceUrlTitle(
"https://github.com/mozilla-firefox/firefox/blob/b52296d542b89092ffd707ca478a85ebf0f89ff5/accessible/metrics.yaml#L14"
)
).toBe("accessible/metrics.yaml#14");
});
});