feat: Handle API URLs for Forgejo, Gitea, GitLab and GitHub

This commit is contained in:
Jiří Štefka 2024-12-15 19:23:00 +01:00
parent 9791a4f146
commit a752ad7b66
Signed by: jiriks74
GPG key ID: 1D5E30D3DB2264DE

55
dist/index.js vendored
View file

@ -405,8 +405,8 @@ function createPullRequest(inputs) {
core.startGroup('Determining the base and head repositories');
const baseRemote = gitConfigHelper.getGitRemote();
// Init the GitHub clients
const ghBranch = new github_helper_1.GitHubHelper(baseRemote.hostname, inputs.branchToken);
const ghPull = new github_helper_1.GitHubHelper(baseRemote.hostname, inputs.token);
const ghBranch = GitHubHelper.init(baseRemote.hostname, inputs.branchToken);
const ghPull = GitHubHelper.init(baseRemote.hostname, inputs.token);
// Determine the head repository; the target for the pull request branch
const branchRemoteName = inputs.pushToFork ? 'fork' : 'origin';
const branchRepository = inputs.pushToFork
@ -1277,18 +1277,57 @@ 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);
async function determineApiBaseUrl(hostname) {
const getApiUrl = async (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 response = await fetch(url, { method: 'GET', redirect: 'manual' });
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
) {
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}`);
};
// Synchronous wrapper for async function
const promise = getApiUrl(hostname);
const result = require('deasync')(promise.then.bind(promise));
return result;
}
class GitHubHelper {
constructor(githubServerHostname, token) {
}
async init(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 +36059,4 @@ function validateConcurrency(concurrency) {
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
;
;