From 4a60504e1aae90620765a825da386637d2c861b3 Mon Sep 17 00:00:00 2001 From: jiriks74 Date: Sun, 15 Dec 2024 19:23:00 +0100 Subject: [PATCH] feat: Handle API URLs for Forgejo, Gitea, GitLab and GitHub --- dist/index.js | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8cbfb72..92791d3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1277,18 +1277,51 @@ 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_FORK_COLLAB = `Fork collab can't be granted by someone without permission`; const blobCreationLimit = (0, p_limit_1.default)(8); +function determineApiBaseUrl(hostname) { + if (hostname === '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 xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + xhr.setRequestHeader('Accept', 'application/json'); + xhr.send(); + + if (xhr.status === 200 || xhr.status === 401 || xhr.status === 403) { + const contentType = xhr.getResponseHeader('Content-Type') || ''; + if (contentType.includes('application/json')) { + return path.includes('/version') ? url.replace('/version', '') : url; + } + } + } catch (error) { + // Ignore errors and try the next path + } + } + + throw new Error(`Unable to determine API base URL for hostname: ${hostname}`); +} + class GitHubHelper { constructor(githubServerHostname, token) { const options = {}; if (token) { options.auth = `${token}`; } - if (githubServerHostname !== 'github.com') { - options.baseUrl = `https://${githubServerHostname}/api/v1`; - } - else { - options.baseUrl = 'https://api.github.com'; + try { + options.baseUrl = await determineApiBaseUrl(githubServerHostname); + core.info(`Valid API URL detected: ${apiBaseUrl}`); + } catch (error) { + console.error(error.message); + process.exit(1); // Exit with an error code if no API path is found } + core.info(`baseUrl: ${options.baseUrl}`); + options.throttle = octokit_client_1.throttleOptions; this.octokit = new octokit_client_1.Octokit(options); } @@ -36020,4 +36053,4 @@ function validateConcurrency(concurrency) { /******/ module.exports = __webpack_exports__; /******/ /******/ })() -; \ No newline at end of file +;