From fad5b44456ec8ba9c04161449d8ca37ab1a37ae9 Mon Sep 17 00:00:00 2001 From: Billy Lynch Date: Wed, 23 Oct 2019 11:44:19 -0400 Subject: [PATCH] Add JSON marshalling support for State codes. This converts JSON serialization of statuses into their string representation. e.g. Old: ``` { "state": 3, } ``` New: ``` { "state": "success", } ``` --- scm/const.go | 9 +++++++++ scm/driver/bitbucket/testdata/status.json.golden | 2 +- scm/driver/bitbucket/testdata/statuses.json.golden | 2 +- scm/driver/gitea/testdata/status.json.golden | 2 +- scm/driver/gitea/testdata/statuses.json.golden | 2 +- scm/driver/github/testdata/combined_status.json.golden | 6 +++--- scm/driver/github/testdata/status.json.golden | 2 +- scm/driver/github/testdata/statuses.json.golden | 2 +- scm/driver/gitlab/testdata/status.json.golden | 2 +- scm/driver/gitlab/testdata/statuses.json.golden | 2 +- .../stash/testdata/commit_build_status.json.golden | 4 ++-- 11 files changed, 22 insertions(+), 13 deletions(-) diff --git a/scm/const.go b/scm/const.go index 435f8ddf4..ca21858a7 100644 --- a/scm/const.go +++ b/scm/const.go @@ -70,6 +70,15 @@ func ToState(s string) State { } } +func (s State) MarshalJSON() ([]byte, error) { + return []byte(s.String()), nil +} + +func (s *State) UnmarshalJSON(b []byte) error { + *s = ToState(strings.Trim(string(b), `"`)) + return nil +} + // Action identifies webhook actions. type Action int diff --git a/scm/driver/bitbucket/testdata/status.json.golden b/scm/driver/bitbucket/testdata/status.json.golden index d95711928..876bd479d 100644 --- a/scm/driver/bitbucket/testdata/status.json.golden +++ b/scm/driver/bitbucket/testdata/status.json.golden @@ -1,5 +1,5 @@ { - "State": 3, + "State": "success", "Label": "drone", "Desc": "Build has completed successfully", "Target": "https://ci.example.com/1000/output" diff --git a/scm/driver/bitbucket/testdata/statuses.json.golden b/scm/driver/bitbucket/testdata/statuses.json.golden index 39abcccfb..de9c75b31 100644 --- a/scm/driver/bitbucket/testdata/statuses.json.golden +++ b/scm/driver/bitbucket/testdata/statuses.json.golden @@ -1,6 +1,6 @@ [ { - "State": 3, + "State": "success", "Label": "drone", "Desc": "Build has completed successfully", "Target": "https://ci.example.com/1000/output" diff --git a/scm/driver/gitea/testdata/status.json.golden b/scm/driver/gitea/testdata/status.json.golden index f3ac5e970..30ebd65aa 100644 --- a/scm/driver/gitea/testdata/status.json.golden +++ b/scm/driver/gitea/testdata/status.json.golden @@ -1,5 +1,5 @@ { - "State": 3, + "State": "success", "Label": "continuous-integration/drone", "Desc": "", "Target": "https://example.com" diff --git a/scm/driver/gitea/testdata/statuses.json.golden b/scm/driver/gitea/testdata/statuses.json.golden index fec7fb06a..1a1ee0c60 100644 --- a/scm/driver/gitea/testdata/statuses.json.golden +++ b/scm/driver/gitea/testdata/statuses.json.golden @@ -1,6 +1,6 @@ [ { - "State": 3, + "State": "success", "Label": "continuous-integration/drone", "Desc": "", "Target": "https://example.com" diff --git a/scm/driver/github/testdata/combined_status.json.golden b/scm/driver/github/testdata/combined_status.json.golden index 0d25e0c0e..11941c498 100644 --- a/scm/driver/github/testdata/combined_status.json.golden +++ b/scm/driver/github/testdata/combined_status.json.golden @@ -1,15 +1,15 @@ { - "State": 3, + "State": "success", "Sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", "Statuses": [ { - "State": 3, + "State": "success", "Label": "continuous-integration/jenkins", "Desc": "Build has completed successfully", "Target": "https://ci.example.com/1000/output" }, { - "State": 3, + "State": "success", "Label": "security/brakeman", "Desc": "Testing has completed successfully", "Target": "https://ci.example.com/2000/output" diff --git a/scm/driver/github/testdata/status.json.golden b/scm/driver/github/testdata/status.json.golden index 632cc43d8..53acbe968 100644 --- a/scm/driver/github/testdata/status.json.golden +++ b/scm/driver/github/testdata/status.json.golden @@ -1,5 +1,5 @@ { - "State": 3, + "State": "success", "Label": "continuous-integration/drone", "Desc": "Build has completed successfully", "Target": "https://ci.example.com/1000/output" diff --git a/scm/driver/github/testdata/statuses.json.golden b/scm/driver/github/testdata/statuses.json.golden index f2d5e1d64..e1f6b56fa 100644 --- a/scm/driver/github/testdata/statuses.json.golden +++ b/scm/driver/github/testdata/statuses.json.golden @@ -1,6 +1,6 @@ [ { - "State": 3, + "State": "success", "Label": "continuous-integration/drone", "Desc": "Build has completed successfully", "Target": "https://ci.example.com/1000/output" diff --git a/scm/driver/gitlab/testdata/status.json.golden b/scm/driver/gitlab/testdata/status.json.golden index c186dae4c..d781fe1f2 100644 --- a/scm/driver/gitlab/testdata/status.json.golden +++ b/scm/driver/gitlab/testdata/status.json.golden @@ -1,5 +1,5 @@ { - "State": 1, + "State": "pending", "Label": "default", "Desc": "the dude abides", "Target": "https://gitlab.example.com/thedude/gitlab-ce/builds/91" diff --git a/scm/driver/gitlab/testdata/statuses.json.golden b/scm/driver/gitlab/testdata/statuses.json.golden index cb32f75bc..1dab8932a 100644 --- a/scm/driver/gitlab/testdata/statuses.json.golden +++ b/scm/driver/gitlab/testdata/statuses.json.golden @@ -1,6 +1,6 @@ [ { - "State": 1, + "State": "pending", "Label": "default", "Desc": "the dude abides", "Target": "https://gitlab.example.com/thedude/gitlab-ce/builds/91" diff --git a/scm/driver/stash/testdata/commit_build_status.json.golden b/scm/driver/stash/testdata/commit_build_status.json.golden index acd4c1073..a508023df 100644 --- a/scm/driver/stash/testdata/commit_build_status.json.golden +++ b/scm/driver/stash/testdata/commit_build_status.json.golden @@ -1,12 +1,12 @@ [ { - "State": 3, + "State": "success", "Label": "team-bb-server » repo-1 » master #23", "Desc": "This commit looks good.", "Target": "http://example.com/master-1/job/kyounger/job/repo-1/job/master/21/display/redirect" }, { - "State": 3, + "State": "success", "Label": "team-bb-server » repo-1 » master #22", "Desc": "This commit looks good.", "Target": "http://example.com/master-1/job/kyounger/job/repo-1/job/master/22/display/redirect"