@@ -2,6 +2,7 @@ package github
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"log"
6
7
"time"
7
8
@@ -24,36 +25,44 @@ func CheckStalePRs(githubClient *github.Client, internalTeam string, cfg config.
24
25
}
25
26
26
27
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
29
35
}
30
36
}
31
37
return stalePRUrls , nil
32
38
}
33
39
34
40
// 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
+
37
46
listOptions := & github.ListOptions {PerPage : 100 }
38
47
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 )
42
49
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 )
45
51
}
46
52
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
49
58
}
50
59
}
51
60
if resp .NextPage == 0 {
52
61
break
53
62
}
54
63
listOptions .Page = resp .NextPage
55
64
}
56
- return true // No recent updates by team members
65
+ return true , nil
57
66
}
58
67
59
68
// Take the list of PRs and send a message to a keybase channel
0 commit comments