Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2248: Fix search path for application-services hosted libs #2280

Merged
merged 2 commits into from
Nov 18, 2024
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: 1 addition & 1 deletion src/components/MetadataTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let item = {};

function format(ref, formatter) {
return formatter ? formatter(ref, appName) : ref;
return formatter ? formatter(ref, appName, item) : ref;
}
</script>

Expand Down
2 changes: 1 addition & 1 deletion src/data/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const METRIC_DEFINITION_SCHEMA = [
type: "link",
helpText:
"Finds uses of this metric using simple matching. Metrics used dynamically may not appear in the results, only metrics defined in the application will be found.",
linkFormatter: (metricId, appName) => getCodeSearchLink(appName, metricId),
linkFormatter: getCodeSearchLink,
},
];
export const METRIC_METADATA_SCHEMA = [
Expand Down
10 changes: 7 additions & 3 deletions src/formatters/codesearch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { camelCase, upperFirst, snakeCase } from "lodash";
import { getGithubRepoName } from "./links";

const searchfoxMap = {
fenix: "fenix",
Expand Down Expand Up @@ -48,7 +49,7 @@ const sourcegraphMap = {
mozilla_vpn: "mozilla-vpn-client",
};

export const getCodeSearchLink = (app, metric) => {
export function getCodeSearchLink(metric, app, item) {
const [category, name] = metric.split(/\.(?=[^.]+$)/);

// Generate all possible casings for a search query:
Expand Down Expand Up @@ -79,9 +80,12 @@ export const getCodeSearchLink = (app, metric) => {
/* eslint no-else-return: "error" */

if (searchfoxMap[app]) {
const repoName = getGithubRepoName(item.source_url);
const searchFoxPath =
repoName === "application-services" ? repoName : searchfoxMap[app];
return app === "firefox_desktop"
? `https://searchfox.org/mozilla-central/search?q=${allLanguagePatterns}&regexp=true`
: `https://searchfox.org/mozilla-mobile/search?q=${allLanguagePatterns}&path=${searchfoxMap[app]}&regexp=true`;
: `https://searchfox.org/mozilla-mobile/search?q=${allLanguagePatterns}&path=${searchFoxPath}&regexp=true`;
} else if (sourcegraphMap[app]) {
return (
`https://sourcegraph.com/search?q=repo:%5Egithub%5C.com%5C/%5BMm%5Dozilla%28.*%29%5C/` +
Expand All @@ -90,4 +94,4 @@ export const getCodeSearchLink = (app, metric) => {
}

return undefined;
};
}
13 changes: 13 additions & 0 deletions src/formatters/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ export function getSourceUrlTitle(url) {
}
return url;
}

export function getGithubRepoName(url) {
const urlObj = new URL(url);
if (!urlObj.hostname.endsWith("github.com")) {
return null;
}
// Segments without falsy values that can be introduced by things like '//'
const pathSegments = urlObj.pathname.split("/").filter(Boolean);
if (pathSegments.length < 2) {
return null;
}
return pathSegments[1];
}
48 changes: 42 additions & 6 deletions tests/formatters.codesearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { getCodeSearchLink } from "../src/formatters/codesearch";

describe("Searchfox links for firefox-desktop", () => {
it("works as expected", () => {
expect(getCodeSearchLink("firefox_desktop", "test.metric_name")).toBe(
expect(
getCodeSearchLink("test.metric_name", "firefox_desktop", {
source_url: "https://foo.bar",
})
).toBe(
"https://searchfox.org/mozilla-central/search?q=test.metric_name|test.metricName|Test.metricName|test.metric_name|test::metric_name&regexp=true"
);
expect(
getCodeSearchLink("firefox_desktop", "test_category.test_name")
getCodeSearchLink("test_category.test_name", "firefox_desktop", {
source_url: "https://foo.bar",
})
).toBe(
"https://searchfox.org/mozilla-central/search?q=test_category.test_name|testCategory.testName|TestCategory.testName|test_category.test_name|test_category::test_name&regexp=true"
);
Expand All @@ -15,25 +21,55 @@ describe("Searchfox links for firefox-desktop", () => {

describe("Searchfox links for applications in mozilla-mobile", () => {
it("works as expected", () => {
expect(getCodeSearchLink("fenix", "test.metric_name")).toBe(
expect(
getCodeSearchLink("test.metric_name", "fenix", {
source_url: "https://foo.bar",
})
).toBe(
"https://searchfox.org/mozilla-mobile/search?q=test.metric_name|test.metricName|Test.metricName|test.metric_name|test::metric_name&path=fenix&regexp=true"
);
expect(getCodeSearchLink("fenix", "test_category.test_name")).toBe(
expect(
getCodeSearchLink("test_category.test_name", "fenix", {
source_url: "https://foo.bar",
})
).toBe(
"https://searchfox.org/mozilla-mobile/search?q=test_category.test_name|testCategory.testName|TestCategory.testName|test_category.test_name|test_category::test_name&path=fenix&regexp=true"
);
expect(
getCodeSearchLink("test_category.test_name", "fenix", {
source_url:
"https://github.com/mozilla/application-services/foo/bar/metrics.yaml",
})
).toBe(
"https://searchfox.org/mozilla-mobile/search?q=test_category.test_name|testCategory.testName|TestCategory.testName|test_category.test_name|test_category::test_name&path=application-services&regexp=true"
);
expect(
getCodeSearchLink("test_category.test_name", "firefox_ios", {
source_url:
"https://github.com/mozilla/application-services/foo/bar/metrics.yaml",
})
).toBe(
"https://searchfox.org/mozilla-mobile/search?q=test_category.test_name|testCategory.testName|TestCategory.testName|test_category.test_name|test_category::test_name&path=application-services&regexp=true"
);
});
});

describe("returns Sourcegraph link if the application is not indexed on Searchfox", () => {
it("works as expected", () => {
expect(getCodeSearchLink("mozregression", "usage.bad_date")).toBe(
expect(
getCodeSearchLink("usage.bad_date", "mozregression", {
source_url: "https://foo.bar",
})
).toBe(
"https://sourcegraph.com/search?q=repo:%5Egithub%5C.com%5C/%5BMm%5Dozilla%28.*%29%5C/mozregression%24+usage.bad_date|usage.badDate|Usage.badDate|usage.bad_date|usage::bad_date&patternType=regexp"
);
});
});

describe("returns undefined if the application cannot be indexed", () => {
it("works as expected", () => {
expect(getCodeSearchLink("foo", "test.metric")).toBeUndefined();
expect(
getCodeSearchLink("test.metric", "foo", { source_url: "https://foo.bar" })
).toBeUndefined();
});
});