This commit is contained in:
Shaoqi-Cen 2025-09-27 13:18:04 -07:00 committed by GitHub
commit eb677fa0cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,6 +4,38 @@ import { env } from './env.js';
type onProgressFn = (event: SimpleGitProgressEvent) => void; type onProgressFn = (event: SimpleGitProgressEvent) => void;
// Helper function to prepare Azure DevOps authentication
const prepareAzureDevOpsAuth = (cloneUrl: string) => {
// Check if this is an Azure DevOps URL
const isAzureDevOps = cloneUrl.includes('dev.azure.com') || cloneUrl.includes('devops');
if (!isAzureDevOps) {
return { url: cloneUrl, authHeader: null };
}
let token = '';
try {
const url = new (globalThis as any).URL(cloneUrl);
token = url.password;
} catch (e) {
return { url: cloneUrl, authHeader: null };
}
if (!token) {
return { url: cloneUrl, authHeader: null };
}
// For Azure DevOps, we need to remove the token from the URL and pass it as a header
const url = new (globalThis as any).URL(cloneUrl);
url.password = '';
const cleanUrl = url.toString();
// Create the authorization header
const authHeader = `Authorization: Basic ${(globalThis as any).Buffer.from(`:${token}`).toString('base64')}`;
return { url: cleanUrl, authHeader };
};
export const cloneRepository = async ( export const cloneRepository = async (
{ {
cloneUrl, cloneUrl,
@ -18,20 +50,34 @@ export const cloneRepository = async (
try { try {
await mkdir(path, { recursive: true }); await mkdir(path, { recursive: true });
const git = simpleGit({ const git = simpleGit({
progress: onProgress, progress: onProgress,
}).cwd({ }).cwd({
path, path,
}) })
await git.clone( const { url: cleanUrl, authHeader } = prepareAzureDevOpsAuth(cloneUrl);
cloneUrl,
path,
[
"--bare",
]
);
if (authHeader) {
await git.clone(
cleanUrl,
path,
[
"--bare",
"-c",
`http.extraHeader=${authHeader}`
]
);
} else {
await git.clone(
cloneUrl,
path,
[
"--bare",
]
);
}
await unsetGitConfig(path, ["remote.origin.url"]); await unsetGitConfig(path, ["remote.origin.url"]);
} catch (error: unknown) { } catch (error: unknown) {
const baseLog = `Failed to clone repository: ${path}`; const baseLog = `Failed to clone repository: ${path}`;
@ -65,12 +111,26 @@ export const fetchRepository = async (
path: path, path: path,
}) })
await git.fetch([ const { url: cleanUrl, authHeader } = prepareAzureDevOpsAuth(cloneUrl);
cloneUrl,
"+refs/heads/*:refs/heads/*", if (authHeader) {
"--prune", // Temporarily set git configuration
"--progress" await git.addConfig('http.extraHeader', authHeader);
]);
await git.fetch([
cleanUrl,
"+refs/heads/*:refs/heads/*",
"--prune",
"--progress"
]);
} else {
await git.fetch([
cloneUrl,
"+refs/heads/*:refs/heads/*",
"--prune",
"--progress"
]);
}
} catch (error: unknown) { } catch (error: unknown) {
const baseLog = `Failed to fetch repository: ${path}`; const baseLog = `Failed to fetch repository: ${path}`;
if (env.SOURCEBOT_LOG_LEVEL !== "debug") { if (env.SOURCEBOT_LOG_LEVEL !== "debug") {