diff --git a/package-lock.json b/package-lock.json index cb1cb900..e7732184 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index bc7836bd..80545826 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "author": "Karl-Philipp Wulfert ", "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", diff --git a/src/cli.ts b/src/cli.ts index 2910f443..1cdc1f43 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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 . */ +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 ') + .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);