feat: add GitLab API

This commit is contained in:
Karl-Philipp Wulfert
2018-11-29 12:08:06 +01:00
commit 54d212963c
13 changed files with 3427 additions and 0 deletions

65
src/cli.ts Normal file
View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as commander from 'commander';
import {readFileSync} from 'fs';
import {join} from 'path';
import {Api, ApiRequestOptions, logger} from './api';
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json')).toString());
commander
.version(pkgJson.version);
commander
.option('-u, --url [url]', 'GitLab API URL', 'https://gitlab.com/api/v4/')
.option('-c, --call <call>', 'API call/sub url')
.option('-m, --method [method]', 'HTTP verb to use for the API call', 'GET')
.option('-d, --data [data]', 'JSON representation of data to send');
commander
.parse(process.argv);
if (typeof commander.call === 'undefined') {
commander.outputHelp();
process.exit(1);
}
if (typeof commander.token === 'undefined') {
if (typeof process.env.GITLAB_PRIVATE_TOKEN === 'undefined') {
logger.error('You have to supply ');
}
commander.token = process.env.GITLAB_PRIVATE_TOKEN;
}
const gitLabApi = new Api(commander.url, commander.token);
const options: ApiRequestOptions = {};
if (commander.method !== 'GET') {
options.method = commander.method;
}
if (typeof commander.data !== 'undefined') {
options.data = JSON.parse(commander.data);
}
gitLabApi.makeGitLabAPIRequest(commander.call, options).then((result) => {
logger.ok(result);
process.exit(1);
}, (err) => {
logger.error(err);
process.exit(1);
});