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

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);