Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new options (masked, protected, raw, scope) and handle HTTP errors #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ bin
gitlab.env.yml
.nyc_output
.gitlabrc
.idea
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
],
"scripts": {
"lint": "$(npm bin)/eslint src",
"test": "NODE_ENV=test $(npm bin)/mocha --recursive --compilers js:babel-core/register test",
"test:coverage": "$(npm bin)/nyc --check-coverage --lines 90 --reporter=text --reporter=text-summary npm test",
"build": "rm -Rf bin && $(npm bin)/babel src --out-dir bin --source-maps inline",
"test": "NODE_ENV=test ./node_modules/.bin/mocha --recursive --compilers js:babel-core/register test",
"test:coverage": "./node_modules/.bin/nyc --check-coverage --lines 90 --reporter=text --reporter=text-summary npm test",
"build": "rm -Rf bin && ./node_modules/.bin/babel src --out-dir bin --source-maps inline && chmod +x ./bin/*.js",
"deploy": "npm run build && npm link",
"preversion": "npm run build && npm run lint && npm run test"
},
Expand Down
25 changes: 21 additions & 4 deletions src/glci-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ async function execute(cmd) {

const keyExists = existingKeys.includes(conf.key);

const isProtected = Boolean(cmd.protected);
const masked = Boolean(cmd.masked);
const raw = Boolean(cmd.raw);
const scope = cmd.environmentScope !== undefined ? cmd.environmentScope : '*';

if (keyExists && cmd.doNotForce) {
console.log(`Skipping ${conf.key}, already set.`);
return undefined;
}

if (keyExists) {
resp = await handler.updateVariable(conf.key, conf.value);
resp = await handler.updateVariable(conf.key, conf.value, isProtected, masked, raw, scope);
} else {
resp = await handler.createVariable(conf.key, conf.value);
resp = await handler.createVariable(conf.key, conf.value, isProtected, masked, raw, scope);
}

console.log('Completed setting variable on Gitlab CI.');
Expand All @@ -53,8 +58,20 @@ program
'Your Gitlab CI value',
)
.option(
'--do-not-force',
'Ignore variable if it already exists on gitlab CI. By default variable is overridden',
'--protected',
'Set the variable as protected. By default it is not protected',
)
.option(
'--masked',
'Set the variable as masked. By default it is not masked',
)
.option(
'--raw',
'Set the variable as raw. By default it is not raw',
)
.option(
'--environment-scope <environment-scope>',
'Set the environment scope. By default it is set to *',
)
.parse(process.argv);

Expand Down
23 changes: 22 additions & 1 deletion src/glci-setAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ async function execute(cmd) {
const conf = await getConf();

const properties = getProperties();
const forceUpdate = !cmd.doNotForce;
const isProtected = Boolean(cmd.protected);
const masked = Boolean(cmd.masked);
const raw = Boolean(cmd.raw);
const scope = cmd.environmentScope !== undefined ? cmd.environmentScope : '*';
const handler = gitlabCI(conf.url, conf.token);
const resp = await handler.setVariables(properties, !cmd.doNotForce);
const resp = await handler.setVariables(properties, forceUpdate, isProtected, masked, raw, scope);

console.log('Completed setting variables on Gitlab CI.');
return resp;
Expand All @@ -29,6 +34,22 @@ program
'--do-not-force',
'Ignore variables if they already exist on gitlab CI. By default all variables are overridden',
)
.option(
'--protected',
'Set the variable as protected. By default it is not protected',
)
.option(
'--masked',
'Set the variable as masked. By default it is not masked',
)
.option(
'--raw',
'Set the variable as raw. By default it is not raw',
)
.option(
'--environment-scope <environmentScope>',
'Set the environment scope. By default it is set to *',
)
.parse(process.argv);

execute(program);
78 changes: 60 additions & 18 deletions src/lib/gitlab-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,73 @@ export default function gitlabCI(url, token) {
*
* @param key
* @param value
*
* @param isProtected
* @param masked
* @param raw
* @param scope
* @return {Promise<Object>} variable object
*/
async function createVariable(key, value) {
const response = await axios({
async function createVariable(key, value, isProtected, masked, raw, scope) {
await axios({
method: 'post',
url: `${apiUrl}?${tokenQueryString}`,
data: {
key,
value: serialiseValue(value),
protected: isProtected,
masked,
raw,
environment_scope: scope,
},
});
}).then((response) => {
console.log(`Created new variable ${key} = ${JSON.stringify(value)}`);

console.log(`Created new variable ${key} = ${JSON.stringify(value)}`);
return response.data;
}).catch((error) => {
console.log(`Creation failed: ${error}`);

return response.data;
if (masked) {
console.log('Retrying unmasked...');
createVariable(key, value, isProtected, false, raw, scope);
}
});
}

/**
* Update project variable
*
* @param key
* @param value
*
* @param isProtected
* @param masked
* @param raw
* @param scope
* @return {Promise<Object>} variable object
*/
async function updateVariable(key, value) {
const response = await axios({
async function updateVariable(key, value, isProtected, masked, raw, scope) {
await axios({
method: 'put',
url: `${apiUrl}/${key}?${tokenQueryString}`,
data: {
key,
value: serialiseValue(value),
protected: isProtected,
masked,
raw,
environment_scope: scope,
},
});
}).then((response) => {
console.log(`Updated variable ${key} = ${JSON.stringify(value)}`);

console.log(`Updated variable ${key} = ${JSON.stringify(value)}`);
return response.data;
}).catch((error) => {
console.log(`Update failed: ${error}`);

return response.data;
if (masked) {
console.log('Retrying unmasked...');
updateVariable(key, value, isProtected, false, raw, scope);
}
});
}

/**
Expand All @@ -90,20 +118,34 @@ export default function gitlabCI(url, token) {
* @return {Promise<Array>} array of variable objects
*/
async function listVariables() {
const response = await axios.get(`${apiUrl}?${tokenQueryString}&${perPageQueryString}`);
let response = await axios.get(`${apiUrl}?${tokenQueryString}&${perPageQueryString}&page=1`);

const nbPages = response.headers['x-total-pages'] !== undefined ? response.headers['x-total-pages'] : 1;

let data = response.data;

for (let page = 2; page <= nbPages; page += 1) {
/* eslint-disable no-await-in-loop */
response = await axios.get(`${apiUrl}?${tokenQueryString}&${perPageQueryString}&page=${page}`);
data = [...data, ...response.data];
}

return response.data;
return data;
}

/**
* Set project variables
*
* @param {Object} properties
* @param forceUpdate if true, override existing values, otherwise ignore them
* @param forceUpdate
* @param isProtected
* @param masked
* @param raw
* @param scope
*
* @return {Promise<Array>} array of variable objects
*/
async function setVariables(properties, forceUpdate) {
async function setVariables(properties, forceUpdate, isProtected, masked, raw, scope) {
if (!properties) {
return null;
}
Expand All @@ -123,10 +165,10 @@ export default function gitlabCI(url, token) {
let variable;
if (keyExists) {
// Update variable
variable = await updateVariable(key, value);
variable = await updateVariable(key, value, isProtected, masked, raw, scope);
} else {
// Create variable
variable = await createVariable(key, value);
variable = await createVariable(key, value, isProtected, masked, raw, scope);
}

return variable;
Expand Down