mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 00:23:03 +00:00
refactor: adjust code to match eslint rules
This commit is contained in:
153
src/cli.ts
153
src/cli.ts
@@ -18,84 +18,75 @@ import {AddLogLevel} from '@openstapps/logger/lib/transformations/add-log-level'
|
||||
import {Colorize} from '@openstapps/logger/lib/transformations/colorize';
|
||||
import {Command} from 'commander';
|
||||
import {readFileSync} from 'fs';
|
||||
import {join} from 'path';
|
||||
import path from 'path';
|
||||
import {Api, ApiRequestOptions} from './api';
|
||||
import {Issue, IssueState, MembershipScope, Scope} from './types';
|
||||
|
||||
Logger.setTransformations([
|
||||
new AddLogLevel(),
|
||||
new Colorize(),
|
||||
]);
|
||||
Logger.setTransformations([new AddLogLevel(), new Colorize()]);
|
||||
|
||||
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'))
|
||||
.toString());
|
||||
// eslint-disable-next-line unicorn/prefer-module
|
||||
const packageJson = JSON.parse(readFileSync(path.join(__dirname, '..', 'package.json')).toString());
|
||||
|
||||
const commander = new Command('openstapps-gitlab-api');
|
||||
|
||||
commander
|
||||
.version(pkgJson.version);
|
||||
commander.version(packageJson.version);
|
||||
|
||||
commander
|
||||
.option('-t, --token [token]', 'GitLab API token', process.env.GITLAB_PRIVATE_TOKEN)
|
||||
.option('-u, --url [url]', 'GitLab API URL', 'https://gitlab.com/api/v4/');
|
||||
|
||||
commander
|
||||
.command('request <call> [method] [data]')
|
||||
.action(async (call, method, data) => {
|
||||
const options: ApiRequestOptions = {};
|
||||
commander.command('request <call> [method] [data]').action(async (call, method, data) => {
|
||||
const options: ApiRequestOptions = {};
|
||||
|
||||
if (method !== 'GET') {
|
||||
options.method = method;
|
||||
if (method !== 'GET') {
|
||||
options.method = method;
|
||||
}
|
||||
|
||||
if (data !== undefined) {
|
||||
options.data = JSON.parse(data);
|
||||
}
|
||||
|
||||
const api = new Api(commander.url, commander.token);
|
||||
|
||||
const result = await api.makeGitLabAPIRequest(call, options);
|
||||
|
||||
// eslint-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).`);
|
||||
|
||||
await asyncPool(5, issues, async issue => {
|
||||
if (action === 'close') {
|
||||
Logger.info(`Closing issue #${issue.iid} of project '${projectId}': ${issue.title}.`);
|
||||
|
||||
await api.makeGitLabAPIRequest(`/projects/${projectId}/issues/${issue.iid}`, {
|
||||
data: {
|
||||
state_event: 'close',
|
||||
},
|
||||
method: 'PUT',
|
||||
retryOnAnyError: true,
|
||||
tries: 10,
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof data !== 'undefined') {
|
||||
options.data = JSON.parse(data);
|
||||
}
|
||||
|
||||
const api = new Api(commander.url, commander.token);
|
||||
|
||||
const result = await api.makeGitLabAPIRequest(call, options);
|
||||
|
||||
// tslint:disable-next-line:no-console
|
||||
console.log(result);
|
||||
Logger.info(`Processed issue #${issue.iid} of project '${projectId}': ${issue.title}`);
|
||||
});
|
||||
|
||||
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') {
|
||||
Logger.info(`Closing issue #${issue.iid} of project '${projectId}': ${issue.title}.`);
|
||||
|
||||
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.');
|
||||
});
|
||||
Logger.ok('Processed all issues.');
|
||||
});
|
||||
|
||||
commander
|
||||
.command('copy <projectId> <targetUrl> <targetToken> <targetProjectId>')
|
||||
@@ -104,10 +95,10 @@ commander
|
||||
const targetApi = new Api(targetUrl, targetToken);
|
||||
|
||||
// get all issues from project
|
||||
const issues = await api.makeGitLabAPIRequest(`/projects/${projectId}/issues`, {
|
||||
const issues = (await api.makeGitLabAPIRequest(`/projects/${projectId}/issues`, {
|
||||
retryOnAnyError: true,
|
||||
tries: 10,
|
||||
}) as Issue[];
|
||||
})) as Issue[];
|
||||
|
||||
// sort issues by their project specific ids
|
||||
issues.sort((a, b) => {
|
||||
@@ -117,17 +108,23 @@ commander
|
||||
// get members of target project
|
||||
const members = await targetApi.getMembers(MembershipScope.PROJECTS, targetProjectId);
|
||||
|
||||
let idx = 0;
|
||||
let index = 0;
|
||||
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
await asyncPool(2, issues, async (issue) => {
|
||||
await asyncPool(2, issues, async issue => {
|
||||
// get notes of old issue
|
||||
const notes = await api.getNotes(projectId, issue);
|
||||
|
||||
// create new issue
|
||||
const newIssue = await targetApi.createIssue(targetProjectId, issue.title, issue.description === null ? '---' : `${issue.web_url}
|
||||
const newIssue = await targetApi.createIssue(
|
||||
targetProjectId,
|
||||
issue.title,
|
||||
issue.description === null
|
||||
? '---'
|
||||
: `${issue.web_url}
|
||||
|
||||
${issue.description}`);
|
||||
${issue.description}`,
|
||||
);
|
||||
|
||||
for (const note of notes) {
|
||||
// skip system notes
|
||||
@@ -136,9 +133,14 @@ ${issue.description}`);
|
||||
}
|
||||
|
||||
// create new note in new issue for every note in issue
|
||||
await targetApi.createNote(targetProjectId, Scope.ISSUES, newIssue.iid, `**${note.author.name} (@${note.author.username}):**
|
||||
await targetApi.createNote(
|
||||
targetProjectId,
|
||||
Scope.ISSUES,
|
||||
newIssue.iid,
|
||||
`**${note.author.name} (@${note.author.username}):**
|
||||
|
||||
${note.body}`);
|
||||
${note.body}`,
|
||||
);
|
||||
}
|
||||
|
||||
// close newly created issue if original is closed to
|
||||
@@ -154,7 +156,7 @@ ${note.body}`);
|
||||
}
|
||||
|
||||
// search for member in target group with same username
|
||||
const assignee = members.find((member) => {
|
||||
const assignee = members.find(member => {
|
||||
if (issue.assignee === null) {
|
||||
return false;
|
||||
}
|
||||
@@ -163,17 +165,18 @@ ${note.body}`);
|
||||
});
|
||||
|
||||
// set assignee if usernames match
|
||||
if (typeof assignee !== 'undefined') {
|
||||
if (assignee !== undefined) {
|
||||
await targetApi.setAssigneeForIssue(newIssue, assignee.id);
|
||||
}
|
||||
|
||||
Logger.log(`Finished issue ${++idx} of ${issues.length}.`);
|
||||
Logger.log(`Finished issue ${++index} of ${issues.length}.`);
|
||||
});
|
||||
});
|
||||
|
||||
commander
|
||||
.parse(process.argv);
|
||||
commander.parse(process.argv);
|
||||
|
||||
if (typeof commander.token !== 'string' || commander.token.length === 0) {
|
||||
Logger.warn('You probably want to supply a GitLab token either via option or environment variable (GITLAB_PRIVATE_TOKEN).');
|
||||
Logger.warn(
|
||||
'You probably want to supply a GitLab token either via option or environment variable (GITLAB_PRIVATE_TOKEN).',
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user