From ba8db09ccf18c566e1864d4924c34a37a71410ce Mon Sep 17 00:00:00 2001 From: Alessandro Dalfovo Date: Fri, 5 Aug 2022 11:00:37 +0200 Subject: [PATCH] Introduce reviewers Signed-off-by: Alessandro Dalfovo --- .../builtin/publish/githubPullRequest.ts | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts index f8f9b22d61..cfaee62804 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/githubPullRequest.ts @@ -33,14 +33,14 @@ export type Encoding = 'utf-8' | 'base64'; class GithubResponseError extends CustomErrorBase {} /** @public */ -export interface OctokitWithPullRequestPluginClient { +export type OctokitWithPullRequestPluginClient = Octokit & { createPullRequest(options: createPullRequest.Options): Promise<{ data: { html_url: string; number: number; }; } | null>; -} +}; /** * The options passed to the client factory function. @@ -117,6 +117,8 @@ export const createPublishGithubPullRequestAction = ({ targetPath?: string; sourcePath?: string; token?: string; + reviewers?: string[]; + teamReviewers?: string[]; }>({ id: 'publish:github:pull-request', schema: { @@ -165,6 +167,22 @@ export const createPublishGithubPullRequestAction = ({ type: 'string', description: 'The token to use for authorization to GitHub', }, + reviewers: { + title: 'Pull Request Reviewers', + type: 'array', + items: { + type: 'string', + }, + description: 'Pull Request Reviewers', + }, + teamReviewers: { + title: 'Pull Request Team Reviewers', + type: 'array', + items: { + type: 'string', + }, + description: 'Pull Request Team Reviewers', + }, }, }, output: { @@ -194,6 +212,8 @@ export const createPublishGithubPullRequestAction = ({ targetPath, sourcePath, token: providedToken, + reviewers, + teamReviewers, } = ctx.input; const { owner, repo, host } = parseRepoUrl(repoUrl, integrations); @@ -259,8 +279,33 @@ export const createPublishGithubPullRequestAction = ({ throw new GithubResponseError('null response from Github'); } + const pullRequestNumber = response.data.number; + if (reviewers !== null || teamReviewers !== null) { + try { + const result = await client.rest.pulls.requestReviewers({ + owner, + repo, + pull_number: pullRequestNumber, + reviewers: reviewers, + team_reviewers: teamReviewers, + }); + const addedUsers = result.data.requested_reviewers ?? []; + const addedTeams = result.data.requested_teams ?? []; + ctx.logger.info( + `Added users [${addedUsers.join( + ',', + )}] and teams [${addedTeams.join(',')}] as reviewers`, + ); + } catch (e) { + ctx.logger.error( + `Failure when adding reviewers to Pull request ${pullRequestNumber}`, + e, + ); + } + } + ctx.output('remoteUrl', response.data.html_url); - ctx.output('pullRequestNumber', response.data.number); + ctx.output('pullRequestNumber', pullRequestNumber); } catch (e) { throw new GithubResponseError('Pull request creation failed', e); }