Skip to content

Commit

Permalink
refactor and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
quangkhoa committed Aug 17, 2017
1 parent fc65619 commit cc60549
Show file tree
Hide file tree
Showing 15 changed files with 2,870 additions and 217 deletions.
8 changes: 0 additions & 8 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
[
"transform-async-to-module-method",
{ "module": "bluebird", "method": "coroutine" }
],
[
"babel-plugin-transform-builtin-extend",
{
"globals": [
"Error"
]
}
]
]
}
12 changes: 12 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"parser": "babel-eslint",
"extends": "airbnb-base",
"root": true,
"env": {
"node": true,
"mocha": true
},
"rules": {
"no-console": 0
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
bin
gitlab.env.yml
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
{
"name": "gitlab-ci",
"version": "0.0.1",
"description": "CLI tool to interact with gitlab CI",
"description": "CLI tool to handle gitlab CI project variables",
"author": "Khoa Tran",
"bin": {
"setAllVars": "bin/setAllVariables.js"
},
"main": "bin/setAllVariables.js",
"files": [
"bin"
],
"scripts": {
"lint": "$(npm bin)/eslint src",
"test": "NODE_ENV=test $(npm bin)/mocha --recursive --compilers js:babel-core/register test",
"test:watch": "npm run test -- --watch"
"test:watch": "npm run test -- --watch",
"build": "$(npm bin)/babel src --out-dir bin",
"preversion": "npm run build && npm run lint"
},
"dependencies": {
"axios": "^0.16.2",
"commander": "^2.11.0",
"js-yaml": "^3.9.1",
"url-parse": "^1.1.9",
"yaml-js": "^0.2.0"
"url-parse": "^1.1.9"
},
"devDependencies": {
"axios-mock-adapter": "^1.9.0",
"babel-cli": "^6.24.1",
"babel-eslint": "^7.2.3",
"babel-plugin-transform-async-to-module-method": "^6.24.1",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.0",
"bluebird": "^3.5.0",
"chai": "^4.1.1",
"eslint": "^4.4.1",
"eslint-config-airbnb-base": "^11.3.1",
"eslint-plugin-import": "^2.7.0",
"mocha": "^3.5.0"
}
}
12 changes: 0 additions & 12 deletions src/gitlabci-variables.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/gitlabci.js

This file was deleted.

143 changes: 71 additions & 72 deletions src/lib/gitlab-ci-project-handler.js → src/lib/gitlab-ci.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import URL from 'url-parse'
import axios from 'axios';
import URL from 'url-parse';

/**
* Provides utility functions to simplify interacting with a GitLab CI project through the API
Expand All @@ -9,72 +9,17 @@ import URL from 'url-parse'
*
* @return {Object} utility functions wrapped in an object
*/
export function gitlabCIProjectHandler (url, token) {
const parsedUrl = new URL(url)
export default function gitlabCI(url, token) {
const parsedUrl = new URL(url);

// Construct project id by encoding namespace/projectName
const projectId = parsedUrl.pathname
.split('/')
.filter(token => token)
.join('%2F')
.filter(x => x)
.join('%2F');

const apiUrl = `${parsedUrl.origin}/api/v4/projects/${projectId}/variables`
const tokenQueryString = `private_token=${token}`

/**
* Get all variables for project
*
* @return {Promise<Array>} array of variable objects
*/
async function listVariables () {
const response = await axios.get(`${apiUrl}?${tokenQueryString}`)

return response.data
}

/**
* Set project variables
*
* @param {Object} properties
* @param forceUpdate if true, override existing values, otherwise ignore them
*
* @return {Promise<Array>} array of variable objects
*/
async function setVariables (properties, forceUpdate) {
if (!properties) {
return
}

const existingKeys = (await listVariables()).map(variable => variable.key)
const keysToSet = Object.keys(properties)

const promises = keysToSet.map(async key => {
const value = properties[key]
const keyExists = existingKeys.some(existingKey => existingKey === key)

if (keyExists && !forceUpdate) {
console.log(`Skipping ${key}, already set for ${projectId}.`)
return undefined
}

let variable
if (keyExists) {
// Update variable
variable = await updateVariable(key, value)
} else {
// Create variable
variable = await setVariable(key, value)
}

console.log(`Set ${key} = ${value} for ${projectId}`)
return variable
}
)

const variables = await Promise.all(promises)

return variables.filter(variable => variable)
}
const apiUrl = `${parsedUrl.origin}/api/v4/projects/${projectId}/variables`;
const tokenQueryString = `private_token=${token}`;

/**
* Set project variable
Expand All @@ -84,17 +29,17 @@ export function gitlabCIProjectHandler (url, token) {
*
* @return {Promise<Object>} variable object
*/
async function setVariable (key, value) {
async function setVariable(key, value) {
const response = await axios({
method: 'post',
url: `${apiUrl}?${tokenQueryString}`,
data: {
key,
value,
},
})
});

return response.data
return response.data;
}

/**
Expand All @@ -105,23 +50,77 @@ export function gitlabCIProjectHandler (url, token) {
*
* @return {Promise<Object>} variable object
*/
async function updateVariable (key, value) {
async function updateVariable(key, value) {
const response = await axios({
method: 'put',
url: `${apiUrl}/${key}?${tokenQueryString}`,
data: {
key,
value,
},
})
});

return response.data;
}

/**
* Get all variables for project
*
* @return {Promise<Array>} array of variable objects
*/
async function listVariables() {
const response = await axios.get(`${apiUrl}?${tokenQueryString}`);

return response.data;
}

/**
* Set project variables
*
* @param {Object} properties
* @param forceUpdate if true, override existing values, otherwise ignore them
*
* @return {Promise<Array>} array of variable objects
*/
async function setVariables(properties, forceUpdate) {
if (!properties) {
return null;
}

const existingKeys = (await listVariables()).map(variable => variable.key);
const keysToSet = Object.keys(properties);

const promises = keysToSet.map(async (key) => {
const value = properties[key];
const keyExists = existingKeys.some(existingKey => existingKey === key);

if (keyExists && !forceUpdate) {
console.log(`Skipping ${key}, already set for ${projectId}.`);
return undefined;
}

return response.data
let variable;
if (keyExists) {
// Update variable
variable = await updateVariable(key, value);
} else {
// Create variable
variable = await setVariable(key, value);
}

console.log(`Set ${key} = ${value} for ${projectId}`);
return variable;
});

const variables = await Promise.all(promises);

return variables.filter(variable => variable);
}

return {
setVariable,
updateVariable,
listVariables,
setVariables,
setVariable,
updateVariable
}
};
}
21 changes: 0 additions & 21 deletions src/lib/properties-file-handler.js

This file was deleted.

22 changes: 22 additions & 0 deletions src/lib/properties-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from 'fs';
import yaml from 'js-yaml';

/**
* Load properties file into properties object
* A property is a key/value pair.
*
* @param path
*
* @return {Object} properties
*/
export default function loadPropertiesFile(path) {
let doc;
try {
const contents = fs.readFileSync(path, 'utf8');
doc = yaml.safeLoad(contents);
} catch (error) {
console.log(error);
}

return doc;
}
Loading

0 comments on commit cc60549

Please sign in to comment.