refactor: replace GitHub API with Gitea-compatible git provider service
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import simpleGit, { SimpleGit } from "simple-git";
|
||||
import fs from "fs/promises";
|
||||
import { REPOS, type RepoName } from "../config/repos.js";
|
||||
import { REPOS, GIT_PROVIDER_URL, type RepoName } from "../config/repos.js";
|
||||
import { getRepoPath, readConfig } from "./workspace.service.js";
|
||||
import { broadcast } from "./ws.service.js";
|
||||
|
||||
@@ -111,19 +111,29 @@ export async function getDiff(repoPath: string, file?: string) {
|
||||
return g.diff(["HEAD"]);
|
||||
}
|
||||
|
||||
function getCloneUrl(token: string, user: string, repoName: string, provider: string): string {
|
||||
const host = provider === "github" ? "github.com" : new URL(GIT_PROVIDER_URL).host;
|
||||
return `https://${token}@${host}/${user}/${repoName}.git`;
|
||||
}
|
||||
|
||||
function getUpstreamUrl(owner: string, repo: string, provider: string, token?: string): string {
|
||||
if (provider === "github") {
|
||||
return `https://github.com/${owner}/${repo}.git`;
|
||||
}
|
||||
const host = new URL(GIT_PROVIDER_URL).host;
|
||||
if (token) {
|
||||
return `https://${token}@${host}/${owner}/${repo}.git`;
|
||||
}
|
||||
return `https://${host}/${owner}/${repo}.git`;
|
||||
}
|
||||
|
||||
export async function setupClone(repoName: RepoName) {
|
||||
const config = await readConfig();
|
||||
const pat = config.githubPat;
|
||||
if (!pat) throw new Error("GitHub PAT not configured");
|
||||
if (!pat) throw new Error("Git provider token not configured");
|
||||
|
||||
const repoDef = getRepoDef(repoName);
|
||||
const [upOwner, upRepo] = repoDef.upstream.split("/");
|
||||
|
||||
const { validatePat } = await import("./github.service.js");
|
||||
const { login } = await validatePat(pat);
|
||||
|
||||
const forkUrl = `https://${pat}@github.com/${login}/${upRepo}.git`;
|
||||
const upstreamUrl = `https://github.com/${upOwner}/${upRepo}.git`;
|
||||
const targetDir = getRepoPath(repoName);
|
||||
|
||||
try {
|
||||
@@ -133,11 +143,19 @@ export async function setupClone(repoName: RepoName) {
|
||||
if (err instanceof Error && err.message.startsWith("Repo directory")) throw err;
|
||||
}
|
||||
|
||||
await cloneRepo(forkUrl, targetDir, { branch: repoDef.branch, depth: 1 });
|
||||
|
||||
const g = git(targetDir);
|
||||
await g.addRemote("upstream", upstreamUrl);
|
||||
await g.fetch("upstream");
|
||||
if (repoDef.provider === "github") {
|
||||
const upstreamUrl = getUpstreamUrl(upOwner, upRepo, "github");
|
||||
await cloneRepo(upstreamUrl, targetDir, { branch: repoDef.branch, depth: 1 });
|
||||
} else {
|
||||
const { validateToken } = await import("./git-provider.service.js");
|
||||
const { login } = await validateToken(pat as string);
|
||||
const forkUrl = getCloneUrl(pat as string, login, upRepo, repoDef.provider);
|
||||
const upstreamUrl = getUpstreamUrl(upOwner, upRepo, repoDef.provider, pat as string);
|
||||
await cloneRepo(forkUrl, targetDir, { branch: repoDef.branch, depth: 1 });
|
||||
const g = git(targetDir);
|
||||
await g.addRemote("upstream", upstreamUrl);
|
||||
await g.fetch("upstream");
|
||||
}
|
||||
|
||||
return { path: targetDir, branch: repoDef.branch };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user