Skip to content

Commit 9dbf9ce

Browse files
committed
Try GitHub API first
1 parent 51abd73 commit 9dbf9ce

File tree

2 files changed

+171
-31
lines changed

2 files changed

+171
-31
lines changed

public/scripts/workflow.js

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function parseRepositoryURL(repoURL) {
3333
// Enterprise: http(s)://github.com/enterprises/enterprise/owner/repo
3434
return {
3535
origin: url.origin,
36+
hostname: url.hostname,
3637
enterprise: parts[1],
3738
owner: parts[2],
3839
repo: parts[3],
@@ -41,6 +42,7 @@ function parseRepositoryURL(repoURL) {
4142
// Standard: http(s)://host/owner/repo
4243
return {
4344
origin: url.origin,
45+
hostname: url.hostname,
4446
owner: parts[0],
4547
repo: parts[1],
4648
};
@@ -51,36 +53,81 @@ function parseRepositoryURL(repoURL) {
5153
return null;
5254
}
5355

54-
async function detectDefaultBranch(repoInfo, timeoutMs = 3000) {
56+
async function detectDefaultBranch(repoInfo) {
5557
if (!repoInfo) return null;
5658

57-
let baseUrl = repoInfo.origin;
58-
if (repoInfo.enterprise) {
59-
baseUrl += `/enterprises/${repoInfo.enterprise}`;
59+
async function fetchWithTimeout(url, options = {}) {
60+
const controller = new AbortController();
61+
const timeout = setTimeout(() => controller.abort(), 3000); // 3 seconds timeout
62+
try {
63+
const resp = await fetch(url, {
64+
...options,
65+
signal: controller.signal,
66+
});
67+
return resp;
68+
} finally {
69+
clearTimeout(timeout);
70+
}
6071
}
61-
baseUrl += `/${repoInfo.owner}/${repoInfo.repo}`;
6272

63-
const branches = ["main", "master", "develop"];
73+
// Try to detect default branch using the GitHub API
74+
async function tryApi() {
75+
let apiUrl;
76+
if (repoInfo.hostname.replace(/^www\./, "") === "github.com") {
77+
apiUrl = `https://api.github.com/repos/${repoInfo.owner}/${repoInfo.repo}`;
78+
} else {
79+
apiUrl = `${origin}/api/v3/repos/${repoInfo.owner}/${repoInfo.repo}`;
80+
}
81+
82+
try {
83+
const resp = await fetchWithTimeout(apiUrl, {
84+
headers: { Accept: "application/vnd.github+json" },
85+
});
6486

65-
function fetchWithTimeout(url, timeoutMs) {
66-
const controller = new AbortController();
67-
const timeout = setTimeout(() => controller.abort(), timeoutMs);
68-
return fetch(url, {
69-
method: "HEAD",
70-
signal: controller.signal,
71-
redirect: "manual",
72-
}).finally(() => clearTimeout(timeout));
87+
if (!resp.ok) return null;
88+
89+
const data = await resp.json();
90+
return data.default_branch || null;
91+
} catch {
92+
// Network or abort error, fallback
93+
return null;
94+
}
7395
}
7496

75-
const checks = branches.map((branch) => {
76-
const webUrl = `${baseUrl}/blob/${branch}/README.md`;
77-
return fetchWithTimeout(webUrl, timeoutMs)
78-
.then((resp) => (resp && resp.ok ? branch : null))
79-
.catch(() => null);
80-
});
97+
// Fallback: Probe common branch names by checking for a README.md
98+
async function tryBlobFallback() {
99+
let repoBaseUrl;
100+
if (repoInfo.hostname.replace(/^www\./, "") === "github.com") {
101+
repoBaseUrl = "https://github.com";
102+
} else {
103+
repoBaseUrl = `${repoInfo.origin}`;
104+
}
105+
if (repoInfo.enterprise) {
106+
repoBaseUrl += `/enterprises/${repoInfo.enterprise}`;
107+
}
108+
repoBaseUrl += `/${repoInfo.owner}/${repoInfo.repo}`;
109+
110+
const branches = ["main", "master", "develop"];
111+
const checks = branches.map(async (branch) => {
112+
const url = `${repoBaseUrl}/blob/${branch}/README.md`;
113+
try {
114+
const resp = await fetchWithTimeout(url, {
115+
method: "HEAD",
116+
redirect: "manual",
117+
});
118+
return resp.ok ? branch : null;
119+
} catch {
120+
return null;
121+
}
122+
});
123+
const results = await Promise.all(checks);
124+
return results.find((branch) => branch !== null) || null;
125+
}
81126

82-
const results = await Promise.all(checks);
83-
return results.find((branch) => branch !== null) || null;
127+
// Main logic: Try API first, then fallback if needed
128+
const apiBranch = await tryApi();
129+
if (apiBranch) return apiBranch;
130+
return await tryBlobFallback();
84131
}
85132

86133
function generateWorkflow({

0 commit comments

Comments
 (0)