Skip to content

Commit 6310d0a

Browse files
committed
Fix error handling for CheckStalePrs and include a check for if a PR has been reviewed.
1 parent ffcb782 commit 6310d0a

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

internal/github/checkStalePRs.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"time"
78

@@ -24,36 +25,44 @@ func CheckStalePRs(githubClient *github.Client, internalTeam string, cfg config.
2425
}
2526

2627
for _, pr := range communityPRs {
27-
if isStale(githubClient, pr, teamMembers, cutoffDate) {
28-
stalePRUrls = append(stalePRUrls, pr.GetHTMLURL()) // Collecting URLs instead of PR objects
28+
stale, err := isStale(githubClient, pr, teamMembers, cutoffDate) // Handle both returned values
29+
if err != nil {
30+
log.Printf("Error checking if PR is stale: %v", err) // Log or handle the error
31+
continue // Skip this PR or handle the error appropriately
32+
}
33+
if stale {
34+
stalePRUrls = append(stalePRUrls, pr.GetHTMLURL()) // Append if PR is confirmed stale
2935
}
3036
}
3137
return stalePRUrls, nil
3238
}
3339

3440
// Checks if a PR is stale based on the last update from team members
35-
func isStale(githubClient *github.Client, pr *github.PullRequest, teamMembers map[string]bool, cutoffDate time.Time) bool {
36-
// Retrieve the timeline for the PR to find the latest relevant event
41+
func isStale(githubClient *github.Client, pr *github.PullRequest, teamMembers map[string]bool, cutoffDate time.Time) (bool, error) {
42+
// Set up a context with a timeout to control all operations within this function
43+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
44+
defer cancel() // Ensure resources are cleaned up correctly after the function exits
45+
3746
listOptions := &github.ListOptions{PerPage: 100}
3847
for {
39-
// Note: As far as the GitHub API is concerned, every pull request is an issue,
40-
// but not every issue is a pull request.
41-
events, resp, err := githubClient.Issues.ListIssueTimeline(context.TODO(), pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber(), listOptions)
48+
events, resp, err := githubClient.Issues.ListIssueTimeline(ctx, pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber(), listOptions)
4249
if err != nil {
43-
log.Printf("Failed to get timeline for PR #%d: %v", pr.GetNumber(), err)
44-
break // Skip to next PR on error
50+
return false, fmt.Errorf("failed to get timeline for PR #%d: %w", pr.GetNumber(), err)
4551
}
4652
for _, event := range events {
47-
if event.Event != nil && *event.Event == "commented" && teamMembers[*event.Actor.Login] && event.CreatedAt.After(cutoffDate) {
48-
return false // PR has been updated by a team member recently
53+
if event.Event == nil || event.Actor == nil || event.Actor.Login == nil {
54+
continue
55+
}
56+
if (*event.Event == "commented" || *event.Event == "reviewed") && teamMembers[*event.Actor.Login] && event.CreatedAt.After(cutoffDate) {
57+
return false, nil
4958
}
5059
}
5160
if resp.NextPage == 0 {
5261
break
5362
}
5463
listOptions.Page = resp.NextPage
5564
}
56-
return true // No recent updates by team members
65+
return true, nil
5766
}
5867

5968
// Take the list of PRs and send a message to a keybase channel

0 commit comments

Comments
 (0)