Cleaned up a duplicate export of `REPO_OVERRIDE_RE` from the core package index, removing the redundant declaration from `packages/core/src/index.ts` while keeping it defined in `github-tracking.ts` where it logically belongs. Fusion-Task-Id: FN-3929
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
import { REPO_OVERRIDE_RE, type GlobalSettings, type ProjectSettings, type Task } from "./types.js";
|
|
|
|
export { REPO_OVERRIDE_RE };
|
|
|
|
export interface RepoSlug {
|
|
owner: string;
|
|
repo: string;
|
|
}
|
|
|
|
export interface ResolvedTaskGithubTracking {
|
|
enabled: boolean;
|
|
repo: RepoSlug | null;
|
|
source: {
|
|
enabled: "task" | "project" | "global" | "default";
|
|
repo: "task" | "project" | "global" | "none";
|
|
};
|
|
}
|
|
|
|
function parseRepoSlugCandidate(input: unknown): RepoSlug | null {
|
|
if (typeof input !== "string") return null;
|
|
const trimmed = input.trim();
|
|
if (!trimmed || !REPO_OVERRIDE_RE.test(trimmed)) return null;
|
|
|
|
const [owner, repo] = trimmed.split("/");
|
|
return { owner, repo };
|
|
}
|
|
|
|
export function parseRepoSlug(input: string | undefined | null): RepoSlug | null {
|
|
return parseRepoSlugCandidate(input);
|
|
}
|
|
|
|
export function isValidRepoSlug(input: string): boolean {
|
|
return REPO_OVERRIDE_RE.test(input.trim());
|
|
}
|
|
|
|
export function resolveTaskGithubTracking(
|
|
task: Pick<Task, "githubTracking">,
|
|
projectSettings?: Pick<ProjectSettings, "githubTrackingEnabledByDefault" | "githubTrackingDefaultRepo"> & {
|
|
githubTrackingDefaultEnabledForNewTasks?: boolean;
|
|
githubDefaultRepo?: string;
|
|
},
|
|
globalSettings?: Pick<GlobalSettings, "githubTrackingDefaultRepo"> & {
|
|
githubTrackingDefaultEnabledForNewTasks?: boolean;
|
|
githubDefaultRepo?: string;
|
|
},
|
|
): ResolvedTaskGithubTracking {
|
|
const taskEnabled = task.githubTracking?.enabled;
|
|
const projectEnabled = projectSettings?.githubTrackingEnabledByDefault
|
|
?? projectSettings?.githubTrackingDefaultEnabledForNewTasks;
|
|
// TODO(FN-3868): remove legacy fallback keys once all callers are migrated.
|
|
const globalEnabled = globalSettings?.githubTrackingDefaultEnabledForNewTasks;
|
|
|
|
let enabled = false;
|
|
let enabledSource: ResolvedTaskGithubTracking["source"]["enabled"] = "default";
|
|
|
|
if (typeof taskEnabled === "boolean") {
|
|
enabled = taskEnabled;
|
|
enabledSource = "task";
|
|
} else if (typeof projectEnabled === "boolean") {
|
|
enabled = projectEnabled;
|
|
enabledSource = "project";
|
|
} else if (typeof globalEnabled === "boolean") {
|
|
enabled = globalEnabled;
|
|
enabledSource = "global";
|
|
} else {
|
|
enabled = false;
|
|
enabledSource = "default";
|
|
}
|
|
|
|
const taskRepo = parseRepoSlugCandidate(task.githubTracking?.repoOverride);
|
|
const projectRepo = parseRepoSlugCandidate(projectSettings?.githubTrackingDefaultRepo ?? projectSettings?.githubDefaultRepo);
|
|
const globalRepo = parseRepoSlugCandidate(globalSettings?.githubTrackingDefaultRepo ?? globalSettings?.githubDefaultRepo);
|
|
|
|
let repo: RepoSlug | null = null;
|
|
let repoSource: ResolvedTaskGithubTracking["source"]["repo"] = "none";
|
|
|
|
if (taskRepo) {
|
|
repo = taskRepo;
|
|
repoSource = "task";
|
|
} else if (projectRepo) {
|
|
repo = projectRepo;
|
|
repoSource = "project";
|
|
} else if (globalRepo) {
|
|
repo = globalRepo;
|
|
repoSource = "global";
|
|
}
|
|
|
|
return {
|
|
enabled,
|
|
repo,
|
|
source: {
|
|
enabled: enabledSource,
|
|
repo: repoSource,
|
|
},
|
|
};
|
|
}
|