From 65d05bfe692c7589ae4138439724ba7c0c73bec5 Mon Sep 17 00:00:00 2001 From: Rainer Killinger Date: Tue, 14 Apr 2020 11:55:46 +0200 Subject: [PATCH] feat: add merge request assignment --- src/tasks/tidy.ts | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/tasks/tidy.ts b/src/tasks/tidy.ts index b5a66a5a..6c4d5b8b 100644 --- a/src/tasks/tidy.ts +++ b/src/tasks/tidy.ts @@ -14,7 +14,7 @@ */ import {asyncPool} from '@krlwlfrt/async-pool'; import {Api} from '@openstapps/gitlab-api'; -import {AccessLevel, IssueState, MembershipScope, Milestone, Project, Scope} from '@openstapps/gitlab-api/lib/types'; +import {AccessLevel, IssueState, MembershipScope, MergeRequestState, Milestone, Project, Scope} from '@openstapps/gitlab-api/lib/types'; import {Logger} from '@openstapps/logger'; import {flatten2dArray, getProjects} from '../common'; import { @@ -360,6 +360,47 @@ export async function tidyIssuesWithoutAssignee(api: Api): Promise { Logger.ok('Tidied issues without assignee.'); } +/** + * Tidy merge requests without assignee + * + * Set assignee to author if no assignee is set. + * + * @param api GitLab API instance to use for the requests + */ +export async function tidyMergeRequestsWithoutAssignee(api: Api): Promise { + const mergeRequestResults = await asyncPool(CONCURRENCY, GROUPS, async (groupId) => { + return api.getMergeRequests( + MembershipScope.GROUPS, + groupId, + MergeRequestState.OPENED, + ); + }); + + // flatten structure, e.g. put all issues in one array + const mergeRequests = flatten2dArray(mergeRequestResults); + + const mergeRequestsWithoutAssignee = mergeRequests.filter((mergeRequest) => { + return mergeRequest.assignee === null; + }); + + Logger.info(`Found '${mergeRequestsWithoutAssignee.length}' merge requests without assignee.`); + + await asyncPool(CONCURRENCY, mergeRequestsWithoutAssignee, async (mergeRequest) => { + await api.setAssigneeForMergeRequest(mergeRequest, mergeRequest.author.id); + + Logger.log(`Set assignee for '${mergeRequest.title}' to '${mergeRequest.author.name}'.`); + + await api.createNote( + mergeRequest.project_id, + Scope.MERGE_REQUESTS, + mergeRequest.iid, + `${NOTE_PREFIX} Assignee was set automatically to author.`, + ); + }); + + Logger.ok('Tidied merge requests without assignee.'); +} + /** * Tidy * @@ -382,4 +423,5 @@ export async function tidy(api: Api) { await tidyOpenIssuesWithoutMeetingLabel(api); await tidyIssuesWithoutAssignee(api); + await tidyMergeRequestsWithoutAssignee(api); }