]> khamidou — GitHub mirrors - prguy.git/commitdiff
Get accurate PR status
authorKarim Hamidou <karim@khamidou.com>
Thu, 4 Jul 2024 23:14:02 +0000 (16:14 -0700)
committerKarim Hamidou <karim@khamidou.com>
Thu, 4 Jul 2024 23:14:02 +0000 (16:14 -0700)
cmd/prguy/main.go
cmd/prguy/repos.go

index 6312403b589891cb0bc3f7d79a2815ef5da52d17..da648136d45477a21f9e4df995f1a1b48420f345 100644 (file)
@@ -62,6 +62,7 @@ func setupMenu(ctx context.Context, cancel context.CancelFunc) {
 
                        cfg.load()
                        systray.ResetMenu()
+                       systray.AddMenuItem("Fetching PRs from Github...", "")
 
                        fmt.Println("Fetching PRs...")
                        myPRs, otherPRs, err := listUserPRs(cfg.OAuthToken)
@@ -71,18 +72,29 @@ func setupMenu(ctx context.Context, cancel context.CancelFunc) {
                                return
                        }
 
+                       systray.ResetMenu()
+
                        if len(myPRs) == 0 {
                                systray.AddMenuItem("No PRs out from you, let's get after it!", "")
                        } else {
                                for _, pr := range myPRs {
                                        var pr_status string
+                                       var build_status string
                                        if pr.mergeable {
                                                pr_status = "✅"
                                        } else {
                                                pr_status = "❌"
                                        }
 
-                                       title := fmt.Sprintf("%-*s %s", 50, pr.title, pr_status)
+                                       if pr.buildStatus == buildSuccess {
+                                               build_status = "🟢"
+                                       } else if pr.buildStatus == buildFailure {
+                                               build_status = "🔴"
+                                       } else {
+                                               build_status = "🔵"
+                                       }
+
+                                       title := fmt.Sprintf("%-*s %s%s", 50, pr.title, build_status, pr_status)
                                        channel := systray.AddMenuItem(title, "")
                                        channels = append(channels, channel.ClickedCh)
                                        channelsMap[channel.ClickedCh] = pr
index d97780eb068755225e970a80083faa386916713b..3ecacc5d0bf074201a37a8fa2b7ee3ca2ad091cf 100644 (file)
@@ -13,16 +13,17 @@ type buildStatus int
 var errNoAccessToPR = errors.New("No access to PR")
 
 const (
-       buildPending = iota
+       buildPending buildStatus = iota
        buildSuccess
        buildFailure
        buildCanceled
 )
 
 type pullRequest struct {
-       url       string
-       title     string
-       mergeable bool
+       url         string
+       title       string
+       mergeable   bool
+       buildStatus buildStatus
 }
 
 func listUserPRs(token string) ([]pullRequest, []pullRequest, error) {
@@ -71,7 +72,6 @@ func listUserPRs(token string) ([]pullRequest, []pullRequest, error) {
 
                subject := pr["subject"].(map[string]interface{})
                prAPIUrl := subject["url"].(string)
-               fmt.Println("fetching one PR", subject["title"].(string))
 
                prData, err := fetchOneRESTObject(prAPIUrl, token)
                if err == errNoAccessToPR {
@@ -81,23 +81,49 @@ func listUserPRs(token string) ([]pullRequest, []pullRequest, error) {
                        return nil, nil, err
                }
 
+               var prSHA string
+               var repoFullName string
+               prStatus := buildPending
+               if _, ok := prData["head"]; ok {
+                       headInfo := prData["head"].(map[string]interface{})
+                       if _, ok := headInfo["sha"]; ok {
+                               prSHA = headInfo["sha"].(string)
+                       }
+
+                       if _, ok := headInfo["repo"]; ok {
+                               repoInfo := headInfo["repo"].(map[string]interface{})
+                               repoFullName = repoInfo["full_name"].(string)
+                       }
+               }
+
+               if prSHA != "" && repoFullName != "" {
+                       prStatus, err = getBuildStatus(repoFullName, prSHA, token)
+                       if err != nil {
+                               prStatus = buildPending
+                       }
+               }
+
                prUrl := prData["_links"].(map[string]interface{})["html"].(map[string]interface{})["href"].(string)
                if _, ok := PRsSeen[prUrl]; ok {
                        continue
                }
 
                PRsSeen[prUrl] = true
+               mergeableState := prData["mergeable_state"].(string)
+               mergeable := mergeableState == "clean" || mergeableState == "has_hooks"
                if reason == "author" {
                        myPRs = append(myPRs, pullRequest{
-                               url:       prUrl,
-                               title:     subject["title"].(string),
-                               mergeable: prData["mergeable"].(bool),
+                               url:         prUrl,
+                               title:       subject["title"].(string),
+                               mergeable:   mergeable,
+                               buildStatus: prStatus,
                        })
                } else {
                        otherPRs = append(otherPRs, pullRequest{
-                               url:       prUrl,
-                               title:     subject["title"].(string),
-                               mergeable: prData["mergeable"].(bool),
+                               url:         prUrl,
+                               title:       subject["title"].(string),
+                               mergeable:   mergeable,
+                               buildStatus: prStatus,
                        })
                }
        }
@@ -105,6 +131,54 @@ func listUserPRs(token string) ([]pullRequest, []pullRequest, error) {
        return myPRs, otherPRs, nil
 }
 
+func getApprovalStatus(prUrl string, token string) (bool, error) {
+       return false, nil
+}
+
+func getBuildStatus(repoFullName string, sha string, token string) (buildStatus, error) {
+       possibleUrls := []string{
+               fmt.Sprintf("https://api.github.com/repos/%s/commits/%s/check-runs",
+                       repoFullName, sha),
+               fmt.Sprintf("https://api.github.com/repos/%s/commits/%s/status",
+                       repoFullName, sha),
+       }
+
+       // There's two different APIs for statuses, so we try both
+       for _, url := range possibleUrls {
+               resp, err := fetchOneRESTObject(url, token)
+               if err != nil {
+                       continue
+               }
+
+               if _, ok := resp["check_runs"]; ok {
+                       for _, checkRun := range resp["check_runs"].([]interface{}) {
+                               checkRunMap := checkRun.(map[string]interface{})
+                               if checkRunMap["status"].(string) == "completed" {
+                                       if checkRunMap["conclusion"].(string) == "success" {
+                                               return buildSuccess, nil
+                                       } else {
+                                               return buildFailure, nil
+                                       }
+                               }
+                       }
+               } else if _, ok := resp["state"]; ok {
+                       state := resp["state"].(string)
+                       switch state {
+                       case "success":
+                               return buildSuccess, nil
+                       case "failure":
+                               return buildFailure, nil
+                       case "pending":
+                               continue
+                       case "error":
+                               return buildFailure, nil
+                       }
+               }
+       }
+
+       return buildPending, errors.New("Unknown build status")
+}
+
 func fetchOneRESTObject(url string, token string) (map[string]interface{}, error) {
        req, err := http.NewRequest("GET", url, nil)
        if err != nil {