Skip to content

Commit

Permalink
feat: add information on changed files/lines on request review message
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Nov 10, 2023
1 parent 8fd7119 commit f3427ae
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/events/pr-handlers/readyForReview.test.ts
Expand Up @@ -42,6 +42,10 @@ describe('edited', (): void => {
body: '### Progress\n\n☑️ Step 1: ✏️ Write code\n☑️ Step 2: 💚 Checks\n⬜ Step 3: 👌 Code Review\n⬜ Step 4: 🚦 Merge Pull Request\n\n### Options:\n- [ ] <!-- reviewflow-autoMergeWithSkipCi -->Add `[skip ci]` on merge commit\n- [ ] <!-- reviewflow-autoMerge -->Auto merge when this PR is ready and has no failed statuses. (Also has a queue per repo to prevent multiple useless "Update branch" triggers)\n- [x] <!-- reviewflow-deleteAfterMerge -->Automatic branch delete after this PR is merged',
})

.get('/repos/reviewflow/reviewflow-test/pulls/54')
.times(1)
.reply(200, pullRequestReadyForReview.payload.pull_request)

.get(
'/repos/reviewflow/reviewflow-test/commits/f354ffb37cf238108fbb4c915f155d925d82a61b/check-runs?per_page=100',
)
Expand Down
20 changes: 15 additions & 5 deletions src/events/pr-handlers/readyForReview.ts
Expand Up @@ -8,6 +8,7 @@ import { updateStatusCheckFromStepsState } from './actions/updateStatusCheckFrom
import hasLabelInPR from './actions/utils/labels/hasLabelInPR';
import { calcStepsState } from './actions/utils/steps/calcStepsState';
import { createPullRequestHandler } from './utils/createPullRequestHandler';
import { fetchPr, type PullRequestFromRestEndpoint } from './utils/fetchPr';

export default function readyForReview(
app: Probot,
Expand Down Expand Up @@ -35,6 +36,8 @@ export default function readyForReview(
})),
);

let updatedPullRequest: PullRequestFromRestEndpoint | undefined;

/* if repo is not ignored */
if (reviewflowPrContext) {
const autoMergeLabel = repoContext.labels['merge/automerge'];
Expand All @@ -44,7 +47,8 @@ export default function readyForReview(
reviewflowPrContext,
});

await Promise.all([
const [updatedPr] = await Promise.all([
fetchPr(context, pullRequest.number),
updateReviewStatus(pullRequest, context, repoContext, stepsState),
editOpenedPR({
stepsState,
Expand Down Expand Up @@ -79,6 +83,7 @@ export default function readyForReview(
}
}),
]);
updatedPullRequest = updatedPr;
}

/* update slack home */
Expand Down Expand Up @@ -112,6 +117,12 @@ export default function readyForReview(

/* send slack notification */
if (repoContext.slack) {
const prChangesInformation =
updatedPullRequest &&
slackUtils.createPrChangesInformation(
updatedPullRequest,
repoContext,
);
const createText = ({
requestedTeam,
}: {
Expand All @@ -121,10 +132,9 @@ export default function readyForReview(
sender.login,
)} marked as ready to review and requests ${
!requestedTeam ? 'your' : `your team _${requestedTeam.name}_`
} review on ${slackUtils.createPrLink(
pullRequest,
repoContext,
)} !\n> ${pullRequest.title}`;
} review on ${slackUtils.createPrLink(pullRequest, repoContext)}${
prChangesInformation ? ` · ${prChangesInformation}` : ''
}\n> ${pullRequest.title}`;

const messageRequestedReviewers = {
text: createText({}),
Expand Down
13 changes: 12 additions & 1 deletion src/events/pr-handlers/reviewRequested.ts
Expand Up @@ -4,6 +4,7 @@ import * as slackUtils from '../../slack/utils';
import { getReviewersWithState } from '../../utils/github/pullRequest/reviews';
import { updateAfterReviewChange } from './actions/updateAfterReviewChange';
import { createPullRequestHandler } from './utils/createPullRequestHandler';
import type { PullRequestFromRestEndpoint } from './utils/fetchPr';
import { fetchPr } from './utils/fetchPr';

export default function reviewRequested(
Expand All @@ -29,6 +30,8 @@ export default function reviewRequested(
? [requestedReviewer]
: await repoContext.getMembersForTeams([requestedTeam.id]);

let updatedPullRequest: PullRequestFromRestEndpoint | undefined;

if (
/* repo is not ignored */
reviewflowPrContext
Expand All @@ -37,6 +40,7 @@ export default function reviewRequested(
fetchPr(context, pullRequest.number),
getReviewersWithState(context, pullRequest),
]);
updatedPullRequest = updatedPr;

await updateAfterReviewChange(
updatedPr,
Expand Down Expand Up @@ -80,12 +84,19 @@ export default function reviewRequested(
sender.login,
)} requests ${
requestedReviewer ? 'your' : `your team _${requestedTeam.name}_`
} review on ${slackUtils.createPrLink(pullRequest, repoContext)} !${
} review on ${slackUtils.createPrLink(pullRequest, repoContext)}${
requestedByNameInTeam.length > 0
? ` (team members requested by name: ${requestedByNameInTeam.join(
', ',
)})`
: ''
}${
updatedPullRequest
? ` · ${slackUtils.createPrChangesInformation(
updatedPullRequest,
repoContext,
)}`
: ''
}\n> ${pullRequest.title}`;

const message = { text };
Expand Down
14 changes: 13 additions & 1 deletion src/slack/utils.ts
@@ -1,6 +1,9 @@
import type { RepoContext } from '../context/repoContext';
import type { CommitFromRestEndpoint } from '../events/commit-handlers/utils/fetchCommit';
import type { PullRequestWithDecentData } from '../events/pr-handlers/utils/PullRequestData';
import type {
PullRequestFromRestEndpoint,
PullRequestWithDecentData,
} from '../events/pr-handlers/utils/PullRequestData';

// https://api.slack.com/reference/surfaces/formatting#escaping
export const escapeText = (text: string): string => {
Expand All @@ -26,6 +29,15 @@ export const createPrLink = (
);
};

export const createPrChangesInformation = (
pr: PullRequestFromRestEndpoint,
repoContext: RepoContext,
): string => {
return `${pr.changed_files} file${
pr.changed_files > 1 ? 's' : ''
} changed (+${pr.additions} -${pr.deletions})`;
};

export const createCommitLink = (
commit: CommitFromRestEndpoint,
repoContext: RepoContext,
Expand Down

0 comments on commit f3427ae

Please sign in to comment.