refactor: js output
This commit is contained in:
parent
1aa28c2a34
commit
c6704c3a04
96 changed files with 3816 additions and 147 deletions
70
src/bugzilla/origin.js
Normal file
70
src/bugzilla/origin.js
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// Origin URL — builds a resolvable URL pointing to a TODO issue in the repo's web UI
|
||||
// Format varies by host:
|
||||
// Bitbucket: <base>/src/<branch>/<path>#<id>
|
||||
// GitHub: <base>/blob/<branch>/<path>#<id>
|
||||
// GitLab: <base>/-/blob/<branch>/<path>#<id>
|
||||
import { execSync } from "child_process";
|
||||
export function getGitRemoteUrl(cwd = process.cwd()) {
|
||||
try {
|
||||
return execSync("git remote get-url origin", { cwd, encoding: "utf-8" }).trim();
|
||||
}
|
||||
catch {
|
||||
throw new Error("Could not determine git remote URL. Is this a git repository with an 'origin' remote?");
|
||||
}
|
||||
}
|
||||
export function getGitBranch(cwd = process.cwd()) {
|
||||
try {
|
||||
return execSync("git rev-parse --abbrev-ref HEAD", { cwd, encoding: "utf-8" }).trim();
|
||||
}
|
||||
catch {
|
||||
return "main";
|
||||
}
|
||||
}
|
||||
export function normalizeRemoteUrl(url) {
|
||||
let normalized = url;
|
||||
const sshMatch = normalized.match(/^git@([^:]+):(.+?)(?:\.git)?$/);
|
||||
if (sshMatch) {
|
||||
normalized = `https://${sshMatch[1]}/${sshMatch[2]}`;
|
||||
}
|
||||
normalized = normalized.replace(/\.git$/, "");
|
||||
normalized = normalized.replace(/\/+$/, "");
|
||||
return normalized;
|
||||
}
|
||||
function detectHost(url) {
|
||||
if (url.includes("bitbucket.org"))
|
||||
return "bitbucket";
|
||||
if (url.includes("github.com"))
|
||||
return "github";
|
||||
if (url.includes("gitlab.com") || url.includes("gitlab"))
|
||||
return "gitlab";
|
||||
return "unknown";
|
||||
}
|
||||
export function buildOriginUrl(remoteUrl, branch, todoPath, issueId) {
|
||||
const base = normalizeRemoteUrl(remoteUrl);
|
||||
const host = detectHost(base);
|
||||
switch (host) {
|
||||
case "github":
|
||||
return `${base}/blob/${branch}/${todoPath}#${issueId}`;
|
||||
case "gitlab":
|
||||
return `${base}/-/blob/${branch}/${todoPath}#${issueId}`;
|
||||
case "bitbucket":
|
||||
default:
|
||||
return `${base}/src/${branch}/${todoPath}#${issueId}`;
|
||||
}
|
||||
}
|
||||
export function buildCommitUrl(remoteUrl, commitHash) {
|
||||
const base = normalizeRemoteUrl(remoteUrl);
|
||||
const host = detectHost(base);
|
||||
switch (host) {
|
||||
case "github":
|
||||
return `${base}/commit/${commitHash}`;
|
||||
case "gitlab":
|
||||
return `${base}/-/commit/${commitHash}`;
|
||||
case "bitbucket":
|
||||
default:
|
||||
return `${base}/commits/${commitHash}`;
|
||||
}
|
||||
}
|
||||
export function shortHash(hash) {
|
||||
return hash.slice(0, 7);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue