From 49da0ff52fa0acc69eedbde505730b1b52b34387 Mon Sep 17 00:00:00 2001 From: pmckl Date: Tue, 15 Apr 2025 23:38:57 +0200 Subject: [PATCH] add assignees to publish pr action Signed-off-by: pmckl --- .../src/actions/githubPullRequest.ts | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index c0ff0fb3d4..adf80c0e0a 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -139,6 +139,7 @@ export const createPublishGithubPullRequestAction = ( sourcePath?: string; token?: string; reviewers?: string[]; + assignees?: string[]; teamReviewers?: string[]; commitMessage?: string; update?: boolean; @@ -212,6 +213,15 @@ export const createPublishGithubPullRequestAction = ( description: 'The users that will be added as reviewers to the pull request', }, + assignees: { + title: 'Pull Request Assignees', + type: 'array', + items: { + type: 'string', + }, + description: + 'The users that will be added as assignees to the pull request', + }, teamReviewers: { title: 'Pull Request Team Reviewers', type: 'array', @@ -295,6 +305,7 @@ export const createPublishGithubPullRequestAction = ( sourcePath, token: providedToken, reviewers, + assignees, teamReviewers, commitMessage, update, @@ -453,8 +464,8 @@ export const createPublishGithubPullRequestAction = ( } const pullRequestNumber = pr.number; + const pullRequest = { owner, repo, number: pullRequestNumber }; if (reviewers || teamReviewers) { - const pullRequest = { owner, repo, number: pullRequestNumber }; await requestReviewersOnPullRequest( pullRequest, reviewers, @@ -465,6 +476,21 @@ export const createPublishGithubPullRequestAction = ( ); } + if (assignees) { + if (assignees.length > 10) { + ctx.logger.warn( + 'Assignees list is too long, only the first 10 will be used.', + ); + } + await addAssigneesToPullRequest( + pullRequest, + assignees, + client, + ctx.logger, + ctx.checkpoint, + ); + } + const targetBranch = pr.base.ref; ctx.output('targetBranchName', targetBranch); ctx.output('remoteUrl', pr.html_url); @@ -475,6 +501,42 @@ export const createPublishGithubPullRequestAction = ( }, }); + async function addAssigneesToPullRequest( + pr: GithubPullRequest, + assignees: string[], + client: Octokit, + logger: LoggerService, + checkpoint: (opts: { + key: string; + fn: () => Promise | T; + }) => Promise, + ) { + try { + await checkpoint({ + key: `add.assignees.${pr.owner}.${pr.repo}.${pr.number}`, + fn: async () => { + const result = await client.rest.issues.addAssignees({ + owner: pr.owner, + repo: pr.repo, + issue_number: pr.number, + assignees, + }); + + const addedAssignees = result.data.assignees?.join(', ') ?? ''; + + logger.info( + `Added assignees [${addedAssignees}] to Pull request ${pr.number}`, + ); + }, + }); + } catch (e) { + logger.error( + `Failure when adding assignees to Pull request ${pr.number}`, + e, + ); + } + } + async function requestReviewersOnPullRequest( pr: GithubPullRequest, reviewers: string[] | undefined,