61 lines
2.1 KiB
JavaScript
61 lines
2.1 KiB
JavaScript
import { pathToFileURL } from 'node:url';
|
|
|
|
const endpoint = process.env.RENOVATE_ENDPOINT;
|
|
const token = process.env.RENOVATE_TOKEN;
|
|
const repository = 'arnaud.dartois/Heimdall';
|
|
|
|
export function validateEndpoint(value) {
|
|
let parsed;
|
|
try {
|
|
parsed = new URL(value);
|
|
} catch {
|
|
throw new Error('GITEA_API_ENDPOINT must be a valid HTTPS URL ending in /api/v1.');
|
|
}
|
|
|
|
if (parsed.protocol !== 'https:' || parsed.username || parsed.password ||
|
|
parsed.search || parsed.hash || !parsed.pathname.replace(/\/$/, '').endsWith('/api/v1')) {
|
|
throw new Error('GITEA_API_ENDPOINT must be a valid HTTPS URL ending in /api/v1.');
|
|
}
|
|
|
|
return parsed.href.replace(/\/$/, '');
|
|
}
|
|
|
|
export async function checkGiteaAccess(apiEndpoint, accessToken, fetchImpl = fetch) {
|
|
const root = `${validateEndpoint(apiEndpoint)}/`;
|
|
const urls = [new URL('user', root), new URL(`repos/${repository}`, root)];
|
|
|
|
for (const url of urls) {
|
|
let response;
|
|
try {
|
|
response = await fetchImpl(url, {
|
|
headers: { Authorization: `token ${accessToken}`, Accept: 'application/json' },
|
|
signal: AbortSignal.timeout(10_000),
|
|
});
|
|
} catch (error) {
|
|
throw new Error(`Gitea API preflight could not connect (${error?.name ?? 'network error'}).`);
|
|
}
|
|
|
|
if (!response.ok) {
|
|
const detail = response.status === 401 || response.status === 403
|
|
? 'Check that RENOVATE_TOKEN is active and has repository read and write access.'
|
|
: 'Check GITEA_API_ENDPOINT and the Gitea API availability.';
|
|
throw new Error(`Gitea API preflight failed with HTTP ${response.status}. ${detail}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
if (!token) {
|
|
console.error('RENOVATE_TOKEN is missing. Configure it as a Gitea Actions secret.');
|
|
process.exitCode = 1;
|
|
} else {
|
|
try {
|
|
await checkGiteaAccess(endpoint, token);
|
|
console.log('Renovate token authenticated with Gitea and can read the target repository.');
|
|
} catch (error) {
|
|
console.error(error.message);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|
|
}
|