Skip to content
Snippets Groups Projects
Commit 60c67c5f authored by Martin Lowe's avatar Martin Lowe :flag_ca:
Browse files

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

Resolves #10
parent 40e49f4e
No related branches found
No related tags found
1 merge request!13feat: add support for private repository fetches via build env var
Pipeline #73897 failed
......@@ -50,3 +50,7 @@ specs:
- name: "eclipsefdn-info-api"
displayName: Info API
location: "https://gitlab.eclipse.org/eclipsefdn/it/api/eclipsefdn-info-api/-/raw/main/spec/openapi.yaml"
- name: eclipse-projects-api
displayName: Projects API
location: "https://gitlab.eclipse.org/api/v4/projects/1641/repository/files/spec%2Fopenapi%2Eyaml/raw"
isPrivate: true
......@@ -24,6 +24,7 @@ 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.
......@@ -31,14 +32,18 @@ const artificialSpecData = { items: [] };
* @param {*} externalLocation the URL for the raw openapi spec file.
* @param {*} localPath the location where the downloaded spec should be saved
*/
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 +118,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 +128,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`);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment