feat: Handle API URLs for Forgejo, Gitea, GitLab and GitHub
This commit is contained in:
parent
9791a4f146
commit
3f96d29f8c
2 changed files with 60 additions and 11 deletions
33
dist/index.js
vendored
33
dist/index.js
vendored
|
@ -1283,15 +1283,36 @@ class GitHubHelper {
|
||||||
if (token) {
|
if (token) {
|
||||||
options.auth = `${token}`;
|
options.auth = `${token}`;
|
||||||
}
|
}
|
||||||
if (githubServerHostname !== 'github.com') {
|
options.baseUrl = this.determineApiPath(githubServerHostname);
|
||||||
options.baseUrl = `https://${githubServerHostname}/api/v1`;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
options.baseUrl = 'https://api.github.com';
|
|
||||||
}
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
determineApiPath(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); // false makes it synchronous
|
||||||
|
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) {
|
||||||
|
// Try the next path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new Error(`Unable to determine API base URL for hostname: ${hostname}`);
|
||||||
|
}
|
||||||
parseRepository(repository) {
|
parseRepository(repository) {
|
||||||
const [owner, repo] = repository.split('/');
|
const [owner, repo] = repository.split('/');
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -46,15 +46,42 @@ export class GitHubHelper {
|
||||||
if (token) {
|
if (token) {
|
||||||
options.auth = `${token}`
|
options.auth = `${token}`
|
||||||
}
|
}
|
||||||
if (githubServerHostname !== 'github.com') {
|
options.baseUrl = this.determineApiPath(githubServerHostname)
|
||||||
options.baseUrl = `https://${githubServerHostname}/api/v3`
|
|
||||||
} else {
|
|
||||||
options.baseUrl = 'https://api.github.com'
|
|
||||||
}
|
|
||||||
options.throttle = throttleOptions
|
options.throttle = throttleOptions
|
||||||
this.octokit = new Octokit(options)
|
this.octokit = new Octokit(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private determineApiPath(hostname: string): 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 xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', url, false); // false makes it synchronous
|
||||||
|
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) {
|
||||||
|
// Try the next path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Unable to determine API base URL for hostname: ${hostname}`)
|
||||||
|
}
|
||||||
|
|
||||||
private parseRepository(repository: string): Repository {
|
private parseRepository(repository: string): Repository {
|
||||||
const [owner, repo] = repository.split('/')
|
const [owner, repo] = repository.split('/')
|
||||||
return {
|
return {
|
||||||
|
@ -403,3 +430,4 @@ export class GitHubHelper {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue