From fd44eaa12417d85eeb177b37477d648c6ec525dd Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Wed, 31 Oct 2018 09:31:52 -0700 Subject: [PATCH 01/12] feat(deployment): pull custom payload out of deployment object This is unique to the andyet deployments started by our slack bot --- .gitignore | 1 + README.md | 13 ++++---- build.sh | 4 +++ main.go | 42 ++++++++++++++++---------- plugin.go | 88 +++++++++++++++++++++++++++++++++++++++++------------- 5 files changed, 105 insertions(+), 43 deletions(-) create mode 100755 build.sh diff --git a/.gitignore b/.gitignore index ccf8328..b2d0f06 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ vendor/ coverage.out drone-slack +run.sh diff --git a/README.md b/README.md index f478cb9..109860c 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,13 @@ # drone-slack -[![Build Status](http://beta.drone.io/api/badges/drone-plugins/drone-slack/status.svg)](http://beta.drone.io/drone-plugins/drone-slack) -[![Join the discussion at https://discourse.drone.io](https://img.shields.io/badge/discourse-forum-orange.svg)](https://discourse.drone.io) -[![Drone questions at https://stackoverflow.com](https://img.shields.io/badge/drone-stackoverflow-orange.svg)](https://stackoverflow.com/questions/tagged/drone.io) -[![Go Doc](https://godoc.org/github.com/drone-plugins/drone-slack?status.svg)](http://godoc.org/github.com/drone-plugins/drone-slack) -[![Go Report](https://goreportcard.com/badge/github.com/drone-plugins/drone-slack)](https://goreportcard.com/report/github.com/drone-plugins/drone-slack) -[![](https://images.microbadger.com/badges/image/plugins/slack.svg)](https://microbadger.com/images/plugins/slack "Get your own image badge on microbadger.com") - Drone plugin for sending Slack notifications. For the usage information and a listing of the available options please take a look at [the docs](http://plugins.drone.io/drone-plugins/drone-slack/). +## NOTE: + +This is a fork of the original project, customized for our andyet deploy +pipeline. + + ## Build Build the binary with the following commands: diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..fd4be3d --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-slack +docker build --rm -t andyet/drone-slack . diff --git a/main.go b/main.go index 32a0231..635b527 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "os" "log" + "os" "github.com/urfave/cli" ) @@ -25,6 +25,11 @@ func main() { Usage: "slack webhook url", EnvVar: "SLACK_WEBHOOK,PLUGIN_WEBHOOK", }, + cli.StringFlag{ + Name: "ghtoken", + Usage: "github access token", + EnvVar: "GITHUB_ACCESS_TOKEN", + }, cli.StringFlag{ Name: "channel", Usage: "slack channel", @@ -81,6 +86,11 @@ func main() { EnvVar: "DRONE_COMMIT_SHA", Value: "00000000", }, + cli.StringFlag{ + Name: "commit.link", + Usage: "git commit link", + EnvVar: "DRONE_COMMIT_LINK", + }, cli.StringFlag{ Name: "commit.ref", Value: "refs/heads/master", @@ -169,25 +179,27 @@ func run(c *cli.Context) error { Name: c.String("repo.name"), }, Build: Build{ - Tag: c.String("build.tag"), - Number: c.Int("build.number"), - Event: c.String("build.event"), - Status: c.String("build.status"), - Commit: c.String("commit.sha"), - Ref: c.String("commit.ref"), - Branch: c.String("commit.branch"), - Author: c.String("commit.author"), - Pull: c.String("commit.pull"), - Message: c.String("commit.message"), - DeployTo: c.String("build.deployTo"), - Link: c.String("build.link"), - Started: c.Int64("build.started"), - Created: c.Int64("build.created"), + Tag: c.String("build.tag"), + Number: c.Int("build.number"), + Event: c.String("build.event"), + Status: c.String("build.status"), + Commit: c.String("commit.sha"), + CommitLink: c.String("commit.link"), + Ref: c.String("commit.ref"), + Branch: c.String("commit.branch"), + Author: c.String("commit.author"), + Pull: c.String("commit.pull"), + Message: c.String("commit.message"), + DeployTo: c.String("build.deployTo"), + Link: c.String("build.link"), + Started: c.Int64("build.started"), + Created: c.Int64("build.created"), }, Job: Job{ Started: c.Int64("job.started"), }, Config: Config{ + GhToken: c.String("ghtoken"), Webhook: c.String("webhook"), Channel: c.String("channel"), Recipient: c.String("recipient"), diff --git a/plugin.go b/plugin.go index f9f8c1b..a2218b7 100644 --- a/plugin.go +++ b/plugin.go @@ -3,9 +3,14 @@ package main import ( "fmt" "strings" + "context" + "strconv" + "encoding/json" "github.com/bluele/slack" "github.com/drone/drone-template-lib/template" + "github.com/google/go-github/github" + "golang.org/x/oauth2" ) type ( @@ -15,20 +20,21 @@ type ( } Build struct { - Tag string - Event string - Number int - Commit string - Ref string - Branch string - Author string - Pull string - Message string - DeployTo string - Status string - Link string - Started int64 - Created int64 + Tag string + Event string + Number int + Commit string + CommitLink string + Ref string + Branch string + Author string + Pull string + Message string + DeployTo string + Status string + Link string + Started int64 + Created int64 } Config struct { @@ -41,6 +47,7 @@ type ( IconURL string IconEmoji string LinkNames bool + GhToken string } Job struct { @@ -53,6 +60,10 @@ type ( Config Config Job Job } + + DeploymentPayload struct { + Person *string `json:"person,omitempty"` + } ) func (p Plugin) Exec() error { @@ -70,11 +81,21 @@ func (p Plugin) Exec() error { payload.IconUrl = p.Config.IconURL payload.IconEmoji = p.Config.IconEmoji - if p.Config.Recipient != "" { - payload.Channel = prepend("@", p.Config.Recipient) - } else if p.Config.Channel != "" { - payload.Channel = prepend("#", p.Config.Channel) - } + if (p.Build.Event == "deployment") { + user := deployUser(p.Config, p.Repo, p.Build) + if (user != "") { + payload.Channel = prepend("@", user) + } + } + + if (payload.Channel == "") { + if p.Config.Recipient != "" { + payload.Channel = prepend("@", p.Config.Recipient) + } else if p.Config.Channel != "" { + payload.Channel = prepend("#", p.Config.Channel) + } + } + if p.Config.LinkNames == true { payload.LinkNames = "1" } @@ -88,8 +109,8 @@ func (p Plugin) Exec() error { attachment.Text = txt } - client := slack.NewWebHook(p.Config.Webhook) - return client.PostMessage(&payload) + client := slack.NewWebHook(p.Config.Webhook) + return client.PostMessage(&payload) } func message(repo Repo, build Build) string { @@ -133,3 +154,28 @@ func prepend(prefix, s string) string { return s } + +func deployUser(config Config, repo Repo, build Build) string { + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: config.GhToken}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + parts := strings.Split(build.CommitLink, "/") + owner := parts[4] + name := parts[5] + id, _ := strconv.ParseInt(parts[7], 10, 64) + deployment, _, gherr := client.Repositories.GetDeployment(ctx, owner, name, id) + if (gherr != nil) { + fmt.Println(gherr) + return "" + } + var payload DeploymentPayload + jsonerr := json.Unmarshal(deployment.Payload, &payload) + if (jsonerr != nil) { + fmt.Println(jsonerr) + return "" + } + return *payload.Person +} From eeb16490f189b5ad17c14303e642bb33515f4b6a Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Fri, 1 Feb 2019 15:42:27 -0800 Subject: [PATCH 02/12] feat(plugin): use response_url or slack username lookups --- .gitignore | 2 + main.go | 59 ++++++++++++++------ plugin.go | 154 ++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 166 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index ccf8328..ea87d59 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ vendor/ coverage.out drone-slack +run.sh +build.sh diff --git a/main.go b/main.go index 32a0231..646f31c 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,15 @@ package main import ( "fmt" - "os" "log" + "os" "github.com/urfave/cli" ) var ( - version = "0.0.0" - build = "0" + version = "2.1.0" + build = "1" ) func main() { @@ -25,6 +25,16 @@ func main() { Usage: "slack webhook url", EnvVar: "SLACK_WEBHOOK,PLUGIN_WEBHOOK", }, + cli.StringFlag{ + Name: "ghtoken", + Usage: "github access token", + EnvVar: "GITHUB_ACCESS_TOKEN", + }, + cli.StringFlag{ + Name: "ghtoslacks", + Usage: "github_slack_lookup", + EnvVar: "GITHUB_SLACK_LOOKUP", + }, cli.StringFlag{ Name: "channel", Usage: "slack channel", @@ -81,6 +91,11 @@ func main() { EnvVar: "DRONE_COMMIT_SHA", Value: "00000000", }, + cli.StringFlag{ + Name: "commit.link", + Usage: "git commit link", + EnvVar: "DRONE_COMMIT_LINK", + }, cli.StringFlag{ Name: "commit.ref", Value: "refs/heads/master", @@ -125,6 +140,12 @@ func main() { Value: "success", EnvVar: "DRONE_BUILD_STATUS", }, + cli.StringFlag{ + Name: "build.prevstatus", + Usage: "build previous status", + Value: "success", + EnvVar: "DRONE_PREV_BUILD_STATUS", + }, cli.StringFlag{ Name: "build.link", Usage: "build link", @@ -169,25 +190,29 @@ func run(c *cli.Context) error { Name: c.String("repo.name"), }, Build: Build{ - Tag: c.String("build.tag"), - Number: c.Int("build.number"), - Event: c.String("build.event"), - Status: c.String("build.status"), - Commit: c.String("commit.sha"), - Ref: c.String("commit.ref"), - Branch: c.String("commit.branch"), - Author: c.String("commit.author"), - Pull: c.String("commit.pull"), - Message: c.String("commit.message"), - DeployTo: c.String("build.deployTo"), - Link: c.String("build.link"), - Started: c.Int64("build.started"), - Created: c.Int64("build.created"), + Tag: c.String("build.tag"), + Number: c.Int("build.number"), + Event: c.String("build.event"), + Status: c.String("build.status"), + PrevStatus: c.String("build.prevstatus"), + Commit: c.String("commit.sha"), + CommitLink: c.String("commit.link"), + Ref: c.String("commit.ref"), + Branch: c.String("commit.branch"), + Author: c.String("commit.author"), + Pull: c.String("commit.pull"), + Message: c.String("commit.message"), + DeployTo: c.String("build.deployTo"), + Link: c.String("build.link"), + Started: c.Int64("build.started"), + Created: c.Int64("build.created"), }, Job: Job{ Started: c.Int64("job.started"), }, Config: Config{ + GhToken: c.String("ghtoken"), + GhToSlackJSON: c.String("ghtoslacks"), Webhook: c.String("webhook"), Channel: c.String("channel"), Recipient: c.String("recipient"), diff --git a/plugin.go b/plugin.go index f9f8c1b..b22ffba 100644 --- a/plugin.go +++ b/plugin.go @@ -1,11 +1,16 @@ package main import ( + "context" + "encoding/json" "fmt" + "strconv" "strings" - "github.com/bluele/slack" "github.com/drone/drone-template-lib/template" + "github.com/google/go-github/github" + "github.com/wraithgar/slack" + "golang.org/x/oauth2" ) type ( @@ -15,20 +20,22 @@ type ( } Build struct { - Tag string - Event string - Number int - Commit string - Ref string - Branch string - Author string - Pull string - Message string - DeployTo string - Status string - Link string - Started int64 - Created int64 + Tag string + Event string + Number int + Commit string + CommitLink string + Ref string + Branch string + Author string + Pull string + Message string + DeployTo string + Status string + PrevStatus string + Link string + Started int64 + Created int64 } Config struct { @@ -40,7 +47,9 @@ type ( ImageURL string IconURL string IconEmoji string - LinkNames bool + LinkNames bool + GhToken string + GhToSlackJSON string } Job struct { @@ -53,9 +62,21 @@ type ( Config Config Job Job } + + //Custom payload object from andbang deploy event + DeploymentPayload struct { + ResponseURL string `json:"response_url,omitempty"` + Person string `json:"person,omitempty"` + } + + GhToSlack struct { + Github string `json:"github"` + Slack string `json:"slack"` + } ) func (p Plugin) Exec() error { + fmt.Printf("andyet custom drone-slack version %s build %s\n", version, build) attachment := slack.Attachment{ Text: message(p.Repo, p.Build), Fallback: fallback(p.Repo, p.Build), @@ -64,20 +85,6 @@ func (p Plugin) Exec() error { ImageURL: p.Config.ImageURL, } - payload := slack.WebHookPostPayload{} - payload.Username = p.Config.Username - payload.Attachments = []*slack.Attachment{&attachment} - payload.IconUrl = p.Config.IconURL - payload.IconEmoji = p.Config.IconEmoji - - if p.Config.Recipient != "" { - payload.Channel = prepend("@", p.Config.Recipient) - } else if p.Config.Channel != "" { - payload.Channel = prepend("#", p.Config.Channel) - } - if p.Config.LinkNames == true { - payload.LinkNames = "1" - } if p.Config.Template != "" { txt, err := template.RenderTrim(p.Config.Template, p) @@ -88,8 +95,66 @@ func (p Plugin) Exec() error { attachment.Text = txt } - client := slack.NewWebHook(p.Config.Webhook) - return client.PostMessage(&payload) + fmt.Printf("Build event: %s\n", p.Build.Event) + if p.Build.Event == "deployment" { + //Send a delayed response if we have a response url + deployPayload, err := getDeploymentPayload(p.Config, p.Repo, p.Build) + if err != nil { + return err + } + response := slack.ResponsePayload{} + response.Attachments = []*slack.Attachment{&attachment} + if p.Config.LinkNames == true { + response.LinkNames = "1" + } + fmt.Println("sending delayed response") + delayedClient := slack.NewDelayedResponse(deployPayload.ResponseURL) + return delayedClient.PostDelayedResponse(&response) + } + + //Send a webhook message + payload := slack.WebHookPostPayload{} + payload.Username = p.Config.Username + payload.Attachments = []*slack.Attachment{&attachment} + payload.IconUrl = p.Config.IconURL + payload.IconEmoji = p.Config.IconEmoji + payload.Attachments = []*slack.Attachment{&attachment} + + //These are the only cases we care about + if p.Build.Event == "tag" || p.Build.Status == "failure" || p.Build.PrevStatus == "failure" { + if p.Config.GhToSlackJSON != "" { + var ghtoslacks []GhToSlack + jsonerr := json.Unmarshal([]byte(p.Config.GhToSlackJSON), &ghtoslacks) + if jsonerr != nil { + fmt.Println(jsonerr) + } else { + for _, v := range ghtoslacks { + if (v.Github == p.Build.Author) { + payload.Channel = prepend("@", v.Slack) + } + } + } + } + + if payload.Channel == "" { + if p.Config.Recipient != "" { + payload.Channel = prepend("@", p.Config.Recipient) + } else if p.Config.Channel != "" { + payload.Channel = prepend("#", p.Config.Channel) + } + } + + if p.Config.LinkNames == true { + payload.LinkNames = "1" + } + + fmt.Printf("sending webhook message to %s\n", payload.Channel) + client := slack.NewWebHook(p.Config.Webhook) + return client.PostMessage(&payload) + } + fmt.Println("Not sending this one to slack") + fmt.Println(attachment.Text) + return nil } func message(repo Repo, build Build) string { @@ -133,3 +198,28 @@ func prepend(prefix, s string) string { return s } + +func getDeploymentPayload(config Config, repo Repo, build Build) (DeploymentPayload, error) { + var payload DeploymentPayload + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: config.GhToken}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) + parts := strings.Split(build.CommitLink, "/") + owner := parts[4] + name := parts[5] + id, _ := strconv.ParseInt(parts[7], 10, 64) + deployment, _, gherr := client.Repositories.GetDeployment(ctx, owner, name, id) + if gherr != nil { + fmt.Println(gherr) + return payload, gherr + } + jsonerr := json.Unmarshal(deployment.Payload, &payload) + if jsonerr != nil { + fmt.Println(jsonerr) + return payload, jsonerr + } + return payload, nil +} From 2f68049752e78e129f56840f535e4aed1af03998 Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Mon, 4 Feb 2019 13:39:45 -0800 Subject: [PATCH 03/12] tweak dockerfile to point to us --- .gitignore | 1 - Dockerfile | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ea87d59..b2d0f06 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,3 @@ vendor/ coverage.out drone-slack run.sh -build.sh diff --git a/Dockerfile b/Dockerfile index d2ec668..18b3788 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,8 @@ FROM plugins/base:multiarch -LABEL maintainer="Drone.IO Community " \ +LABEL maintainer="Andyet OPS " \ org.label-schema.name="Drone Slack" \ - org.label-schema.vendor="Drone.IO Community" \ - org.label-schema.schema-version="1.0" + org.label-schema.schema-version="2.0" ADD release/linux/amd64/drone-slack /bin/ ENTRYPOINT ["/bin/drone-slack"] From d54694ea0379038262f6668b0d69da5fdb8e11fa Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Wed, 6 Feb 2019 13:08:12 -0800 Subject: [PATCH 04/12] feat(plugin): add github to slack username lookups --- Dockerfile | 2 +- build.sh | 4 ++-- main.go | 2 +- plugin.go | 53 ++++++++++++++++++++++++++++------------------------- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Dockerfile b/Dockerfile index 18b3788..642870d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM plugins/base:multiarch LABEL maintainer="Andyet OPS " \ org.label-schema.name="Drone Slack" \ - org.label-schema.schema-version="2.0" + org.label-schema.schema-version="2.1.0" ADD release/linux/amd64/drone-slack /bin/ ENTRYPOINT ["/bin/drone-slack"] diff --git a/build.sh b/build.sh index fd4be3d..f63e346 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #!/bin/bash -GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-slack -docker build --rm -t andyet/drone-slack . +GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-slack \ +&& docker build --rm -t andyet/drone-slack . diff --git a/main.go b/main.go index 2a60ba8..490bfca 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( var ( version = "2.1.0" - build = "1" + build = "5" ) func main() { diff --git a/plugin.go b/plugin.go index b128087..037bb23 100644 --- a/plugin.go +++ b/plugin.go @@ -122,36 +122,39 @@ func (p Plugin) Exec() error { //These are the only cases we care about if p.Build.Event == "tag" || p.Build.Status == "failure" || p.Build.PrevStatus == "failure" { - if p.Config.GhToSlackJSON != "" { - var ghtoslacks []GhToSlack - jsonerr := json.Unmarshal([]byte(p.Config.GhToSlackJSON), &ghtoslacks) - if jsonerr != nil { - fmt.Println(jsonerr) - } else { - for _, v := range ghtoslacks { - if v.Github == p.Build.Author { - payload.Channel = prepend("@", v.Slack) - } - } - } - } - - if payload.Channel == "" { - if p.Config.Recipient != "" { - payload.Channel = prepend("@", p.Config.Recipient) - } else if p.Config.Channel != "" { - payload.Channel = prepend("#", p.Config.Channel) - } - } - - if p.Config.LinkNames == true { - payload.LinkNames = "1" - } + if p.Config.GhToSlackJSON != "" { + var ghtoslacks []GhToSlack + jsonerr := json.Unmarshal([]byte(p.Config.GhToSlackJSON), &ghtoslacks) + if jsonerr != nil { + fmt.Println(jsonerr) + } else { + for _, v := range ghtoslacks { + if v.Github == p.Build.Author { + payload.Channel = prepend("@", v.Slack) + } + } + } + } else { + fmt.Println("GhToSlackJSON not found") + } + + if payload.Channel == "" { + if p.Config.Recipient != "" { + payload.Channel = prepend("@", p.Config.Recipient) + } else if p.Config.Channel != "" { + payload.Channel = prepend("#", p.Config.Channel) + } + } + + if p.Config.LinkNames == true { + payload.LinkNames = "1" + } fmt.Printf("sending webhook message to %s\n", payload.Channel) client := slack.NewWebHook(p.Config.Webhook) return client.PostMessage(&payload) } + fmt.Println("Not sending this one to slack") fmt.Println(attachment.Text) return nil From b282f96c9dc03e97033bb4b81f24663fd06c1868 Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Wed, 6 Feb 2019 13:10:04 -0800 Subject: [PATCH 05/12] fix(gofmt): run gofmt --- plugin.go | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/plugin.go b/plugin.go index 037bb23..f2e6a45 100644 --- a/plugin.go +++ b/plugin.go @@ -122,33 +122,33 @@ func (p Plugin) Exec() error { //These are the only cases we care about if p.Build.Event == "tag" || p.Build.Status == "failure" || p.Build.PrevStatus == "failure" { - if p.Config.GhToSlackJSON != "" { - var ghtoslacks []GhToSlack - jsonerr := json.Unmarshal([]byte(p.Config.GhToSlackJSON), &ghtoslacks) - if jsonerr != nil { - fmt.Println(jsonerr) - } else { - for _, v := range ghtoslacks { - if v.Github == p.Build.Author { - payload.Channel = prepend("@", v.Slack) - } - } - } - } else { - fmt.Println("GhToSlackJSON not found") - } - - if payload.Channel == "" { - if p.Config.Recipient != "" { - payload.Channel = prepend("@", p.Config.Recipient) - } else if p.Config.Channel != "" { - payload.Channel = prepend("#", p.Config.Channel) - } - } - - if p.Config.LinkNames == true { - payload.LinkNames = "1" - } + if p.Config.GhToSlackJSON != "" { + var ghtoslacks []GhToSlack + jsonerr := json.Unmarshal([]byte(p.Config.GhToSlackJSON), &ghtoslacks) + if jsonerr != nil { + fmt.Println(jsonerr) + } else { + for _, v := range ghtoslacks { + if v.Github == p.Build.Author { + payload.Channel = prepend("@", v.Slack) + } + } + } + } else { + fmt.Println("GhToSlackJSON not found") + } + + if payload.Channel == "" { + if p.Config.Recipient != "" { + payload.Channel = prepend("@", p.Config.Recipient) + } else if p.Config.Channel != "" { + payload.Channel = prepend("#", p.Config.Channel) + } + } + + if p.Config.LinkNames == true { + payload.LinkNames = "1" + } fmt.Printf("sending webhook message to %s\n", payload.Channel) client := slack.NewWebHook(p.Config.Webhook) From 3081c79654663cf070b515b73ce5c9d127f12951 Mon Sep 17 00:00:00 2001 From: Heather Young Date: Fri, 17 Jan 2020 16:55:20 -0800 Subject: [PATCH 06/12] update andyet-> talky logging --- Dockerfile | 2 +- README.md | 2 +- build.sh | 2 +- plugin.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 642870d..c5d2746 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM plugins/base:multiarch -LABEL maintainer="Andyet OPS " \ +LABEL maintainer="Talky OPS " \ org.label-schema.name="Drone Slack" \ org.label-schema.schema-version="2.1.0" diff --git a/README.md b/README.md index 109860c..fb58f86 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Drone plugin for sending Slack notifications. For the usage information and a li ## NOTE: -This is a fork of the original project, customized for our andyet deploy +This is a fork of the original project, customized for Talky Inc. deploy pipeline. diff --git a/build.sh b/build.sh index f63e346..a42469a 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #!/bin/bash GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-slack \ -&& docker build --rm -t andyet/drone-slack . +&& docker build --rm -t one000mph/drone-slack . diff --git a/plugin.go b/plugin.go index f2e6a45..89f82e3 100644 --- a/plugin.go +++ b/plugin.go @@ -76,7 +76,7 @@ type ( ) func (p Plugin) Exec() error { - fmt.Printf("andyet custom drone-slack version %s build %s\n", version, build) + fmt.Printf("talky custom drone-slack version %s build %s\n", version, build) attachment := slack.Attachment{ Text: message(p.Repo, p.Build), Fallback: fallback(p.Repo, p.Build), From 9e35613bf7d6e55d7d841dd4733025447d5a498f Mon Sep 17 00:00:00 2001 From: Heather Young Date: Mon, 20 Jan 2020 09:41:45 -0800 Subject: [PATCH 07/12] update to urfave cli v2 --- build.sh | 2 +- main.go | 120 +++++++++++++++++++++++++++---------------------------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/build.sh b/build.sh index a42469a..19e67a6 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #!/bin/bash GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/amd64/drone-slack \ -&& docker build --rm -t one000mph/drone-slack . +&& docker build --rm -t one000mph/drone-slack:$1 . diff --git a/main.go b/main.go index 490bfca..dee0a33 100644 --- a/main.go +++ b/main.go @@ -20,161 +20,161 @@ func main() { app.Action = run app.Version = fmt.Sprintf("%s+%s", version, build) app.Flags = []cli.Flag{ - cli.StringFlag{ + &cli.StringFlag{ Name: "webhook", Usage: "slack webhook url", - EnvVar: "SLACK_WEBHOOK,PLUGIN_WEBHOOK", + EnvVars: []string{"SLACK_WEBHOOK,PLUGIN_WEBHOOK"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "ghtoken", Usage: "github access token", - EnvVar: "GITHUB_ACCESS_TOKEN", + EnvVars: []string{"GITHUB_ACCESS_TOKEN"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "ghtoslacks", Usage: "github_slack_lookup", - EnvVar: "GITHUB_SLACK_LOOKUP", + EnvVars: []string{"GITHUB_SLACK_LOOKUP"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "channel", Usage: "slack channel", - EnvVar: "PLUGIN_CHANNEL", + EnvVars: []string{"PLUGIN_CHANNEL"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "recipient", Usage: "slack recipient", - EnvVar: "PLUGIN_RECIPIENT", + EnvVars: []string{"PLUGIN_RECIPIENT"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "username", Usage: "slack username", - EnvVar: "PLUGIN_USERNAME", + EnvVars: []string{"PLUGIN_USERNAME"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "template", Usage: "slack template", - EnvVar: "PLUGIN_TEMPLATE", + EnvVars: []string{"PLUGIN_TEMPLATE"}, }, - cli.BoolFlag{ + &cli.BoolFlag{ Name: "link-names", Usage: "slack link names", - EnvVar: "PLUGIN_LINK_NAMES", + EnvVars: []string{"PLUGIN_LINK_NAMES"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "image", Usage: "slack image url", - EnvVar: "PLUGIN_IMAGE_URL", + EnvVars: []string{"PLUGIN_IMAGE_URL"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "icon.url", Usage: "slack icon url", - EnvVar: "PLUGIN_ICON_URL", + EnvVars: []string{"PLUGIN_ICON_URL"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "icon.emoji", Usage: "slack emoji url", - EnvVar: "PLUGIN_ICON_EMOJI", + EnvVars: []string{"PLUGIN_ICON_EMOJI"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "repo.owner", Usage: "repository owner", - EnvVar: "DRONE_REPO_OWNER", + EnvVars: []string{"DRONE_REPO_OWNER"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "repo.name", Usage: "repository name", - EnvVar: "DRONE_REPO_NAME", + EnvVars: []string{"DRONE_REPO_NAME"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.sha", Usage: "git commit sha", - EnvVar: "DRONE_COMMIT_SHA", + EnvVars: []string{"DRONE_COMMIT_SHA"}, Value: "00000000", }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.link", Usage: "git commit link", - EnvVar: "DRONE_COMMIT_LINK", + EnvVars: []string{"DRONE_COMMIT_LINK"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.ref", Value: "refs/heads/master", Usage: "git commit ref", - EnvVar: "DRONE_COMMIT_REF", + EnvVars: []string{"DRONE_COMMIT_REF"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.branch", Value: "master", Usage: "git commit branch", - EnvVar: "DRONE_COMMIT_BRANCH", + EnvVars: []string{"DRONE_COMMIT_BRANCH"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.author", Usage: "git author name", - EnvVar: "DRONE_COMMIT_AUTHOR", + EnvVars: []string{"DRONE_COMMIT_AUTHOR"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.pull", Usage: "git pull request", - EnvVar: "DRONE_PULL_REQUEST", + EnvVars: []string{"DRONE_PULL_REQUEST"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "commit.message", Usage: "commit message", - EnvVar: "DRONE_COMMIT_MESSAGE", + EnvVars: []string{"DRONE_COMMIT_MESSAGE"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "build.event", Value: "push", Usage: "build event", - EnvVar: "DRONE_BUILD_EVENT", + EnvVars: []string{"DRONE_BUILD_EVENT"}, }, - cli.IntFlag{ + &cli.IntFlag{ Name: "build.number", Usage: "build number", - EnvVar: "DRONE_BUILD_NUMBER", + EnvVars: []string{"DRONE_BUILD_NUMBER"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "build.status", Usage: "build status", Value: "success", - EnvVar: "DRONE_BUILD_STATUS", + EnvVars: []string{"DRONE_BUILD_STATUS"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "build.prevstatus", Usage: "build previous status", Value: "success", - EnvVar: "DRONE_PREV_BUILD_STATUS", + EnvVars: []string{"DRONE_PREV_BUILD_STATUS"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "build.link", Usage: "build link", - EnvVar: "DRONE_BUILD_LINK", + EnvVars: []string{"DRONE_BUILD_LINK"}, }, - cli.Int64Flag{ + &cli.Int64Flag{ Name: "build.started", Usage: "build started", - EnvVar: "DRONE_BUILD_STARTED", + EnvVars: []string{"DRONE_BUILD_STARTED"}, }, - cli.Int64Flag{ + &cli.Int64Flag{ Name: "build.created", Usage: "build created", - EnvVar: "DRONE_BUILD_CREATED", + EnvVars: []string{"DRONE_BUILD_CREATED"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "build.tag", Usage: "build tag", - EnvVar: "DRONE_TAG", + EnvVars: []string{"DRONE_TAG"}, }, - cli.StringFlag{ + &cli.StringFlag{ Name: "build.deployTo", Usage: "environment deployed to", - EnvVar: "DRONE_DEPLOY_TO", + EnvVars: []string{"DRONE_DEPLOY_TO"}, }, - cli.Int64Flag{ + &cli.Int64Flag{ Name: "job.started", Usage: "job started", - EnvVar: "DRONE_JOB_STARTED", + EnvVars: []string{"DRONE_JOB_STARTED"}, }, } From 190bd6f432a7af38853912e0baedad3201a60e41 Mon Sep 17 00:00:00 2001 From: Heather Young Date: Mon, 20 Jan 2020 10:46:03 -0800 Subject: [PATCH 08/12] fix deps --- .gitignore | 1 + go.mod | 24 ++++++++++ go.sum | 61 ++++++++++++++++++++++++ main.go | 134 ++++++++++++++++++++++++++--------------------------- 4 files changed, 153 insertions(+), 67 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index b2d0f06..8c8d65d 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ vendor/ coverage.out drone-slack run.sh +ghtoslack.json diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..07c14a3 --- /dev/null +++ b/go.mod @@ -0,0 +1,24 @@ +module github.com/one000mph/drone-slack + +go 1.13 + +require ( + github.com/Masterminds/goutils v1.1.0 // indirect + github.com/Masterminds/semver v1.5.0 // indirect + github.com/Masterminds/sprig v2.22.0+incompatible // indirect + github.com/aymerick/raymond v2.0.2+incompatible + github.com/bluele/slack v0.0.0-20180222052941-7f3862e374f1 + github.com/drone/drone-template-lib v0.0.0-20180401170447-5748d3149f48 + github.com/google/go-github v17.0.0+incompatible + github.com/google/go-querystring v1.0.0 // indirect + github.com/google/uuid v1.1.1 // indirect + github.com/huandu/xstrings v1.3.0 // indirect + github.com/imdario/mergo v0.3.8 // indirect + github.com/mitchellh/copystructure v1.0.0 // indirect + github.com/pkg/errors v0.8.0 + github.com/urfave/cli v1.22.2 + github.com/urfave/cli/v2 v2.1.1 + github.com/wraithgar/slack v0.0.0-20181102160856-1cbcbe076455 + golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect + golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c4756fa --- /dev/null +++ b/go.sum @@ -0,0 +1,61 @@ +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg= +github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= +github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= +github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= +github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60= +github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= +github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0= +github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= +github.com/bluele/slack v0.0.0-20180222052941-7f3862e374f1/go.mod h1:W679Ri2W93VLD8cVpEY/zLH1ow4zhJcCyjzrKxfM3QM= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/drone/drone-template-lib v0.0.0-20180401170447-5748d3149f48 h1:hP2l5isqaiJVvOUM8X/1WDNeeJKlVmM8I1B9aDNT/xw= +github.com/drone/drone-template-lib v0.0.0-20180401170447-5748d3149f48/go.mod h1:u34woe41m58Whrf21iH6z/xLOIRM3650LhWAyUc7Oik= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= +github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= +github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= +github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/huandu/xstrings v1.3.0 h1:gvV6jG9dTgFEncxo+AF7PH6MZXi/vZl25owA/8Dg8Wo= +github.com/huandu/xstrings v1.3.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ= +github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= +github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= +github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= +github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= +github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= +github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= +github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/wraithgar/slack v0.0.0-20181102160856-1cbcbe076455 h1:O0vqZyvv6It15xkN5PAdHuA27DsMvIOZkd8CcO83nPU= +github.com/wraithgar/slack v0.0.0-20181102160856-1cbcbe076455/go.mod h1:+CvmelyVX0sGu36WYIM9Jrl11pgsNlk9qkIIruw+52o= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index dee0a33..131c88a 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import ( "log" "os" - "github.com/urfave/cli" + "github.com/urfave/cli/v2" ) var ( @@ -21,159 +21,159 @@ func main() { app.Version = fmt.Sprintf("%s+%s", version, build) app.Flags = []cli.Flag{ &cli.StringFlag{ - Name: "webhook", - Usage: "slack webhook url", + Name: "webhook", + Usage: "slack webhook url", EnvVars: []string{"SLACK_WEBHOOK,PLUGIN_WEBHOOK"}, }, &cli.StringFlag{ - Name: "ghtoken", - Usage: "github access token", + Name: "ghtoken", + Usage: "github access token", EnvVars: []string{"GITHUB_ACCESS_TOKEN"}, }, &cli.StringFlag{ - Name: "ghtoslacks", - Usage: "github_slack_lookup", + Name: "ghtoslacks", + Usage: "github_slack_lookup", EnvVars: []string{"GITHUB_SLACK_LOOKUP"}, }, &cli.StringFlag{ - Name: "channel", - Usage: "slack channel", + Name: "channel", + Usage: "slack channel", EnvVars: []string{"PLUGIN_CHANNEL"}, }, &cli.StringFlag{ - Name: "recipient", - Usage: "slack recipient", + Name: "recipient", + Usage: "slack recipient", EnvVars: []string{"PLUGIN_RECIPIENT"}, }, &cli.StringFlag{ - Name: "username", - Usage: "slack username", + Name: "username", + Usage: "slack username", EnvVars: []string{"PLUGIN_USERNAME"}, }, &cli.StringFlag{ - Name: "template", - Usage: "slack template", + Name: "template", + Usage: "slack template", EnvVars: []string{"PLUGIN_TEMPLATE"}, }, &cli.BoolFlag{ - Name: "link-names", - Usage: "slack link names", + Name: "link-names", + Usage: "slack link names", EnvVars: []string{"PLUGIN_LINK_NAMES"}, }, &cli.StringFlag{ - Name: "image", - Usage: "slack image url", + Name: "image", + Usage: "slack image url", EnvVars: []string{"PLUGIN_IMAGE_URL"}, }, &cli.StringFlag{ - Name: "icon.url", - Usage: "slack icon url", + Name: "icon.url", + Usage: "slack icon url", EnvVars: []string{"PLUGIN_ICON_URL"}, }, &cli.StringFlag{ - Name: "icon.emoji", - Usage: "slack emoji url", + Name: "icon.emoji", + Usage: "slack emoji url", EnvVars: []string{"PLUGIN_ICON_EMOJI"}, }, &cli.StringFlag{ - Name: "repo.owner", - Usage: "repository owner", + Name: "repo.owner", + Usage: "repository owner", EnvVars: []string{"DRONE_REPO_OWNER"}, }, &cli.StringFlag{ - Name: "repo.name", - Usage: "repository name", + Name: "repo.name", + Usage: "repository name", EnvVars: []string{"DRONE_REPO_NAME"}, }, &cli.StringFlag{ - Name: "commit.sha", - Usage: "git commit sha", + Name: "commit.sha", + Usage: "git commit sha", EnvVars: []string{"DRONE_COMMIT_SHA"}, - Value: "00000000", + Value: "00000000", }, &cli.StringFlag{ - Name: "commit.link", - Usage: "git commit link", + Name: "commit.link", + Usage: "git commit link", EnvVars: []string{"DRONE_COMMIT_LINK"}, }, &cli.StringFlag{ - Name: "commit.ref", - Value: "refs/heads/master", - Usage: "git commit ref", + Name: "commit.ref", + Value: "refs/heads/master", + Usage: "git commit ref", EnvVars: []string{"DRONE_COMMIT_REF"}, }, &cli.StringFlag{ - Name: "commit.branch", - Value: "master", - Usage: "git commit branch", + Name: "commit.branch", + Value: "master", + Usage: "git commit branch", EnvVars: []string{"DRONE_COMMIT_BRANCH"}, }, &cli.StringFlag{ - Name: "commit.author", - Usage: "git author name", + Name: "commit.author", + Usage: "git author name", EnvVars: []string{"DRONE_COMMIT_AUTHOR"}, }, &cli.StringFlag{ - Name: "commit.pull", - Usage: "git pull request", + Name: "commit.pull", + Usage: "git pull request", EnvVars: []string{"DRONE_PULL_REQUEST"}, }, &cli.StringFlag{ - Name: "commit.message", - Usage: "commit message", + Name: "commit.message", + Usage: "commit message", EnvVars: []string{"DRONE_COMMIT_MESSAGE"}, }, &cli.StringFlag{ - Name: "build.event", - Value: "push", - Usage: "build event", + Name: "build.event", + Value: "push", + Usage: "build event", EnvVars: []string{"DRONE_BUILD_EVENT"}, }, &cli.IntFlag{ - Name: "build.number", - Usage: "build number", + Name: "build.number", + Usage: "build number", EnvVars: []string{"DRONE_BUILD_NUMBER"}, }, &cli.StringFlag{ - Name: "build.status", - Usage: "build status", - Value: "success", + Name: "build.status", + Usage: "build status", + Value: "success", EnvVars: []string{"DRONE_BUILD_STATUS"}, }, &cli.StringFlag{ - Name: "build.prevstatus", - Usage: "build previous status", - Value: "success", + Name: "build.prevstatus", + Usage: "build previous status", + Value: "success", EnvVars: []string{"DRONE_PREV_BUILD_STATUS"}, }, &cli.StringFlag{ - Name: "build.link", - Usage: "build link", + Name: "build.link", + Usage: "build link", EnvVars: []string{"DRONE_BUILD_LINK"}, }, &cli.Int64Flag{ - Name: "build.started", - Usage: "build started", + Name: "build.started", + Usage: "build started", EnvVars: []string{"DRONE_BUILD_STARTED"}, }, &cli.Int64Flag{ - Name: "build.created", - Usage: "build created", + Name: "build.created", + Usage: "build created", EnvVars: []string{"DRONE_BUILD_CREATED"}, }, &cli.StringFlag{ - Name: "build.tag", - Usage: "build tag", + Name: "build.tag", + Usage: "build tag", EnvVars: []string{"DRONE_TAG"}, }, &cli.StringFlag{ - Name: "build.deployTo", - Usage: "environment deployed to", + Name: "build.deployTo", + Usage: "environment deployed to", EnvVars: []string{"DRONE_DEPLOY_TO"}, }, &cli.Int64Flag{ - Name: "job.started", - Usage: "job started", + Name: "job.started", + Usage: "job started", EnvVars: []string{"DRONE_JOB_STARTED"}, }, } From 81729cfb4a6c64fdb39eb80ec5ef93da5a45ee39 Mon Sep 17 00:00:00 2001 From: Heather Young Date: Fri, 24 Jan 2020 16:29:21 -0800 Subject: [PATCH 09/12] look for PLUGIN_ prefix variables --- .drone.yml | 14 ++++++-------- Gopkg.lock | 4 ++-- go.mod | 16 ++++------------ go.sum | 27 +++------------------------ main.go | 6 +++--- 5 files changed, 18 insertions(+), 49 deletions(-) diff --git a/.drone.yml b/.drone.yml index de8452b..4cf3743 100644 --- a/.drone.yml +++ b/.drone.yml @@ -4,22 +4,20 @@ workspace: pipeline: deps: - image: golang:1.10 + image: golang:1.13 pull: true commands: - go get -u github.com/golang/dep/cmd/dep - dep ensure - dep status - test: - image: golang:1.10 + image: golang:1.13 pull: true commands: - go vet ./... - go test -cover ./... - build_linux_amd64: - image: golang:1.10 + image: golang:1.13 pull: true group: build environment: @@ -35,7 +33,7 @@ pipeline: fi build_linux_i386: - image: golang:1.10 + image: golang:1.13 pull: true group: build environment: @@ -51,7 +49,7 @@ pipeline: fi build_linux_arm64: - image: golang:1.10 + image: golang:1.13 pull: true group: build environment: @@ -67,7 +65,7 @@ pipeline: fi build_linux_arm: - image: golang:1.10 + image: golang:1.13 pull: true group: build environment: diff --git a/Gopkg.lock b/Gopkg.lock index 13adf74..af2b99c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -14,7 +14,7 @@ [[projects]] branch = "master" - name = "github.com/bluele/slack" + name = "github.com/wraithgar/slack" packages = ["."] revision = "7f3862e374f157ad3f23c3092fa23b744d87c68d" @@ -31,7 +31,7 @@ version = "v0.8.0" [[projects]] - name = "github.com/urfave/cli" + name = "github.com/urfave/cli/v2" packages = ["."] revision = "cfb38830724cc34fedffe9a2a29fb54fa9169cd1" version = "v1.20.0" diff --git a/go.mod b/go.mod index 07c14a3..788336f 100644 --- a/go.mod +++ b/go.mod @@ -3,22 +3,14 @@ module github.com/one000mph/drone-slack go 1.13 require ( - github.com/Masterminds/goutils v1.1.0 // indirect - github.com/Masterminds/semver v1.5.0 // indirect - github.com/Masterminds/sprig v2.22.0+incompatible // indirect - github.com/aymerick/raymond v2.0.2+incompatible - github.com/bluele/slack v0.0.0-20180222052941-7f3862e374f1 + github.com/aymerick/raymond v2.0.2+incompatible // indirect + github.com/bluele/slack v0.0.0-20180222052941-7f3862e374f1 // indirect github.com/drone/drone-template-lib v0.0.0-20180401170447-5748d3149f48 github.com/google/go-github v17.0.0+incompatible github.com/google/go-querystring v1.0.0 // indirect - github.com/google/uuid v1.1.1 // indirect - github.com/huandu/xstrings v1.3.0 // indirect - github.com/imdario/mergo v0.3.8 // indirect - github.com/mitchellh/copystructure v1.0.0 // indirect - github.com/pkg/errors v0.8.0 - github.com/urfave/cli v1.22.2 + github.com/pkg/errors v0.8.0 // indirect + github.com/urfave/cli v1.20.0 // indirect github.com/urfave/cli/v2 v2.1.1 github.com/wraithgar/slack v0.0.0-20181102160856-1cbcbe076455 - golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d ) diff --git a/go.sum b/go.sum index c4756fa..c21e173 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,5 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg= -github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= -github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= -github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= -github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60= -github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0= github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= github.com/bluele/slack v0.0.0-20180222052941-7f3862e374f1/go.mod h1:W679Ri2W93VLD8cVpEY/zLH1ow4zhJcCyjzrKxfM3QM= @@ -13,21 +7,12 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/drone/drone-template-lib v0.0.0-20180401170447-5748d3149f48 h1:hP2l5isqaiJVvOUM8X/1WDNeeJKlVmM8I1B9aDNT/xw= github.com/drone/drone-template-lib v0.0.0-20180401170447-5748d3149f48/go.mod h1:u34woe41m58Whrf21iH6z/xLOIRM3650LhWAyUc7Oik= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= -github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/huandu/xstrings v1.3.0 h1:gvV6jG9dTgFEncxo+AF7PH6MZXi/vZl25owA/8Dg8Wo= -github.com/huandu/xstrings v1.3.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= -github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ= -github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= -github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= -github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= -github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= -github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -38,24 +23,18 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= -github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/wraithgar/slack v0.0.0-20181102160856-1cbcbe076455 h1:O0vqZyvv6It15xkN5PAdHuA27DsMvIOZkd8CcO83nPU= github.com/wraithgar/slack v0.0.0-20181102160856-1cbcbe076455/go.mod h1:+CvmelyVX0sGu36WYIM9Jrl11pgsNlk9qkIIruw+52o= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= -golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 131c88a..28d12b0 100644 --- a/main.go +++ b/main.go @@ -23,17 +23,17 @@ func main() { &cli.StringFlag{ Name: "webhook", Usage: "slack webhook url", - EnvVars: []string{"SLACK_WEBHOOK,PLUGIN_WEBHOOK"}, + EnvVars: []string{"SLACK_WEBHOOK,PLUGIN_SLACK_WEBHOOK,"}, }, &cli.StringFlag{ Name: "ghtoken", Usage: "github access token", - EnvVars: []string{"GITHUB_ACCESS_TOKEN"}, + EnvVars: []string{"GITHUB_ACCESS_TOKEN,PLUGIN_GITHUB_ACCESS_TOKEN"}, }, &cli.StringFlag{ Name: "ghtoslacks", Usage: "github_slack_lookup", - EnvVars: []string{"GITHUB_SLACK_LOOKUP"}, + EnvVars: []string{"GITHUB_SLACK_LOOKUP,PLUGIN_GITHUB_SLACK_LOOKUP"}, }, &cli.StringFlag{ Name: "channel", From 9bb5a99c7174d838f19448e5469c5019694f958e Mon Sep 17 00:00:00 2001 From: Heather Young Date: Fri, 24 Jan 2020 17:26:43 -0800 Subject: [PATCH 10/12] use urfave cli v2, prep for Deploy ID --- Gopkg.toml | 4 ++-- main.go | 16 +++++++++++++--- plugin.go | 55 +++++++++++++++++++++++++++--------------------------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/Gopkg.toml b/Gopkg.toml index 4329a89..6c89075 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -1,13 +1,13 @@ [[constraint]] branch = "master" - name = "github.com/bluele/slack" + name = "github.com/wraithgar/slack" [[constraint]] branch = "master" name = "github.com/drone/drone-template-lib" [[constraint]] - name = "github.com/urfave/cli" + name = "github.com/urfave/cli/v2" version = "1.20.0" [prune] diff --git a/main.go b/main.go index 28d12b0..1f95eda 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,11 @@ var ( ) func main() { + + for _, pair := range os.Environ() { + fmt.Println(pair) + } + app := cli.NewApp() app.Name = "slack plugin" app.Usage = "slack plugin" @@ -23,17 +28,17 @@ func main() { &cli.StringFlag{ Name: "webhook", Usage: "slack webhook url", - EnvVars: []string{"SLACK_WEBHOOK,PLUGIN_SLACK_WEBHOOK,"}, + EnvVars: []string{"SLACK_WEBHOOK", "PLUGIN_SLACK_WEBHOOK,"}, }, &cli.StringFlag{ Name: "ghtoken", Usage: "github access token", - EnvVars: []string{"GITHUB_ACCESS_TOKEN,PLUGIN_GITHUB_ACCESS_TOKEN"}, + EnvVars: []string{"GITHUB_ACCESS_TOKEN", "PLUGIN_GITHUB_ACCESS_TOKEN"}, }, &cli.StringFlag{ Name: "ghtoslacks", Usage: "github_slack_lookup", - EnvVars: []string{"GITHUB_SLACK_LOOKUP,PLUGIN_GITHUB_SLACK_LOOKUP"}, + EnvVars: []string{"GITHUB_SLACK_LOOKUP", "PLUGIN_GITHUB_SLACK_LOOKUP"}, }, &cli.StringFlag{ Name: "channel", @@ -171,6 +176,11 @@ func main() { Usage: "environment deployed to", EnvVars: []string{"DRONE_DEPLOY_TO"}, }, + &cli.StringFlag{ + Name: "build.deployID", + Usage: "github deployment id", + EnvVars: []string{"DRONE_DEPLOY_ID"}, + }, &cli.Int64Flag{ Name: "job.started", Usage: "job started", diff --git a/plugin.go b/plugin.go index 89f82e3..c5dce6d 100644 --- a/plugin.go +++ b/plugin.go @@ -1,16 +1,12 @@ package main import ( - "context" "encoding/json" "fmt" - "strconv" "strings" "github.com/drone/drone-template-lib/template" - "github.com/google/go-github/github" "github.com/wraithgar/slack" - "golang.org/x/oauth2" ) type ( @@ -96,9 +92,10 @@ func (p Plugin) Exec() error { } fmt.Printf("Build event: %s\n", p.Build.Event) - if p.Build.Event == "deployment" { + if p.Build.Event == "promote" { //Send a delayed response if we have a response url deployPayload, err := getDeploymentPayload(p.Config, p.Repo, p.Build) + fmt.Println(deployPayload) if err != nil { return err } @@ -107,9 +104,10 @@ func (p Plugin) Exec() error { if p.Config.LinkNames == true { response.LinkNames = "1" } - fmt.Println("sending delayed response") - delayedClient := slack.NewDelayedResponse(deployPayload.ResponseURL) - return delayedClient.PostDelayedResponse(&response) + fmt.Println("pretending to send delayed response") + return err + // delayedClient := slack.NewDelayedResponse(deployPayload.ResponseURL) + // return delayedClient.PostDelayedResponse(&response) } //Send a webhook message @@ -204,25 +202,26 @@ func prepend(prefix, s string) string { func getDeploymentPayload(config Config, repo Repo, build Build) (DeploymentPayload, error) { var payload DeploymentPayload - ctx := context.Background() - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: config.GhToken}, - ) - tc := oauth2.NewClient(ctx, ts) - client := github.NewClient(tc) - parts := strings.Split(build.CommitLink, "/") - owner := parts[4] - name := parts[5] - id, _ := strconv.ParseInt(parts[7], 10, 64) - deployment, _, gherr := client.Repositories.GetDeployment(ctx, owner, name, id) - if gherr != nil { - fmt.Println(gherr) - return payload, gherr - } - jsonerr := json.Unmarshal(deployment.Payload, &payload) - if jsonerr != nil { - fmt.Println(jsonerr) - return payload, jsonerr - } + // ctx := context.Background() + // ts := oauth2.StaticTokenSource( + // &oauth2.Token{AccessToken: config.GhToken}, + // ) + // tc := oauth2.NewClient(ctx, ts) + // client := github.NewClient(tc) + parts := strings.Split(build.Link, "/") + owner := parts[3] + name := parts[4] + fmt.Println("OWNER, NAME", owner, name) + // id, _ := strconv.ParseInt(parts[7], 10, 64) + // deployment, _, gherr := client.Repositories.GetDeployment(ctx, owner, name, id) + // if gherr != nil { + // fmt.Println(gherr) + // return payload, gherr + // } + // jsonerr := json.Unmarshal(deployment.Payload, &payload) + // if jsonerr != nil { + // fmt.Println(jsonerr) + // return payload, jsonerr + // } return payload, nil } From e46362eb63c1bffde04807c43b46c4d9f9a548c5 Mon Sep 17 00:00:00 2001 From: Heather Young Date: Mon, 3 Feb 2020 18:56:33 -0800 Subject: [PATCH 11/12] fix ping deploy initiator after event --- main.go | 5 +---- plugin.go | 50 +++++++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 1f95eda..1add5cd 100644 --- a/main.go +++ b/main.go @@ -15,10 +15,6 @@ var ( func main() { - for _, pair := range os.Environ() { - fmt.Println(pair) - } - app := cli.NewApp() app.Name = "slack plugin" app.Usage = "slack plugin" @@ -213,6 +209,7 @@ func run(c *cli.Context) error { Pull: c.String("commit.pull"), Message: c.String("commit.message"), DeployTo: c.String("build.deployTo"), + DeployID: c.Int64("build.deployID"), Link: c.String("build.link"), Started: c.Int64("build.started"), Created: c.Int64("build.created"), diff --git a/plugin.go b/plugin.go index c5dce6d..e25fb11 100644 --- a/plugin.go +++ b/plugin.go @@ -1,12 +1,15 @@ package main import ( + "context" "encoding/json" "fmt" "strings" "github.com/drone/drone-template-lib/template" + "github.com/google/go-github/github" "github.com/wraithgar/slack" + "golang.org/x/oauth2" ) type ( @@ -27,6 +30,7 @@ type ( Pull string Message string DeployTo string + DeployID int64 Status string PrevStatus string Link string @@ -95,19 +99,19 @@ func (p Plugin) Exec() error { if p.Build.Event == "promote" { //Send a delayed response if we have a response url deployPayload, err := getDeploymentPayload(p.Config, p.Repo, p.Build) - fmt.Println(deployPayload) if err != nil { return err } response := slack.ResponsePayload{} + response.Attachments = []*slack.Attachment{&attachment} if p.Config.LinkNames == true { response.LinkNames = "1" } - fmt.Println("pretending to send delayed response") - return err - // delayedClient := slack.NewDelayedResponse(deployPayload.ResponseURL) - // return delayedClient.PostDelayedResponse(&response) + fmt.Printf("%+v\n", response) + // return err + delayedClient := slack.NewDelayedResponse(deployPayload.ResponseURL) + return delayedClient.PostDelayedResponse(&response) } //Send a webhook message @@ -202,26 +206,26 @@ func prepend(prefix, s string) string { func getDeploymentPayload(config Config, repo Repo, build Build) (DeploymentPayload, error) { var payload DeploymentPayload - // ctx := context.Background() - // ts := oauth2.StaticTokenSource( - // &oauth2.Token{AccessToken: config.GhToken}, - // ) - // tc := oauth2.NewClient(ctx, ts) - // client := github.NewClient(tc) + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: config.GhToken}, + ) + tc := oauth2.NewClient(ctx, ts) + client := github.NewClient(tc) parts := strings.Split(build.Link, "/") owner := parts[3] name := parts[4] - fmt.Println("OWNER, NAME", owner, name) - // id, _ := strconv.ParseInt(parts[7], 10, 64) - // deployment, _, gherr := client.Repositories.GetDeployment(ctx, owner, name, id) - // if gherr != nil { - // fmt.Println(gherr) - // return payload, gherr - // } - // jsonerr := json.Unmarshal(deployment.Payload, &payload) - // if jsonerr != nil { - // fmt.Println(jsonerr) - // return payload, jsonerr - // } + id := build.DeployID + + deployment, _, gherr := client.Repositories.GetDeployment(ctx, owner, name, id) + if gherr != nil { + fmt.Println(gherr) + return payload, gherr + } + jsonerr := json.Unmarshal(deployment.Payload, &payload) + if jsonerr != nil { + fmt.Println(jsonerr) + return payload, jsonerr + } return payload, nil } From 3c4e2fd99ff9c8d3efa991cf9957fed9c110f5a0 Mon Sep 17 00:00:00 2001 From: Heather Young Date: Sun, 29 Mar 2020 18:38:00 -0700 Subject: [PATCH 12/12] add logging --- plugin.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin.go b/plugin.go index e25fb11..4311021 100644 --- a/plugin.go +++ b/plugin.go @@ -108,7 +108,7 @@ func (p Plugin) Exec() error { if p.Config.LinkNames == true { response.LinkNames = "1" } - fmt.Printf("%+v\n", response) + fmt.Printf("%+v\n", response.Attachments) // return err delayedClient := slack.NewDelayedResponse(deployPayload.ResponseURL) return delayedClient.PostDelayedResponse(&response) @@ -133,6 +133,7 @@ func (p Plugin) Exec() error { for _, v := range ghtoslacks { if v.Github == p.Build.Author { payload.Channel = prepend("@", v.Slack) + fmt.Printf("%s", payload.Channel) } } } @@ -142,6 +143,7 @@ func (p Plugin) Exec() error { if payload.Channel == "" { if p.Config.Recipient != "" { + fmt.Printf("%s", p.Config.Recipient) payload.Channel = prepend("@", p.Config.Recipient) } else if p.Config.Channel != "" { payload.Channel = prepend("#", p.Config.Channel)