/**
* Repository to GitHub branch mapping utilities
*/
import { REPO_GITHUB_MAPPING } from "../constants.js";
import type { RepoMapping } from "../types.js";
/**
* Get GitHub repository and branch for a given Searchfox repo name
*/
export function getGitHubMapping(searchfoxRepo: string): RepoMapping {
const mapping = REPO_GITHUB_MAPPING[searchfoxRepo];
if (!mapping) {
// Default to mozilla-central if unknown repo
return {
github_repo: REPO_GITHUB_MAPPING["mozilla-central"].repo,
branch: REPO_GITHUB_MAPPING["mozilla-central"].branch,
};
}
return {
github_repo: mapping.repo,
branch: mapping.branch,
};
}
/**
* Get GitHub raw URL for a file
*/
export function getGitHubRawUrl(
searchfoxRepo: string,
filePath: string
): string {
const mapping = getGitHubMapping(searchfoxRepo);
return `https://raw.githubusercontent.com/${mapping.github_repo}/${mapping.branch}/${filePath}`;
}
/**
* Get Searchfox URL for a file
*/
export function getSearchfoxFileUrl(
repo: string,
filePath: string,
line?: number
): string {
const baseUrl = `https://searchfox.org/${repo}/source/${filePath}`;
return line ? `${baseUrl}#${line}` : baseUrl;
}
/**
* Get Searchfox search URL
*/
export function getSearchfoxSearchUrl(
repo: string,
query: string,
options?: {
case?: boolean;
regexp?: boolean;
path?: string;
}
): string {
const params = new URLSearchParams({
q: query,
});
if (options?.case) {
params.append("case", "true");
}
if (options?.regexp) {
params.append("regexp", "true");
}
if (options?.path) {
params.append("path", options.path);
}
return `https://searchfox.org/${repo}/search?${params}`;
}