Merge branch 'checks-crash'

This commit is contained in:
Mislav Marohnić 2019-11-20 11:26:16 +01:00
commit c7a38b6331
2 changed files with 49 additions and 3 deletions

39
api/pull_request_test.go Normal file
View file

@ -0,0 +1,39 @@
package api
import (
"encoding/json"
"testing"
)
func TestPullRequest_ChecksStatus(t *testing.T) {
pr := PullRequest{}
payload := `
{ "commits": { "nodes": [{ "commit": {
"statusCheckRollup": {
"contexts": {
"nodes": [
{ "state": "SUCCESS" },
{ "state": "PENDING" },
{ "state": "FAILURE" },
{ "status": "IN_PROGRESS",
"conclusion": null },
{ "status": "COMPLETED",
"conclusion": "SUCCESS" },
{ "status": "COMPLETED",
"conclusion": "FAILURE" },
{ "status": "COMPLETED",
"conclusion": "ACTION_REQUIRED" }
]
}
}
} }] } }
`
err := json.Unmarshal([]byte(payload), &pr)
eq(t, err, nil)
checks := pr.ChecksStatus()
eq(t, checks.Total, 7)
eq(t, checks.Pending, 2)
eq(t, checks.Failing, 3)
eq(t, checks.Passing, 2)
}

View file

@ -38,6 +38,7 @@ type PullRequest struct {
Contexts struct {
Nodes []struct {
State string
Status string
Conclusion string
}
}
@ -86,16 +87,21 @@ func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) {
}
commit := pr.Commits.Nodes[0].Commit
for _, c := range commit.StatusCheckRollup.Contexts.Nodes {
state := c.State
state := c.State // StatusContext
if state == "" {
state = c.Conclusion
// CheckRun
if c.Status == "COMPLETED" {
state = c.Conclusion
} else {
state = c.Status
}
}
switch state {
case "SUCCESS", "NEUTRAL", "SKIPPED":
summary.Passing++
case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED":
summary.Failing++
case "EXPECTED", "QUEUED", "PENDING", "IN_PROGRESS":
case "EXPECTED", "REQUESTED", "QUEUED", "PENDING", "IN_PROGRESS":
summary.Pending++
default:
panic(fmt.Errorf("unsupported status: %q", state))
@ -150,6 +156,7 @@ func PullRequests(client *Client, ghRepo Repo, currentBranch, currentUsername st
state
}
...on CheckRun {
status
conclusion
}
}