UNPKG

@secustor/backstage-plugin-renovate-common

Version:

Common functionalities for the renovate plugin

77 lines (74 loc) 1.81 kB
import { ANNOTATION_SOURCE_LOCATION, parseEntityRef } from '@backstage/catalog-model'; import gitUrlParse from 'git-url-parse'; import is from '@sindresorhus/is'; import { targetRepo } from './schema.esm.js'; function getTaskID(target) { const repo = getTargetRepo(target); return `renovate_run_${repo.host}_${repo.repository}`; } function getTargetRepo(target) { if (isTargetRepo(target)) { return target; } const url = getTargetURL(target); return { host: url.resource, repository: url.full_name }; } function getTargetRepoSafe(target) { try { return getTargetRepo(target); } catch (e) { return null; } } function parseUrl(url) { if (!url) { return null; } try { return new URL(url); } catch (err) { return null; } } function parseGitUrl(url) { if (!url) { return null; } try { return gitUrlParse(url); } catch (err) { return null; } } function getTargetURL(target) { let rawTargetUrl = is.string(target) ? target : target?.metadata.annotations?.[ANNOTATION_SOURCE_LOCATION]; if (rawTargetUrl?.startsWith("url:")) { rawTargetUrl = rawTargetUrl.replace("url:", ""); } if (!rawTargetUrl?.includes("://")) { rawTargetUrl = `https://${rawTargetUrl}`; } const targetUrl = parseGitUrl(rawTargetUrl); if (is.nullOrUndefined(targetUrl)) { throw new Error( `Could not identify platform url via ${JSON.stringify(target)}` ); } return targetUrl; } function isTargetRepo(value) { return targetRepo.safeParse(value).success; } function isEntityRef(ref) { try { parseEntityRef(ref); return true; } catch (e) { return false; } } export { getTargetRepo, getTargetRepoSafe, getTargetURL, getTaskID, isEntityRef, parseGitUrl, parseUrl }; //# sourceMappingURL=utils.esm.js.map