Skip to content

Commit 31f8e05

Browse files
committed
refactor the commenting code
1 parent b2ff62f commit 31f8e05

File tree

1 file changed

+87
-44
lines changed

1 file changed

+87
-44
lines changed

main.go

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -215,51 +215,13 @@ func (a *App) processApprovalsAndReviewers() (bool, string, error) {
215215
}
216216

217217
// Add comments to the PR if necessary
218-
if a.config.AddComments && len(unapprovedOwners) > 0 {
219-
// Comment on the PR with the codeowner teams that have not approved the PR
220-
comment := allRequiredOwners.ToCommentString()
221-
hasHighPriority, err := a.client.IsInLabels(a.conf.HighPriorityLabels)
222-
if err != nil {
223-
fmt.Fprintf(WarningBuffer, "WARNING: Error checking high priority labels: %v\n", err)
224-
} else if hasHighPriority {
225-
comment = "❗High Prio❗\n\n" + comment
226-
}
227-
if maxReviewsMet {
228-
comment += "\n\n"
229-
comment += "The PR has received the max number of required reviews. No further action is required."
230-
}
231-
fiveDaysAgo := time.Now().AddDate(0, 0, -5)
232-
found, err := a.client.IsInComments(comment, &fiveDaysAgo)
233-
if err != nil {
234-
return false, message, fmt.Errorf("IsInComments Error: %v\n", err)
235-
}
236-
if !found {
237-
err = a.client.AddComment(comment)
238-
if err != nil {
239-
return false, message, fmt.Errorf("AddComment Error: %v\n", err)
240-
}
241-
}
218+
err = a.addReviewStatusComment(allRequiredOwners, unapprovedOwners, maxReviewsMet)
219+
if err != nil {
220+
return false, message, fmt.Errorf("failed to add review status comment: %w", err)
242221
}
243-
if a.config.AddComments && len(allOptionalReviewerNames) > 0 {
244-
var isInCommentsError error = nil
245-
// Add CC comment to the PR with the optional reviewers that have not already been mentioned in the PR comments
246-
viewersToPing := f.Filtered(allOptionalReviewerNames, func(name string) bool {
247-
found, err := a.client.IsSubstringInComments(name, nil)
248-
if err != nil {
249-
isInCommentsError = err
250-
}
251-
return !found
252-
})
253-
if isInCommentsError != nil {
254-
return false, message, fmt.Errorf("IsInComments Error: %v\n", err)
255-
}
256-
if len(viewersToPing) > 0 {
257-
comment := fmt.Sprintf("cc %s", strings.Join(viewersToPing, " "))
258-
err = a.client.AddComment(comment)
259-
if err != nil {
260-
return false, message, fmt.Errorf("AddComment Error: %v\n", err)
261-
}
262-
}
222+
err = a.addOptionalCcComment(allOptionalReviewerNames)
223+
if err != nil {
224+
return false, message, fmt.Errorf("failed to add optional CC comment: %w", err)
263225
}
264226

265227
// Exit if there are any unapproved codeowner teams
@@ -297,6 +259,87 @@ func (a *App) processApprovalsAndReviewers() (bool, string, error) {
297259
return true, message, nil
298260
}
299261

262+
func (a *App) addReviewStatusComment(allRequiredOwners, unapprovedOwners codeowners.ReviewerGroups, maxReviewsMet bool) error {
263+
// Comment on the PR with the codeowner teams that have not approved the PR
264+
265+
if !a.config.AddComments || len(unapprovedOwners) == 0 {
266+
printDebug("Skipping review status comment (disabled or no unapproved owners).\n")
267+
return nil
268+
}
269+
270+
comment := allRequiredOwners.ToCommentString()
271+
hasHighPriority, err := a.client.IsInLabels(a.conf.HighPriorityLabels)
272+
if err != nil {
273+
printWarning("WARNING: Error checking high priority labels: %v\n", err)
274+
} else if hasHighPriority {
275+
comment = "❗High Prio❗\n\n" + comment
276+
}
277+
278+
if maxReviewsMet {
279+
comment += "\n\nThe PR has received the max number of required reviews. No further action is required."
280+
}
281+
282+
fiveDaysAgo := time.Now().AddDate(0, 0, -5)
283+
found, err := a.client.IsInComments(comment, &fiveDaysAgo)
284+
if err != nil {
285+
return fmt.Errorf("IsInComments Error: %v\n", err)
286+
}
287+
288+
// Add the comment if it wasn't found recently
289+
if !found {
290+
printDebug("Adding review status comment: %q\n", comment)
291+
err = a.client.AddComment(comment)
292+
if err != nil {
293+
return fmt.Errorf("AddComment Error: %v\n", err)
294+
}
295+
} else {
296+
printDebug("Similar review status comment already exists.\n")
297+
}
298+
299+
return nil
300+
}
301+
302+
func (a *App) addOptionalCcComment(allOptionalReviewerNames []string) error {
303+
// Add CC comment to the PR with the optional reviewers that have not already been mentioned in the PR comments
304+
305+
if !a.config.AddComments || len(allOptionalReviewerNames) == 0 {
306+
printDebug("Skipping optional CC comment (disabled or no optional reviewers).\n")
307+
return nil
308+
}
309+
310+
var isInCommentsError error
311+
viewersToPing := f.Filtered(allOptionalReviewerNames, func(name string) bool {
312+
if isInCommentsError != nil {
313+
return false
314+
}
315+
found, err := a.client.IsSubstringInComments(name, nil)
316+
if err != nil {
317+
printWarning("WARNING: Error checking comments for substring '%s': %v\n", name, err)
318+
isInCommentsError = err
319+
return false
320+
}
321+
return !found
322+
})
323+
324+
if isInCommentsError != nil {
325+
return fmt.Errorf("IsInComments Error: %v\n", isInCommentsError)
326+
}
327+
328+
// Add the CC comment if there are any viewers to ping
329+
if len(viewersToPing) > 0 {
330+
comment := fmt.Sprintf("cc %s", strings.Join(viewersToPing, " "))
331+
printDebug("Adding CC comment: %q\n", comment)
332+
err := a.client.AddComment(comment)
333+
if err != nil {
334+
return fmt.Errorf("AddComment Error: %v\n", err)
335+
}
336+
} else {
337+
printDebug("No new optional reviewers to CC.\n")
338+
}
339+
340+
return nil
341+
}
342+
300343
func (a *App) processTokenOwnerApproval() (*gh.CurrentApproval, error) {
301344
tokenOwner, err := a.client.GetTokenUser()
302345
if err != nil {

0 commit comments

Comments
 (0)