@@ -67,6 +67,70 @@ export function nextProjectPullRequestStatusCreatedAt(pullRequest, now) {
6767 return Math . max ( now , ( pullRequest . statusCreatedAt ?? 0 ) + 1 ) ;
6868}
6969
70+ /** Keep consecutive decisions ordered across whole-second Nostr timestamps. */
71+ export function nextProjectPullRequestReviewCreatedAt ( pullRequest , now ) {
72+ const latestDecisionCreatedAt = [
73+ ...pullRequest . approvals ,
74+ ...pullRequest . changeRequests ,
75+ ] . reduce ( ( latest , decision ) => Math . max ( latest , decision . createdAt ) , 0 ) ;
76+ return Math . max ( now , latestDecisionCreatedAt + 1 ) ;
77+ }
78+
79+ /** Trusted presentation kind for a compact review timeline row. */
80+ export function projectPullRequestCommentTimelineKind ( comment ) {
81+ if ( comment . isTrustedReviewRequest ) return "review-request" ;
82+ if (
83+ ! comment . isTrustedReviewDecision ||
84+ ! comment . reviewDecisionStatus ||
85+ ! comment . reviewDecision
86+ ) {
87+ return null ;
88+ }
89+ return comment . reviewDecision ;
90+ }
91+
92+ /** Effective review summary shown above the PR review actions. */
93+ export function projectPullRequestReviewSummary ( pullRequest ) {
94+ const approvalCount = pullRequest . approvals . length ;
95+ const changeRequestCount = pullRequest . changeRequests . length ;
96+ const isDraft = pullRequest . status === "Draft" ;
97+ const state = isDraft
98+ ? "This pull request is still a work in progress."
99+ : changeRequestCount > 0
100+ ? `${ changeRequestCount } reviewer${ changeRequestCount === 1 ? "" : "s" } requested changes.`
101+ : pullRequest . reviewers . length > 0
102+ ? "Review requested — no approvals yet."
103+ : "No reviews yet." ;
104+
105+ return {
106+ approvalCount,
107+ changeRequestCount,
108+ detail : isDraft
109+ ? "Draft pull requests cannot be merged."
110+ : approvalCount === 0 && changeRequestCount === 0
111+ ? "Approvals from reviewers will show up here."
112+ : null ,
113+ showState : approvalCount === 0 || changeRequestCount > 0 ,
114+ state,
115+ } ;
116+ }
117+
118+ /** Effective trusted, current-commit review decision represented by a comment. */
119+ export function projectPullRequestEffectiveReviewDecision (
120+ pullRequest ,
121+ comment ,
122+ ) {
123+ if ( pullRequest . approvals . some ( ( decision ) => decision . id === comment . id ) ) {
124+ return "approved" ;
125+ }
126+ if (
127+ pullRequest . changeRequests . some ( ( decision ) => decision . id === comment . id )
128+ ) {
129+ return "changes-requested" ;
130+ }
131+ return null ;
132+ }
133+
70134function eventToPullRequestUpdate ( event ) {
71135 return {
72136 id : event . id ,
@@ -83,17 +147,28 @@ function eventToPullRequestUpdate(event) {
83147// for any client (including `buzz` CLI users) that treats them as comments.
84148export const PR_REVIEW_REQUEST_LABEL = "review-request" ;
85149export const PR_APPROVAL_LABEL = "approval" ;
150+ export const PR_CHANGES_REQUESTED_LABEL = "changes-requested" ;
86151
87152function eventToPullRequestComment ( event ) {
88153 const labels = getAllTags ( event , "t" ) . map ( ( label ) => label . toLowerCase ( ) ) ;
89154 const isReviewRequest = labels . includes ( PR_REVIEW_REQUEST_LABEL ) ;
155+ const isApproval = labels . includes ( PR_APPROVAL_LABEL ) ;
156+ const isChangeRequest = labels . includes ( PR_CHANGES_REQUESTED_LABEL ) ;
90157 return {
91158 id : event . id ,
92159 content : event . content ,
93160 author : event . pubkey ,
94161 createdAt : event . created_at ,
95- isApproval : labels . includes ( PR_APPROVAL_LABEL ) ,
162+ commit : getTag ( event , "c" ) ?? null ,
163+ isApproval,
164+ isChangeRequest,
96165 isReviewRequest,
166+ reviewDecision :
167+ isApproval === isChangeRequest
168+ ? null
169+ : isApproval
170+ ? "approved"
171+ : "changes-requested" ,
97172 // For review requests the `p` tags are the requested reviewers.
98173 reviewerPubkeys : isReviewRequest
99174 ? getAllTags ( event , "p" ) . map ( ( pubkey ) => pubkey . toLowerCase ( ) )
@@ -125,27 +200,62 @@ function reviewersForPullRequest(pullRequest, comments) {
125200 return [ ...reviewers ] ;
126201}
127202
128- /** Latest trusted approval per author, oldest first. */
129- function approvalsForPullRequest ( pullRequest , comments , reviewers ) {
203+ function reviewDecisionCommit ( comment , initialCommit ) {
204+ return comment . commit ?? initialCommit ;
205+ }
206+
207+ function trustedReviewActors ( pullRequest , reviewers ) {
130208 const author = pullRequest . pubkey . toLowerCase ( ) ;
131- const trustedApprovers = new Set ( reviewers ) ;
209+ const trustedActors = new Set ( reviewers ) ;
132210 for ( const actor of allowedActorsForRoot ( pullRequest ) ) {
133- if ( actor !== author ) trustedApprovers . add ( actor ) ;
211+ if ( actor !== author ) trustedActors . add ( actor ) ;
134212 }
213+ return trustedActors ;
214+ }
135215
216+ /** Latest trusted, current-commit review decision per author. */
217+ function reviewDecisionsForPullRequest (
218+ comments ,
219+ trustedActors ,
220+ initialCommit ,
221+ currentCommit ,
222+ ) {
136223 const byAuthor = new Map ( ) ;
137224 for ( const comment of comments ) {
138- if ( ! comment . isApproval ) continue ;
225+ if ( ! comment . reviewDecision || ! currentCommit ) continue ;
139226 const key = comment . author . toLowerCase ( ) ;
140- if ( ! trustedApprovers . has ( key ) ) continue ;
227+ if ( ! trustedActors . has ( key ) ) continue ;
228+ const commit = reviewDecisionCommit ( comment , initialCommit ) ;
229+ if ( commit !== currentCommit ) continue ;
141230 const existing = byAuthor . get ( key ) ;
142- if ( ! existing || comment . createdAt > existing . createdAt ) {
143- byAuthor . set ( key , comment ) ;
231+ if (
232+ ! existing ||
233+ comment . createdAt > existing . createdAt ||
234+ ( comment . createdAt === existing . createdAt && comment . id > existing . id )
235+ ) {
236+ byAuthor . set ( key , { ...comment , commit } ) ;
144237 }
145238 }
146- return [ ...byAuthor . values ( ) ]
147- . map ( ( { id, author, createdAt } ) => ( { id, author, createdAt } ) )
148- . sort ( ( left , right ) => left . createdAt - right . createdAt ) ;
239+ const decisions = [ ...byAuthor . values ( ) ]
240+ . map ( ( { id, author : reviewer , createdAt, commit, reviewDecision } ) => ( {
241+ id,
242+ author : reviewer ,
243+ createdAt,
244+ commit,
245+ reviewDecision,
246+ } ) )
247+ . sort (
248+ ( left , right ) =>
249+ left . createdAt - right . createdAt || left . id . localeCompare ( right . id ) ,
250+ ) ;
251+ return {
252+ approvals : decisions . filter (
253+ ( decision ) => decision . reviewDecision === "approved" ,
254+ ) ,
255+ changeRequests : decisions . filter (
256+ ( decision ) => decision . reviewDecision === "changes-requested" ,
257+ ) ,
258+ } ;
149259}
150260
151261export function eventToProjectPullRequest (
@@ -160,17 +270,41 @@ export function eventToProjectPullRequest(
160270 pullRequest . id ,
161271 trustedUpdatesForPullRequest ( pullRequest , updateEvents ) ,
162272 ) . map ( eventToPullRequestUpdate ) ;
163- const comments = eventsForPullRequest ( pullRequest . id , commentEvents ) . map (
164- eventToPullRequestComment ,
165- ) ;
166- const reviewers = reviewersForPullRequest ( pullRequest , comments ) ;
167- const approvals = approvalsForPullRequest ( pullRequest , comments , reviewers ) ;
273+ const parsedComments = eventsForPullRequest (
274+ pullRequest . id ,
275+ commentEvents ,
276+ ) . map ( eventToPullRequestComment ) ;
277+ const reviewers = reviewersForPullRequest ( pullRequest , parsedComments ) ;
278+ const trustedActors = trustedReviewActors ( pullRequest , reviewers ) ;
279+ const trustedReviewRequestActors = allowedActorsForRoot ( pullRequest ) ;
280+ const latestCommit = getTag ( latestUpdate ?? pullRequest , "c" ) ?? null ;
281+ const initialCommit = getTag ( pullRequest , "c" ) ?? null ;
282+ const comments = parsedComments . map ( ( comment ) => ( {
283+ ...comment ,
284+ isTrustedReviewDecision :
285+ Boolean ( comment . reviewDecision ) &&
286+ trustedActors . has ( comment . author . toLowerCase ( ) ) ,
287+ reviewDecisionStatus :
288+ comment . reviewDecision && trustedActors . has ( comment . author . toLowerCase ( ) )
289+ ? latestCommit &&
290+ reviewDecisionCommit ( comment , initialCommit ) === latestCommit
291+ ? "current"
292+ : "historical"
293+ : null ,
294+ isTrustedReviewRequest :
295+ comment . isReviewRequest &&
296+ trustedReviewRequestActors . has ( comment . author . toLowerCase ( ) ) ,
297+ } ) ) ;
168298 const title =
169299 getTag ( pullRequest , "subject" ) ||
170300 pullRequest . content . split ( "\n" ) [ 0 ] ||
171301 "Untitled pull request" ;
172- const latestCommit = getTag ( latestUpdate ?? pullRequest , "c" ) ?? null ;
173- const initialCommit = getTag ( pullRequest , "c" ) ?? null ;
302+ const reviewDecisions = reviewDecisionsForPullRequest (
303+ comments ,
304+ trustedActors ,
305+ initialCommit ,
306+ latestCommit ,
307+ ) ;
174308
175309 return {
176310 id : pullRequest . id ,
@@ -182,7 +316,8 @@ export function eventToProjectPullRequest(
182316 labels : getAllTags ( pullRequest , "t" ) ,
183317 recipients : getAllTags ( pullRequest , "p" ) ,
184318 reviewers,
185- approvals,
319+ approvals : reviewDecisions . approvals ,
320+ changeRequests : reviewDecisions . changeRequests ,
186321 status : statusFromEvent ( pullRequest , latestStatus ) ,
187322 statusEventId : latestStatus ?. id ?? null ,
188323 statusCreatedAt : latestStatus ?. created_at ?? null ,
0 commit comments