feat: add batch processing of issues

This commit is contained in:
Karl-Philipp Wulfert
2019-11-19 13:56:05 +01:00
parent 61195370c7
commit fcf7caca50
3 changed files with 46 additions and 3 deletions

5
package-lock.json generated
View File

@@ -33,6 +33,11 @@
"regenerator-runtime": "^0.13.2"
}
},
"@krlwlfrt/async-pool": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@krlwlfrt/async-pool/-/async-pool-0.1.0.tgz",
"integrity": "sha512-PbDyjVme3HR8CrMI04SokU97Enq/+txP5fS2O0XYVSmMYteJ7Q9CLO2y0t8PmNZkt4YCxmHgaNEdMs+/Ki+PAA=="
},
"@openstapps/configuration": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/@openstapps/configuration/-/configuration-0.21.1.tgz",

View File

@@ -21,6 +21,7 @@
"author": "Karl-Philipp Wulfert <krlwlfrt@gmail.com>",
"license": "GPL-3.0-only",
"dependencies": {
"@krlwlfrt/async-pool": "0.1.0",
"@openstapps/logger": "0.4.0",
"@types/node": "10.17.5",
"@types/request": "2.48.3",

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 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.
@@ -12,6 +12,7 @@
* 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 {asyncPool} from '@krlwlfrt/async-pool';
import {Logger} from '@openstapps/logger';
import {AddLogLevel} from '@openstapps/logger/lib/transformations/add-log-level';
import {Colorize} from '@openstapps/logger/lib/transformations/colorize';
@@ -19,6 +20,7 @@ import * as commander from 'commander';
import {readFileSync} from 'fs';
import {join} from 'path';
import {Api, ApiRequestOptions} from './api';
import {Issue} from './types';
Logger.setTransformations([
new AddLogLevel(),
@@ -48,14 +50,49 @@ commander
options.data = JSON.parse(data);
}
const gitLabApi = new Api(commander.url, commander.token);
const api = new Api(commander.url, commander.token);
const result = await gitLabApi.makeGitLabAPIRequest(call, options);
const result = await api.makeGitLabAPIRequest(call, options);
// tslint:disable-next-line:no-console
console.log(result);
});
commander
.command('batch-process <projectId> <action>')
.action(async (projectId, action) => {
if (!['close'].includes(action)) {
await Logger.error('Only "close" is supported as action.');
}
const api = new Api(commander.url, commander.token);
const issues = await api.makeGitLabAPIRequest(`/projects/${projectId}/issues?state=opened`, {
retryOnAnyError: true,
tries: 10,
}) as Issue[];
Logger.log(`Fetched ${issues.length} issue(s).`);
// tslint:disable-next-line:no-magic-numbers
await asyncPool(5, issues, async (issue) => {
if (action === 'close') {
await api.makeGitLabAPIRequest(`/projects/${projectId}/issues/${issue.iid}`, {
data: {
state_event: 'close',
},
method: 'PUT',
retryOnAnyError: true,
tries: 10,
});
}
// tslint:disable-next-line:no-console
Logger.info(`Processed issue #${issue.iid} of project '${projectId}': ${issue.title}`);
});
Logger.ok('Processed all issues.');
});
commander
.parse(process.argv);