Skip to content

Commit

Permalink
Merge pull request #2 from Crazybus/fail
Browse files Browse the repository at this point in the history
Print failed target URLs when status checks fail
  • Loading branch information
Crazybus authored Mar 11, 2019
2 parents e7b814f + 5318b7a commit c866046
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions pratus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/oauth2"
)

func getPRState(owner string, repo string, number int) (state string, err error) {
func getPRState(owner string, repo string, number int) (state string, statuses []github.RepoStatus, err error) {

ctx := context.Background()
ts := oauth2.StaticTokenSource(
Expand All @@ -23,17 +23,37 @@ func getPRState(owner string, repo string, number int) (state string, err error)

pr, _, err := g.PullRequests.Get(ctx, owner, repo, number)
if err != nil {
return "", err
return "", nil, err
}

commit := pr.GetHead().GetSHA()

status, _, err := g.Repositories.GetCombinedStatus(ctx, owner, repo, commit, nil)
if err != nil {
return "", err
return "", nil, err
}

return status.GetState(), nil
state = status.GetState()
statuses = status.Statuses
return state, statuses, nil
}

func stillPending(statuses []github.RepoStatus) (pending bool) {
for _, status := range statuses {
if status.GetState() == "pending" {
return true
}
}
return false
}

func getFailedURLs(statuses []github.RepoStatus) (failed []string) {
for _, status := range statuses {
if status.GetState() != "success" {
failed = append(failed, status.GetTargetURL())
}
}
return failed
}

func parseGitHubURL(baseURL string, URL string) (owner string, repo string, number int) {
Expand Down Expand Up @@ -64,17 +84,31 @@ func main() {

for {

state, err := getPRState(owner, repo, number)
state, statuses, err := getPRState(owner, repo, number)
if err != nil {
print(err)
continue
}

if state != "pending" {
fmt.Println("\nPR finished with state: " + state)
os.Exit(0)
if stillPending(statuses) {
fmt.Print(".")
time.Sleep(sleepTimer)
continue
}

fmt.Print(".")
time.Sleep(sleepTimer)
failedURLs := getFailedURLs(statuses)

switch state {
case "success":
fmt.Println("\nPR succeeded :)")
os.Exit(0)
case "failure", "error":
fmt.Println("\nPR failed :( Failed URLs:")
fmt.Println(strings.Join(failedURLs, "\n"))
os.Exit(1)
default:
fmt.Printf("\nPR contained an unknown status: %q", state)
os.Exit(1)
}
}
}

0 comments on commit c866046

Please sign in to comment.