Skip to content

Commit

Permalink
feat(pr): support show requested reviewers
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Aug 18, 2023
1 parent 9cf84ce commit f628d33
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/github/templates/prOrIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
StopHandleError,
renderPrRefInfo,
renderAssigneeInfo,
renderRequestedReviewersInfo,
} from './utils';

import { textTpl } from '.';
Expand Down Expand Up @@ -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));
}
Expand Down
41 changes: 41 additions & 0 deletions src/github/templates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
}
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 =
Expand Down
12 changes: 12 additions & 0 deletions test/github/templates/__snapshots__/prOrIssue.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/github/templates/prOrIssue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
});
});

0 comments on commit f628d33

Please sign in to comment.