feat: Handle API URLs for Forgejo, Gitea, GitLab and GitHub
This commit is contained in:
parent
9791a4f146
commit
8d7b90472d
1 changed files with 36 additions and 6 deletions
42
dist/index.js
vendored
42
dist/index.js
vendored
|
@ -1277,17 +1277,47 @@ const ERROR_PR_ALREADY_EXISTS = 'A pull request already exists for';
|
||||||
const ERROR_PR_REVIEW_TOKEN_SCOPE = 'Validation Failed: "Could not resolve to a node with the global id of';
|
const ERROR_PR_REVIEW_TOKEN_SCOPE = 'Validation Failed: "Could not resolve to a node with the global id of';
|
||||||
const ERROR_PR_FORK_COLLAB = `Fork collab can't be granted by someone without permission`;
|
const ERROR_PR_FORK_COLLAB = `Fork collab can't be granted by someone without permission`;
|
||||||
const blobCreationLimit = (0, p_limit_1.default)(8);
|
const blobCreationLimit = (0, p_limit_1.default)(8);
|
||||||
|
async function determineApiBaseUrl(hostname) {
|
||||||
|
if ( hostname == 'github.com' ){
|
||||||
|
console.log(`Valid API path detected: https://api.github.com`);
|
||||||
|
return "https://api.github.com";
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseUrl = `https://${hostname}`;
|
||||||
|
const possiblePaths = ['/api/v4/version', '/api/forgejo/v1/version', '/api/v1/version' ];
|
||||||
|
|
||||||
|
for (const path of possiblePaths) {
|
||||||
|
try {
|
||||||
|
const url = `${baseUrl}${path}`;
|
||||||
|
const response = await fetch(url, { method: 'GET', redirect: 'manual' }); // GitLab redirects `/api/forgejo/v1/version to
|
||||||
|
// login prompt so we get response 200
|
||||||
|
// if we allow the redirect. Now we get 302.
|
||||||
|
|
||||||
|
const contentType = response.headers.get('Content-Type') || '';
|
||||||
|
if (
|
||||||
|
(response.ok || [401, 403].includes(response.status)) && // Check for valid API status codes
|
||||||
|
contentType.includes('application/json') // Ensure it's returning JSON
|
||||||
|
) {
|
||||||
|
console.log(`Valid API path detected: ${path.includes('/version') ? url.replace('/version', '') : url}`);
|
||||||
|
return path.includes('/version') ? url.replace('/version', '') : url;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unable to determine API base URL for hostname: ${hostname}`);
|
||||||
|
}
|
||||||
class GitHubHelper {
|
class GitHubHelper {
|
||||||
constructor(githubServerHostname, token) {
|
constructor(githubServerHostname, token) {
|
||||||
const options = {};
|
const options = {};
|
||||||
if (token) {
|
if (token) {
|
||||||
options.auth = `${token}`;
|
options.auth = `${token}`;
|
||||||
}
|
}
|
||||||
if (githubServerHostname !== 'github.com') {
|
try {
|
||||||
options.baseUrl = `https://${githubServerHostname}/api/v1`;
|
options.baseUrl = determineApiBaseUrl(githubServerHostname);
|
||||||
}
|
} catch (error) {
|
||||||
else {
|
console.error(error.message);
|
||||||
options.baseUrl = 'https://api.github.com';
|
process.exit(1); // Exit with an error code if no API path is found
|
||||||
}
|
}
|
||||||
options.throttle = octokit_client_1.throttleOptions;
|
options.throttle = octokit_client_1.throttleOptions;
|
||||||
this.octokit = new octokit_client_1.Octokit(options);
|
this.octokit = new octokit_client_1.Octokit(options);
|
||||||
|
@ -36020,4 +36050,4 @@ function validateConcurrency(concurrency) {
|
||||||
/******/ module.exports = __webpack_exports__;
|
/******/ module.exports = __webpack_exports__;
|
||||||
/******/
|
/******/
|
||||||
/******/ })()
|
/******/ })()
|
||||||
;
|
;
|
||||||
|
|
Loading…
Reference in a new issue