Skip to content
Snippets Groups Projects

feat: add support for private repository fetches via build env var

Merged Martin Lowe requested to merge malowe/main/10 into main
1 unresolved thread
Files
3
+ 10
3
@@ -24,21 +24,27 @@ const URL_REGEX = new RegExp('^https?://(?:[^./]+.){1,3}.[a-z]{2,3}(/[^/]+)*.[a-
const TEMP_DIR_NAME = 'tmp';
// the const to hold the generated spec data to be output for Hugo builds
const artificialSpecData = { items: [] };
const PRIVATE_TOKEN = process.env.GITLAB_API_TOKEN;
/**
* Pull an external/remote spec using fetch and write it to the file system for further processing.
*
* @param {*} externalLocation the URL for the raw openapi spec file.
* @param {*} localPath the location where the downloaded spec should be saved
Please register or sign in to reply
* @param {boolean} isPrivate optional, whether an external spec is in a private GitLab repository and should use secret token for access
*/
async function pullExternalSpec(externalLocation, localPath) {
async function pullExternalSpec(externalLocation, localPath, isPrivate = false) {
let retries = 0;
// retry 3 times before giving up
while (retries < 3) {
try {
fs.writeFileSync(
localPath,
await fetch(externalLocation).then(response => {
await fetch(externalLocation, {
headers: {
"PRIVATE-TOKEN": isPrivate ? PRIVATE_TOKEN : ''
}
}).then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
@@ -113,6 +119,7 @@ async function run() {
const specMachineName = currentSpecDefinition['name'];
const specDisplayName = currentSpecDefinition['displayName'] || currentSpecDefinition['name'];
const specLocation = currentSpecDefinition['location'];
const isSpecPrivate = currentSpecDefinition['isPrivate'];
const potentialLocalSpecLocation = `${initialProjectDirectory}${path.sep}${specLocation}`;
const outputSpecLocation = `${specMachineName}${path.sep}openapi.yaml`;
@@ -122,7 +129,7 @@ async function run() {
console.log(`Retrieving openapi spec for ${specDisplayName}...`);
// prepare the spec, fetching remote specs or copying the spec for processing
if (isExternalSpec(specLocation)) {
await pullExternalSpec(specLocation, outputSpecLocation);
await pullExternalSpec(specLocation, outputSpecLocation, isSpecPrivate);
} else {
fs.copyFileSync(potentialLocalSpecLocation, outputSpecLocation);
console.log(`Copied local spec for ${specDisplayName} to output dir`);
Loading