-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathpullRequest.ts
More file actions
161 lines (140 loc) · 4.44 KB
/
pullRequest.ts
File metadata and controls
161 lines (140 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { FC } from 'react';
import type { OcticonProps } from '@primer/octicons-react';
import {
GitMergeIcon,
GitMergeQueueIcon,
GitPullRequestClosedIcon,
GitPullRequestDraftIcon,
GitPullRequestIcon,
} from '@primer/octicons-react';
import {
type GitifyNotification,
type GitifyPullRequestReview,
type GitifyPullRequestState,
type GitifySubject,
IconColor,
type Link,
type SettingsState,
} from '../../../types';
import { fetchPullByNumber } from '../../api/client';
import type {
PullRequestDetailsFragment,
PullRequestReviewFieldsFragment,
} from '../../api/graphql/generated/graphql';
import { formatGitHubNumber } from '../formatters';
import { DefaultHandler, defaultHandler } from './default';
import { getNotificationAuthor } from './utils';
class PullRequestHandler extends DefaultHandler {
override readonly supportsMergedQueryEnrichment = true;
override async enrich(
notification: GitifyNotification,
_settings: SettingsState,
fetchedData?: PullRequestDetailsFragment,
): Promise<Partial<GitifySubject>> {
const pr =
fetchedData ??
(await fetchPullByNumber(notification)).repository?.pullRequest;
let prState: GitifyPullRequestState = pr.state;
if (pr.isDraft) {
prState = 'DRAFT';
} else if (pr.isInMergeQueue) {
prState = 'MERGE_QUEUE';
}
const prComment = pr.comments?.nodes[0];
const prUser = getNotificationAuthor([prComment?.author, pr.author]);
const reviews = getLatestReviewForReviewers(pr.reviews.nodes);
const prReactionCount =
prComment?.reactions.totalCount ?? pr.reactions.totalCount;
const prReactionGroup = prComment?.reactionGroups ?? pr.reactionGroups;
return {
number: pr.number,
state: prState,
user: prUser,
reviews: reviews,
commentCount: pr.comments.totalCount,
labels:
pr.labels?.nodes.map((label) => ({
name: label.name,
color: label.color,
})) ?? [],
linkedIssues: pr.closingIssuesReferences?.nodes.map((issue) =>
formatGitHubNumber(issue.number),
),
milestone: pr.milestone,
htmlUrl: prComment?.url ?? pr.url,
reactionsCount: prReactionCount,
reactionGroups: prReactionGroup,
};
}
override iconType(notification: GitifyNotification): FC<OcticonProps> {
switch (notification.subject.state as GitifyPullRequestState) {
case 'DRAFT':
return GitPullRequestDraftIcon;
case 'CLOSED':
return GitPullRequestClosedIcon;
case 'MERGE_QUEUE':
return GitMergeQueueIcon;
case 'MERGED':
return GitMergeIcon;
default:
return GitPullRequestIcon;
}
}
override iconColor(notification: GitifyNotification): IconColor {
switch (notification.subject.state as GitifyPullRequestState) {
case 'OPEN':
return IconColor.GREEN;
case 'CLOSED':
return IconColor.RED;
case 'MERGE_QUEUE':
return IconColor.YELLOW;
case 'MERGED':
return IconColor.PURPLE;
default:
return defaultHandler.iconColor(notification);
}
}
override defaultUrl(notification: GitifyNotification): Link {
const url = new URL(defaultHandler.defaultUrl(notification));
url.pathname += '/pulls';
return url.href as Link;
}
}
export const pullRequestHandler = new PullRequestHandler();
export function getLatestReviewForReviewers(
reviews: PullRequestReviewFieldsFragment[],
): GitifyPullRequestReview[] {
if (!reviews.length) {
return null;
}
// Find the most recent review for each reviewer
const latestReviews: PullRequestReviewFieldsFragment[] = [];
const sortedReviews = reviews.toReversed();
for (const prReview of sortedReviews) {
const reviewerFound = latestReviews.find(
(review) => review.author.login === prReview.author.login,
);
if (!reviewerFound) {
latestReviews.push(prReview);
}
}
// Group by the review state
const reviewers: GitifyPullRequestReview[] = [];
for (const prReview of latestReviews) {
const reviewerFound = reviewers.find(
(review) => review.state === prReview.state,
);
if (reviewerFound) {
reviewerFound.users.push(prReview.author.login);
} else {
reviewers.push({
state: prReview.state,
users: [prReview.author.login],
});
}
}
// Sort reviews by state for consistent order when rendering
return reviewers.sort((a, b) => {
return a.state.localeCompare(b.state);
});
}