Skip to content

Commit

Permalink
feat: add changesInformation when possible in home slack
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Nov 11, 2023
1 parent e7c5695 commit ddb3892
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 14 deletions.
22 changes: 17 additions & 5 deletions src/events/pr-handlers/actions/editOpenedPR.ts
@@ -1,3 +1,4 @@
import type { Update } from 'liwi-mongo';
import type { StatusInfo } from '../../../accountConfigs/types';
import type { AppContext } from '../../../context/AppContext';
import type {
Expand Down Expand Up @@ -234,6 +235,20 @@ export const editOpenedPR = async <

const body = removeDeprecatedReviewflowInPrBody(pullRequest.body);

const partialUpdateReviewflowPr: Update<ReviewflowPr>['$set'] = {
title,
isDraft: pullRequest.draft === true,
isClosed: !!pullRequest.closed_at,
};

if ('changed_files' in pullRequest) {
partialUpdateReviewflowPr.changesInformation = {
changedFiles: pullRequest.changed_files,
additions: pullRequest.additions,
deletions: pullRequest.deletions,
};
}

const promises: (Promise<unknown> | undefined)[] = [
Promise.all(updateStatusesPromises).then(() => {
// only update reviewflowPr if all create successful
Expand All @@ -253,9 +268,7 @@ export const editOpenedPR = async <
{
$set: {
headSha: pullRequest.head.sha,
title,
isDraft: pullRequest.draft === true,
isClosed: !!pullRequest.closed_at,
...partialUpdateReviewflowPr,
checksConclusion: checksAndStatuses.checksConclusionRecord,
statusesConclusion: checksAndStatuses.statusesConclusionRecord,
...(reviews ? { reviews } : {}),
Expand All @@ -271,8 +284,7 @@ export const editOpenedPR = async <
reviewflowPrContext.reviewflowPr,
{
$set: {
title,
isDraft: pullRequest.draft === true,
...partialUpdateReviewflowPr,
// update old data
...(!reviewflowPrContext.reviewflowPr.assignees &&
pullRequest.assignees
Expand Down
6 changes: 6 additions & 0 deletions src/events/pr-handlers/readyForReview.test.ts
Expand Up @@ -119,7 +119,13 @@ describe('edited', (): void => {
});
expect(partialUpdateOnePr).toHaveBeenCalledWith(expect.any(Object), {
$set: {
isClosed: false,
isDraft: false,
changesInformation: {
additions: 2,
changedFiles: 1,
deletions: 0,
},
title: 'feat: test draft',
},
});
Expand Down
3 changes: 1 addition & 2 deletions src/events/pr-handlers/readyForReview.ts
Expand Up @@ -119,9 +119,8 @@ export default function readyForReview(
if (repoContext.slack) {
const prChangesInformation =
updatedPullRequest &&
slackUtils.createPrChangesInformation(
slackUtils.createPrChangesInformationFromPullRequestRest(
updatedPullRequest,
repoContext,
);
const createText = ({
requestedTeam,
Expand Down
3 changes: 1 addition & 2 deletions src/events/pr-handlers/reviewRequested.ts
Expand Up @@ -92,9 +92,8 @@ export default function reviewRequested(
: ''
}${
updatedPullRequest
? ` 路 ${slackUtils.createPrChangesInformation(
? ` 路 ${slackUtils.createPrChangesInformationFromPullRequestRest(
updatedPullRequest,
repoContext,
)}`
: ''
}\n> ${pullRequest.title}`;
Expand Down
8 changes: 8 additions & 0 deletions src/events/pr-handlers/utils/createPullRequestContext.ts
Expand Up @@ -93,6 +93,14 @@ export const getReviewflowPrContext = async <T extends EventsWithRepository>(
title: 'title' in pullRequest ? pullRequest.title : 'Unknown Title',
isClosed: 'closed_at' in pullRequest ? !!pullRequest.closed_at : false,
isDraft: 'draft' in pullRequest && pullRequest.draft === true,
changesInformation:
'changed_files' in pullRequest
? {
changedFiles: pullRequest.changed_files,
additions: pullRequest.additions,
deletions: pullRequest.deletions,
}
: undefined,
commentId,
reviews: groupReviewsWithState(reviewersWithState!),
assignees:
Expand Down
7 changes: 7 additions & 0 deletions src/mongo.ts
Expand Up @@ -161,6 +161,12 @@ export interface AutomergeLog extends MongoBaseModel {
action: 'remove' | 'reschedule' | 'update branch' | 'wait';
}

interface ReviewflowPrChangesInformation {
changedFiles: number;
additions: number;
deletions: number;
}

export interface ReviewflowPr extends MongoBaseModel {
account: AccountEmbed;
repo: RepoEmbed;
Expand All @@ -170,6 +176,7 @@ export interface ReviewflowPr extends MongoBaseModel {
title: string;
isDraft: boolean;
isClosed: boolean;
changesInformation?: ReviewflowPrChangesInformation;
lastLintStatusesCommit?: string;
lintStatuses?: ReviewflowStatus[];
lastFlowStatusCommit?: string;
Expand Down
18 changes: 16 additions & 2 deletions src/slack/home.ts
Expand Up @@ -2,7 +2,10 @@ import type { KnownBlock } from '@slack/web-api';
import { WebClient } from '@slack/web-api';
import type { MongoStores, Org, OrgMember, ReviewflowPr } from '../mongo';
import type { Octokit } from '../octokit';
import { createLink } from './utils';
import {
createLink,
createPrChangesInformationFromReviewflowPr,
} from './utils';

interface QueueItem {
github: Octokit;
Expand Down Expand Up @@ -147,6 +150,8 @@ export const createSlackHomeWorker = (mongoStores: MongoStores) => {
'https://api.github.com/repos/'.length,
);
const prFullName = `${repoName}#${pr.number}`;
// const changesInformation =
// createPrChangesInformationFromPullRequestRest(pr);

return [
{
Expand Down Expand Up @@ -175,6 +180,9 @@ export const createSlackHomeWorker = (mongoStores: MongoStores) => {
type: 'mrkdwn',
text: `${pr.user.login}`,
},
// ...(changesInformation
// ? [{ type: 'mrkdwn', text: changesInformation }]
// : []),
].filter(Boolean),
},
];
Expand All @@ -196,6 +204,8 @@ export const createSlackHomeWorker = (mongoStores: MongoStores) => {
const repoName = pr.repo.name;
const prFullName = `${repoName}#${pr.pr.number}`;
const prUrl = buildPullRequestUrl(pr);
const changesInformation =
createPrChangesInformationFromReviewflowPr(pr);

return [
{
Expand Down Expand Up @@ -238,6 +248,10 @@ export const createSlackHomeWorker = (mongoStores: MongoStores) => {
text: `${pr.creator.login}`,
},
].filter(Boolean)),

...(changesInformation
? [{ type: 'mrkdwn', text: changesInformation }]
: []),
],
},
];
Expand Down Expand Up @@ -341,7 +355,7 @@ export const createSlackHomeWorker = (mongoStores: MongoStores) => {
: mongoStores.slackTeams
.findByKey(org.slackTeamId)
.then((slackTeam) => {
if (!slackTeam || !slackTeam.botAccessToken) return undefined;
if (!slackTeam?.botAccessToken) return undefined;
return new WebClient(slackTeam.botAccessToken);
}),
mongoStores.orgMembers.cursor(),
Expand Down
18 changes: 15 additions & 3 deletions src/slack/utils.ts
@@ -1,3 +1,4 @@
import type { ReviewflowPr } from 'src/mongo';
import type { RepoContext } from '../context/repoContext';
import type { CommitFromRestEndpoint } from '../events/commit-handlers/utils/fetchCommit';
import type {
Expand Down Expand Up @@ -29,15 +30,26 @@ export const createPrLink = (
);
};

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

export const createPrChangesInformationFromReviewflowPr = (
pr: ReviewflowPr,
): string | null => {
if (!pr.changesInformation) return null;
return `${pr.changesInformation.changedFiles} file${
pr.changesInformation.changedFiles > 1 ? 's' : ''
} changed (+${pr.changesInformation.additions} -${
pr.changesInformation.deletions
})`;
};

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

0 comments on commit ddb3892

Please sign in to comment.