Skip to content

Instantly share code, notes, and snippets.

@kijart
Created May 4, 2020 13:04
Show Gist options
  • Save kijart/5b016ad671a9bafb214b9de6ab356741 to your computer and use it in GitHub Desktop.
Save kijart/5b016ad671a9bafb214b9de6ab356741 to your computer and use it in GitHub Desktop.
A small script to download a file of variables from GitLab CI (use this code on a bookmark URL)
javascript: (() => {
const download = (filename, data) => {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(data));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
const getRepositoryName = () => {
const regex = /\/([a-zA-Z0-9-_./]+\/[a-zA-Z0-9-_.]+)\/-\/settings\/ci_cd$/g;
const regexMatch = regex.exec(window.location.pathname);
if (!regexMatch) {
alert('Wrong site, use this script in a URL like: https://gitlab.com/path/to/repository/-/settings/ci_cd');
return;
}
return regexMatch[1];
};
const getData = () => {
let data = '';
document.querySelectorAll('.ci-variable-list .ci-variable-row:not(.m-0)').forEach(row => {
let key = row.querySelector('.js-ci-variable-input-key').value;
let value = row.querySelector('.js-ci-variable-input-value').value;
let scope = row.querySelector('.js-variable-environment-toggle span').textContent;
if (key !== '') {
data += `# ${scope}\n`;
data += `${key}=${value}\n`;
}
});
data += `\n`;
return data;
};
repositoryName = getRepositoryName();
filename = `${repositoryName.replace('/', '_')}.env`;
data = getData();
download(filename,data);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment