From: Karim Hamidou Date: Thu, 4 Jul 2024 23:14:02 +0000 (-0700) Subject: Get accurate PR status X-Git-Tag: 0.5~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=006c8738fd0ba3036eb6b71f3873513f44da222d;p=prguy.git Get accurate PR status --- diff --git a/cmd/prguy/main.go b/cmd/prguy/main.go index 6312403..da64813 100644 --- a/cmd/prguy/main.go +++ b/cmd/prguy/main.go @@ -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 diff --git a/cmd/prguy/repos.go b/cmd/prguy/repos.go index d97780e..3ecacc5 100644 --- a/cmd/prguy/repos.go +++ b/cmd/prguy/repos.go @@ -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 {