-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·127 lines (114 loc) · 4.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/local/bin/node
require('es6-promise').polyfill();
require('isomorphic-fetch');
let fs = require('fs');
let path = require('path');
var shell = require('shelljs');
let args = require('subarg')(process.argv.slice(2));
let command = args._[0];
let cmdArgs = args._.join(' ');
const SETTINGS_HOME = `${require('os').homedir()}/.git-lab`;
const SETTINGS_LOCAL = `${process.cwd()}/.git-lab`;
const API_VERSION = 'v4';
let debug = process.env.DEBUG || args.d || args.debug;
let settings = {};
try {
settings = JSON.parse(fs.readFileSync(SETTINGS_LOCAL, 'utf8'));
} catch (e) {
if (debug) console.error('no settings file found ".git-lab"');
}
let token = process.env.TOKEN || args.t || args.token || args.access_token || args['access-token'] || settings.token;
let url = process.env.URL || args.u || args.url || settings.url;
let group = process.env.GROUP || args.g || args.group || settings.group;
if (args.h || args.help) {
console.log('\ngitlab tool\n');
console.log('usage: git-lab {options} -- {git command} [git options] \n');
console.log('options:');
console.log(' --token, -t gitlab acces token');
console.log(' --url, -u gitlab url, e.g: https://gitlab.myserver.com');
console.log(' --group, -g gitlab group name\n');
console.log('example:\n');
console.log(' git-lab --token 009afdg0SdfAS14250 --url https://gitlab.myserver.com --group rocket-science --save -- clone\n');
console.log('example with environment variables:\n');
console.log(' TOKEN=009afdg0SdfAS14250 URL=https://gitlab.myserver.com GROUP=rocket-science git-lab -s -- clone\n');
process.exit();
}
if (debug) {
console.log('settings file local: ', SETTINGS_LOCAL);
console.log('settings file home: ', SETTINGS_HOME);
console.log('settings', settings);
console.log(`TOKEN ${token}`);
console.log(`URL ${url}`);
console.log(`GROUP ${group}`);
console.log('git-lab arguments', args);
console.log('command', command);
console.log('command with arguments: ', cmdArgs);
}
if (!token || !url) {
console.error('must provide TOKEN, URL and GROUP as environment variables');
console.error('or provide them as arguments: --token --url --group');
process.exit(-1)
}
if (args.s || args.save) {
fs.writeFileSync(SETTINGS_LOCAL, JSON.stringify({ token, url, group }, null, 2), 'utf8');
}
if (command === 'get-boards') {
process.stdin.pipe(require('split')()).on('data', processLine)
function processLine(projectId) {
var projectId = projectId.replace(/\//g, '%2F')
if (!projectId.trim()) return;
console.log('projectId' + projectId)
fetch(`${url}/api/${API_VERSION}/projects/${projectId}/boards?private_token\=${token}`)
.then(handleError)
.then((boards) => {
boards.forEach((board) => console.log(board));
})
//.catch(reason => console.log("catch" + reason))
}
} else if (command === 'add-label') {
process.stdin.pipe(require('split')()).on('data', processLine)
function processLine(line) {
console.log('bla' + line + '!')
}
} else if (command === 'ls') {
fetch(`${url}/api/${API_VERSION}/projects?private_token\=${token}&per_page=999`)
.then(handleError)
.then((projects) => {
projects.forEach((project) => console.log(project.path_with_namespace));
})
} else {
fetch(`${url}/api/${API_VERSION}/groups/${group}/projects\?private_token\=${token}&per_page=999`)
.then(handleError)
.then(function (projects) {
projects.forEach(project => {
execute(project.http_url_to_repo, command, cmdArgs);
})
});
}
function handleError(res) {
if (res.status >= 400) {
throw new Error("Bad response from server");
}
return res.json();
}
function execute(repo, command, cmdArgs) {
if (command === 'clone') {
output(shell.exec(`git ${cmdArgs} ${repo}`, { async: true, silent: true }));
} else if (command) {
var dirname = repo.split('/').pop().split('.')[0];
var pwd = process.cwd();
shell.cd(`${pwd}/${dirname}`);
output(shell.exec(cmdArgs, { async: true, silent: true }));
shell.cd(pwd);
} else {
console.log(repo);
}
function output(child) {
child.stdout.on('data', function (data) {
console.log(`${repo}: ${data}`);
});
child.stderr.on('data', function (data) {
console.log(`${repo} ERR: ${data}`);
});
}
}