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 1b757dbf52
Signed by: jiriks74
GPG key ID: 1D5E30D3DB2264DE
2 changed files with 39 additions and 8 deletions

View file

@ -51,8 +51,10 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
core.startGroup('Determining the base and head repositories')
const baseRemote = gitConfigHelper.getGitRemote()
// Init the GitHub clients
const ghBranch = new GitHubHelper(baseRemote.hostname, inputs.branchToken)
const ghPull = new GitHubHelper(baseRemote.hostname, inputs.token)
const apiUrl = await GitHubHelper.determineApiUrl(baseRemote.hostname);
core.info(`Using API base URL: ${apiUrl}`);
const ghBranch = new GitHubHelper(apiUrl, inputs.branchToken)
const ghPull = new GitHubHelper(apiUrl, inputs.token)
// Determine the head repository; the target for the pull request branch
const branchRemoteName = inputs.pushToFork ? 'fork' : 'origin'
const branchRepository = inputs.pushToFork

View file

@ -41,20 +41,49 @@ type TreeObject = {
export class GitHubHelper {
private octokit: InstanceType<typeof Octokit>
constructor(githubServerHostname: string, token: string) {
constructor(apiUrl: string, token: string) {
const options: OctokitOptions = {}
if (token) {
options.auth = `${token}`
}
if (githubServerHostname !== 'github.com') {
options.baseUrl = `https://${githubServerHostname}/api/v3`
} else {
options.baseUrl = 'https://api.github.com'
}
options.baseUrl = apiUrl;
options.throttle = throttleOptions
this.octokit = new Octokit(options)
}
static async determineApiUrl(hostname: string): Promise<string> {
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' }); // GitLab redirects
// invalid API paths
// to login prompt
// which returns 200
const contentType = response.headers.get('Content-Type') || '';
if (
(response.ok || [401, 403].includes(response.status)) && // We might get 401, 403
// as we're unauthorised
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}`);
}
private parseRepository(repository: string): Repository {
const [owner, repo] = repository.split('/')
return {