diff --git a/src/github/templates/prOrIssue.ts b/src/github/templates/prOrIssue.ts index 8052f7d5..cc0e2b05 100644 --- a/src/github/templates/prOrIssue.ts +++ b/src/github/templates/prOrIssue.ts @@ -13,6 +13,7 @@ import { StopHandleError, renderPrRefInfo, renderAssigneeInfo, + renderRequestedReviewersInfo, } from './utils'; import { textTpl } from '.'; @@ -177,6 +178,10 @@ export async function handlePr( builder.add(renderPrRefInfo(data)); } + if (data.requested_reviewers?.length) { + builder.add(renderRequestedReviewersInfo(data.requested_reviewers)); + } + if (data.assignees?.length) { builder.add(renderAssigneeInfo(data.assignees)); } diff --git a/src/github/templates/utils.ts b/src/github/templates/utils.ts index 08929854..aaead1d9 100644 --- a/src/github/templates/utils.ts +++ b/src/github/templates/utils.ts @@ -14,6 +14,10 @@ export function renderUserLink(sender: { login: string; html_url: string }) { return `[${sender.login}](${sender.html_url})`; } +export function renderTeamLink(team: { name: string; html_url: string }) { + return `[${team.name}](${team.html_url})`; +} + export function renderAtUserLink(sender: { login: string; html_url: string }) { return `[@${sender.login}](${sender.html_url})`; } @@ -104,6 +108,43 @@ export function renderAssigneeInfo( return `> Assignees: ${assigneeNames} `; } +export function renderRequestedReviewersInfo( + reviewers: ( + | { + name: string; + html_url: string; + } + | { + login: string; + html_url: string; + } + )[], +) { + const reviewerNames = reviewers + .map((v) => + ( + v as { + login: string; + html_url: string; + } + ).login + ? renderUserLink( + v as { + login: string; + html_url: string; + }, + ) + : renderTeamLink( + v as { + name: string; + html_url: string; + }, + ), + ) + .join(', '); + return `> Requested reviewers: ${reviewerNames} `; +} + export function renderDeletedPrOrIssueTitleLink(p: { /** * The title of the pull request. diff --git a/test/fixtures/index.ts b/test/fixtures/index.ts index f17e3dcf..ccf91db6 100644 --- a/test/fixtures/index.ts +++ b/test/fixtures/index.ts @@ -9,6 +9,7 @@ import { import _antd_mini_release_published from './antd_mini_release_published.json'; import pull_request_0_opened from './generated/pull_request_0_opened.json'; +import _pull_request_13_opened from './generated/pull_request_13_opened.json'; import pull_request_3_closed from './generated/pull_request_3_closed.json'; import _pull_request_review_comment_0_created from './generated/pull_request_review_comment_0_created.json'; import _issue2045 from './issue-2045.json'; @@ -25,6 +26,8 @@ export const pull_request_closed = pull_request_3_closed as unknown as PullRequestClosedEvent; export const pull_request_opened = pull_request_0_opened as unknown as PullRequestOpenedEvent; +export const pull_request_13_opened = + _pull_request_13_opened as PullRequestOpenedEvent; export const pull_request_edited_wip = _pull_request_edited_wip as unknown as PullRequestEditedEvent; export const pull_request_edited_base = diff --git a/test/github/templates/__snapshots__/prOrIssue.test.ts.snap b/test/github/templates/__snapshots__/prOrIssue.test.ts.snap index 4f3ce0de..e6920d9c 100644 --- a/test/github/templates/__snapshots__/prOrIssue.test.ts.snap +++ b/test/github/templates/__snapshots__/prOrIssue.test.ts.snap @@ -20,6 +20,18 @@ exports[`github templates pr or issue can handle issue 1`] = ` } `; +exports[`github templates pr or issue can handle pull_request_13_opened 1`] = ` +{ + "text": "#### [Codertocat](https://github.com/Codertocat) opened [pull request](https://github.com/Codertocat/Hello-World/pull/2) +--- +> #### [#2 Update the README with new information.](https://github.com/Codertocat/Hello-World/pull/2) +> master <- changes +> Requested reviewers: [octocat](http://github.com/octocat) +> Assignees: [Codertocat](https://github.com/Codertocat)", + "title": "Pull request#2 opened", +} +`; + exports[`github templates pr or issue can handle pull_request_closed 1`] = ` { "text": "#### [Codertocat](https://github.com/Codertocat) closed [pull request](https://github.com/Codertocat/Hello-World/pull/2) diff --git a/test/github/templates/prOrIssue.test.ts b/test/github/templates/prOrIssue.test.ts index 8f19bcf1..7308ac48 100644 --- a/test/github/templates/prOrIssue.test.ts +++ b/test/github/templates/prOrIssue.test.ts @@ -3,6 +3,7 @@ import { handlePr, handleIssue } from '@/github/templates'; import { pull_request_closed, pull_request_opened, + pull_request_13_opened, pull_request_edited_wip, pull_request_edited_base, issue_opened_event, @@ -49,4 +50,12 @@ describe('github templates pr or issue', () => { expect(result.text).toBeDefined(); expect(result.title).toBeDefined(); }); + it('can handle pull_request_13_opened', async () => { + const result = await handlePr(pull_request_13_opened, ctx); + console.log(`pull_request_13_opened ~ result`, result.text); + expect(result).toMatchSnapshot(); + expect(result.text).toBeDefined(); + expect(result.text).toContain('Requested reviewers'); + expect(result.title).toBeDefined(); + }); });