From 7c731bc51289358c0cb14fb06087bcf2c5ec9d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 19 Nov 2019 09:38:06 +0100 Subject: [PATCH] Avoid crash when parsing in-progress CheckRuns Fixes `panic: unsupported status: ""` This occurs when a CheckRun has status "IN_PROGRESS" (or any other than "COMPLETED") and when its `conclusion` would be null. I previously didn't account for this. This adds support for parsing state of an in-progress CheckRun. --- api/pull_request_test.go | 39 +++++++++++++++++++++++++++++++++++++++ api/queries.go | 13 ++++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 api/pull_request_test.go diff --git a/api/pull_request_test.go b/api/pull_request_test.go new file mode 100644 index 000000000..82386108d --- /dev/null +++ b/api/pull_request_test.go @@ -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) +} diff --git a/api/queries.go b/api/queries.go index 916e34153..2be64e46c 100644 --- a/api/queries.go +++ b/api/queries.go @@ -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 } }