From eea4d26d74e83296af64a9ceee0e34a95c36414d Mon Sep 17 00:00:00 2001 From: MTRNord Date: Thu, 14 Dec 2017 21:45:48 +0100 Subject: [PATCH] Rework mapping of json Data, Deprecate fields named different in CircleCI (but support TravisCI namings), Document most important new namings, Update template and test templates to new preferred namings --- .../go-neb/services/circleci/circleci.go | 41 ++++++++++++++----- .../go-neb/services/circleci/circleci_test.go | 8 ++-- vendor/manifest | 6 +++ vendor/src/github.com/fatih/structs | 1 + 4 files changed, 41 insertions(+), 15 deletions(-) create mode 160000 vendor/src/github.com/fatih/structs diff --git a/src/github.com/matrix-org/go-neb/services/circleci/circleci.go b/src/github.com/matrix-org/go-neb/services/circleci/circleci.go index a999a76..9085953 100644 --- a/src/github.com/matrix-org/go-neb/services/circleci/circleci.go +++ b/src/github.com/matrix-org/go-neb/services/circleci/circleci.go @@ -14,6 +14,7 @@ import ( "github.com/matrix-org/gomatrix" "io/ioutil" "strconv" + "github.com/fatih/structs" ) // ServiceType of the CircleCI service. @@ -21,8 +22,8 @@ const ServiceType = "circleci" // DefaultTemplate contains the template that will be used if none is supplied. // This matches the default mentioned at: https://docs.travis-ci.com/user/notifications#Customizing-slack-notifications -const DefaultTemplate = (`%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message} - Build details : %{build_url}`) +const DefaultTemplate = (`%{repository_slug}#%{buildnum} (%{branch} - %{commit} : %{committername}): %{body} + Build details : %{buildurl}`) // Matches 'owner/repo' var ownerRepoRegex = regexp.MustCompile(`^([A-z0-9-_.]+)/([A-z0-9-_.]+)$`) @@ -41,7 +42,7 @@ var httpClient = &http.Client{} // "!ewfug483gsfe:localhost": { // repos: { // "matrix-org/go-neb": { -// template: "%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}\nBuild details : %{build_url}" +// template: "%{repository_slug}#%{buildnum} (%{branch} - %{commit} : %{committername}): %{body}\nBuild details : %{buildurl}" // } // } // } @@ -61,22 +62,30 @@ type Service struct { // This is identical to the format of Slack Notifications for Travis-CI: // https://docs.travis-ci.com/user/notifications#Customizing-slack-notifications // + // As this is CircleCI it also supports all CircleCI fields (full loiwer case no "_" ) + // Compare with https://circleci.com/docs/api/v1-reference/#build + // // The following variables are available: // repository_slug: your Git* repo identifier (like svenfuchs/minimal) - // repository_name: the slug without the username - // build_number: build number - // build_id: build id + // reponame: the slug without the username + // repository_name: the slug without the username //Deprecated for CircleCI use "reponame" instead + // buildnum: build number + // build_number: build number //Deprecated for CircleCI use "buildnum" instead + // build_id: build id //Deprecated for CircleCI use "build_num" instead as this value doesn't really exist in CircleCI // branch: branch build name // commit: shortened commit SHA - // author: commit author name - // commit_message: commit message of build + // committername: commit author name + // author: commit author name //Deprecated for CircleCI use "committername" instead + // body: commit message of build + // commit_message: commit message of build //Deprecated for CircleCI use "body" instead // commit_subject: first line of the commit message // result: result of build // message: CircleCI message to the build // duration: total duration of all builds in the matrix // elapsed_time: time between build start and finish - // compare_url: commit change view URL - // build_url: URL of the build detail + // buildurl: URL of the build detail + // build_url: URL of the build detail //Deprecated for CircleCI use "buildurl" instead + Template string `json:"template"` } `json:"repos"` } `json:"rooms"` @@ -92,7 +101,6 @@ func notifToTemplate(n WebhookNotification) map[string]string { t["repository_name"] = p.Reponame t["build_number"] = strconv.Itoa(p.BuildNum) t["build_id"] = t["build_number"] // CircleCI doesn't have a difference between number and ID but to be consistent with TravisCI - t["branch"] = p.Branch shaLength := len(p.VcsRevision) if shaLength > 10 { shaLength = 10 @@ -115,6 +123,17 @@ func notifToTemplate(n WebhookNotification) map[string]string { } t["build_url"] = p.BuildURL + + // Make the full struct data into the Map without manual mapping + rawT := structs.Map(p) + for key, value := range rawT { + switch value := value.(type) { + case string: + t[strings.ToLower(key)] = value + case int: + t[strings.ToLower(key)] = strconv.Itoa(value) + } + } return t } diff --git a/src/github.com/matrix-org/go-neb/services/circleci/circleci_test.go b/src/github.com/matrix-org/go-neb/services/circleci/circleci_test.go index 37f5ae0..cbd7b24 100644 --- a/src/github.com/matrix-org/go-neb/services/circleci/circleci_test.go +++ b/src/github.com/matrix-org/go-neb/services/circleci/circleci_test.go @@ -25,22 +25,22 @@ var circleciTests = []struct { }{ { exampleBody, - "%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}", + "%{repository_slug}#%{buildnum} (%{branch} - %{commit} : %{committername}): %{message}", "circleci/mongofinil#22 (master - 1d231626ba : Allen Rohner): success", }, { exampleBody, - "%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}", + "%{repository_slug}#%{buildnum} (%{branch} - %{commit} : %{committername}): %{message}", "circleci/mongofinil#22 (master - 1d231626ba : Allen Rohner): success", }, { strings.TrimSuffix(exampleBody, "%7D") + "%2C%22EXTRA_KEY%22%3Anull%7D", - "%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}", + "%{repository_slug}#%{buildnum} (%{branch} - %{commit} : %{committername}): %{message}", "circleci/mongofinil#22 (master - 1d231626ba : Allen Rohner): success", }, { exampleBody, - "%{repository}#%{build_number} %{duration}", + "%{repository_slug}#%{buildnum} %{duration}", "circleci/mongofinil#22 23s", }, } diff --git a/vendor/manifest b/vendor/manifest index 93d78e9..64bc543 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -76,6 +76,12 @@ "revision": "0e55cff0d6054f900b91c0b5355fe6a6966fad4b", "branch": "master" }, + { + "importpath": "github.com/fatih/structs", + "repository": "https://github.com/fatih/structs", + "revision": "f5faa72e73092639913f5833b75e1ac1d6bc7a63", + "branch": "master" + }, { "importpath": "github.com/garyburd/redigo/internal", "repository": "https://github.com/garyburd/redigo", diff --git a/vendor/src/github.com/fatih/structs b/vendor/src/github.com/fatih/structs new file mode 160000 index 0000000..f5faa72 --- /dev/null +++ b/vendor/src/github.com/fatih/structs @@ -0,0 +1 @@ +Subproject commit f5faa72e73092639913f5833b75e1ac1d6bc7a63