Skip to content

Instantly share code, notes, and snippets.

@loopmode
Last active November 11, 2020 20:06
Show Gist options
  • Save loopmode/7c85aa735a7f71c8d5010d4d5602a1d9 to your computer and use it in GitHub Desktop.
Save loopmode/7c85aa735a7f71c8d5010d4d5602a1d9 to your computer and use it in GitHub Desktop.
Bookmarklet: Gitlab Secrets

gitlab secrets bookmarklets

Helper bookmarklets to display all keys and values of a gitlab secrets page in a single element. This way, you can copy them all in one go.

gitlab_secrets_bookmarklets

Why? Because otherwise, you'd have to select, copy and paste each key and value manually, which is tedious.

Not a common use case, but I needed this several times, e.g. when requested by project managers, or when you need to replicate deployments or builds on a different system or locally.

Installation

Create new bookmark, give it some title like "Gitlab Secrets", but instead of a URL, use the javascript:(...) code found below

You can also just select the code below and drag it to your browser's bookmarks bar:

installation

Usage

On a gitlab secrets page, simply click the bookmark.

gitlab 13.4.3

Display as line-separated text:

javascript:(async function showGitlabSecrets(){const e=e=>new Promise(t=>setTimeout(t,e));if(!document.querySelector("#js-cicd-variables-settings").classList.contains("expanded")){document.querySelector("#js-cicd-variables-settings .js-settings-toggle").click(),await e(350)}if(!(!1===document.querySelector("tr.js-ci-variable-row td:nth-child(3)").innerText.startsWith("***"))){document.querySelector('[data-qa-selector="reveal_ci_variable_value_button"]').click(),await e(350)}const t=[...document.querySelectorAll(".js-ci-variable-row")].reduce((e,t)=>{const c=t.querySelector("td:nth-child(2)").innerText,n=t.querySelector("td:nth-child(3)").innerText;return c&&e.push(`${c}=${n}`),e},[]),c=document.createElement("pre");c.innerHTML=t.join("\n"),document.getElementById("content-body").prepend(c),window.scrollTo(0,0)}())

Display as formatted json:

javascript:(async function showGitlabSecretsJSON(){const e=e=>new Promise(t=>setTimeout(t,e));if(!document.querySelector("#js-cicd-variables-settings").classList.contains("expanded")){document.querySelector("#js-cicd-variables-settings .js-settings-toggle").click(),await e(350)}if(!(!1===document.querySelector("tr.js-ci-variable-row td:nth-child(3)").innerText.startsWith("***"))){document.querySelector('[data-qa-selector="reveal_ci_variable_value_button"]').click(),await e(350)}const t=[...document.querySelectorAll(".js-ci-variable-row")].reduce((e,t)=>{const c=t.querySelector("td:nth-child(2)").innerText,n=t.querySelector("td:nth-child(3)").innerText;return c&&Object.assign(e,{[c]:n}),e},{}),c=document.createElement("pre");c.innerHTML=JSON.stringify(t,null,2),document.getElementById("content-body").prepend(c),window.scrollTo(0,0)}())

Other gitlab versions

In other gitlab versions, the selectors might need to be adjusted, but the concept is the same. The scripts are below and you change selectors as needed. Afterwards, minify the script so it's a one-liner and wrap it in javacript:() so it runs as a bookmarklet.

// this is the unwrapped code of the bookmarks, before it is manually minified via https://javascript-minifier.com/
async function showGitlabSecrets() {
const wait = (delay) => new Promise(resolve => setTimeout(resolve, delay));
/* ensure the secrets table is expanded */
const isExpanded = document.querySelector('#js-cicd-variables-settings').classList.contains('expanded');
if (!isExpanded) {
const btnExpand = document.querySelector('#js-cicd-variables-settings .js-settings-toggle');
btnExpand.click();
await wait(350);
}
/* ensure secrets are revealed */
const isRevealed = document.querySelector('tr.js-ci-variable-row td:nth-child(3)').innerText.startsWith('***') === false;
if (!isRevealed) {
const btnReveal = document.querySelector('[data-qa-selector="reveal_ci_variable_value_button"]');
btnReveal.click();
await wait(350);
}
/* gather secrets in a list of strings */
const secrets = [...document.querySelectorAll('.js-ci-variable-row')].reduce(
(result, row) => {
const key = row.querySelector('td:nth-child(2)').innerText;
const value = row.querySelector('td:nth-child(3)').innerText;
if (key) {
result.push(`${key}=${value}`);
}
return result;
},
[],
);
/* display secrets as text rows */
const el = document.createElement('pre');
el.innerHTML = secrets.join('\n');
document.getElementById('content-body').prepend(el);
window.scrollTo(0, 0);
}
async function showGitlabSecretsJSON() {
const wait = (delay) => new Promise(resolve => setTimeout(resolve, delay));
/* ensure the secrets table is expanded */
const isExpanded = document.querySelector('#js-cicd-variables-settings').classList.contains('expanded');
if (!isExpanded) {
const btnExpand = document.querySelector('#js-cicd-variables-settings .js-settings-toggle');
btnExpand.click();
await wait(350);
}
/* ensure secrets are revealed */
const isRevealed = document.querySelector('tr.js-ci-variable-row td:nth-child(3)').innerText.startsWith('***') === false;
if (!isRevealed) {
const btnReveal = document.querySelector('[data-qa-selector="reveal_ci_variable_value_button"]');
btnReveal.click();
await wait(350);
}
/* gather secrets in a hashmap */
const secrets = [
...document.querySelectorAll('.js-ci-variable-row'),
].reduce((result, row) => {
const key = row.querySelector('td:nth-child(2)').innerText;
const value = row.querySelector('td:nth-child(3)').innerText;
if (key) {
Object.assign(result, { [key]: value });
}
return result;
}, {});
/* display secrets as JSON string */
const el = document.createElement('pre');
el.innerHTML = JSON.stringify(secrets, null, 2);
document.getElementById('content-body').prepend(el);
window.scrollTo(0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment