Skip to content

Instantly share code, notes, and snippets.

@hermanbanken
Created September 9, 2021 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermanbanken/c70398c249b3de22ac33d3e88a2a1280 to your computer and use it in GitHub Desktop.
Save hermanbanken/c70398c249b3de22ac33d3e88a2a1280 to your computer and use it in GitHub Desktop.
GitLab list pipeline variables
#!/bin/env node
const PRIVATE_TOKEN = "redacted";
const headers = { "PRIVATE-TOKEN": PRIVATE_TOKEN };
const root = "https://gitlab.com/api/v4";
const project = 24418136; // "hue-contract-tests";
const { default: fetch } = require("node-fetch");
fetch(`${root}/projects/${project}/pipelines`, { headers }).then((res) => res.json())
.then(async (pipelines) => {
for (const p of pipelines) {
const pid = p.id;
const variables = await fetch(`${root}/projects/${project}/pipelines/${pid}/variables`, { headers }).then((res) => res.json());
console.log(pid, getVar(flatVars(variables), "SUITE") || variables.length ? variables : `No variables for ${p.web_url}`);
}
});
function flatVars(vars) {
return vars.reduce((obj, { key, value }) => ({ ...obj, [key]: value }), {});
}
function getVar(vars, name) {
if (!vars[name]) {
return undefined;
}
return vars[name].replace(/\$([^$]+)/g, m => {
const matchingKey = Object.keys(vars).find((v) => m.startsWith(`$${v}`));
if (matchingKey) {
return m.replace("$"+matchingKey, vars[matchingKey]);
}
return "";
})
}
// test getVar:
// console.log(getVar({ SUITE: "$PROJECT_NAME_FOO_BAR$OTHER", PROJECT_NAME: "bla" }, "SUITE"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment