Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ vendor/

coverage.out
drone-slack

.idea
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/a
docker build --rm -t plugins/slack .
```

## Usage
## Send Slack messages Usage

To Send Slack messages use the following

Execute from the working directory:

Expand All @@ -50,7 +52,6 @@ docker run --rm \
plugins/slack
```


Please note the following new environment variables:

- `SLACK_ACCESS_TOKEN`: The access token for Slack API authentication.
Expand All @@ -60,6 +61,33 @@ Make sure to replace `your_access_token` with your actual Slack access token and

If you provide an access token, it will use the Slack API to send the message. Otherwise, it will use the webhook.


## Upload files on Slack Usage

To Send Slack messages use the following

Execute from the working directory:
```
docker run --network host --rm \
-e SLACK_ACCESS_TOKEN=xoxb-791.... \
-e PLUGIN_CHANNEL=C07TL1KNV8Q \
-e PLUGIN_USERNAME=jenkinstest003app \
-e PLUGIN_FILE_PATH='/home/hns/test/b.txt' \
-e PLUGIN_INITIAL_COMMENT='some start of text' \
-e PLUGIN_TITLE='Build OK now' \
plugins/slack
```

Please note the following new environment variables:

- `SLACK_ACCESS_TOKEN`: The access token for Slack API authentication.
- `PLUGIN_CUSTOM_BLOCK`: Custom blocks in JSON format to include in the Slack message.

Make sure to replace `your_access_token` with your actual Slack access token and adjust

If you provide an access token, it will use the Slack API to send the message.


## Release Preparation

Run the changelog generator.
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ go 1.20
require (
github.com/drone/drone-template-lib v1.0.0
github.com/joho/godotenv v1.5.1
github.com/slack-go/slack v0.12.2
github.com/slack-go/slack v0.15.0
github.com/urfave/cli v1.22.14
github.com/urfave/cli/v2 v2.25.7
gotest.tools/v3 v3.5.1
)

Expand All @@ -19,7 +18,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.1.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/pkg/errors v0.8.0 // indirect
Expand Down
37 changes: 5 additions & 32 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
Expand All @@ -19,15 +18,14 @@ github.com/drone/drone-template-lib v1.0.0 h1:PNBBfUhifRnrPCoWBlTitk3jipXdv8u8WL
github.com/drone/drone-template-lib v1.0.0/go.mod h1:Hqy1tgqPH5mtbFOZmow19l4jOkZvp+WZ00cB4W3MJhg=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
Expand All @@ -40,59 +38,34 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/slack-go/slack v0.12.2 h1:x3OppyMyGIbbiyFhsBmpf9pwkUzMhthJMRNmNlA4LaQ=
github.com/slack-go/slack v0.12.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/slack-go/slack v0.15.0 h1:LE2lj2y9vqqiOf+qIIy0GvEoxgF1N5yLGZffmEZykt0=
github.com/slack-go/slack v0.15.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tkuchiki/faketime v0.0.0-20170607100027-a4500a4f4643 h1:ii/sHfgFMByozryLeiDmn1ClZ/Pena4NgpJ4P7UuX9o=
github.com/tkuchiki/faketime v0.0.0-20170607100027-a4500a4f4643/go.mod h1:RXY/TXAwGGL36IKDjrHFMcjpUrEiyWSEtLhFPw3UWF0=
github.com/urfave/cli v1.22.13 h1:wsLILXG8qCJNse/qAgLNf23737Cx05GflHg/PJGe1Ok=
github.com/urfave/cli v1.22.13/go.mod h1:VufqObjsMTF2BBwKawpx9R8eAneNEWhoO0yx8Vd+FkE=
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/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/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,33 @@ func main() {
Usage: "slack message. either this or the custom template must be set. ",
EnvVar: "PLUGIN_MESSAGE",
},

// File send params
cli.StringFlag{
Name: "filepath",
Usage: "slack file path",
EnvVar: "PLUGIN_FILE_PATH",
},
cli.StringFlag{
Name: "filename",
Usage: "slack file name",
EnvVar: "PLUGIN_FILE_NAME",
},
cli.StringFlag{
Name: "title",
Usage: "slack title",
EnvVar: "PLUGIN_TITLE",
},
cli.StringFlag{
Name: "initial_comment",
Usage: "slack initial comment",
EnvVar: "PLUGIN_INITIAL_COMMENT",
},
cli.BoolFlag{
Name: "fail_on_error",
Usage: "fail build on error",
EnvVar: "PLUGIN_FAIL_ON_ERROR",
},
}

if _, err := os.Stat("/run/drone/env"); err == nil {
Expand Down Expand Up @@ -271,8 +298,15 @@ func run(c *cli.Context) error {
Mentions: c.String("mentions"),
CustomTemplate: c.String("custom.template"),
Message: c.String("message"),
// File upload attributes
FilePath: c.String("filepath"),
FileName: c.String("filename"),
Title: c.String("title"),
InitialComment: c.String("initial_comment"),
FailOnError: c.Bool("fail_on_error"),
},
}

if plugin.Build.Commit == "" {
plugin.Build.Commit = "0000000000000000000000000000000000000000"
}
Expand Down
139 changes: 139 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
textTemplate "text/template"

Expand Down Expand Up @@ -70,6 +72,12 @@ type (
Mentions string
CustomTemplate string
Message string
// File Upload attributes
FilePath string
FileName string
InitialComment string
Title string
FailOnError bool
}

Job struct {
Expand Down Expand Up @@ -111,6 +119,10 @@ func (p Plugin) Exec() error {
var text string
var fallbackText string

if p.Config.FilePath != "" {
return p.UploadFile()
}

// Determine the channel
if p.Config.Recipient != "" {
channel = prepend("@", p.Config.Recipient)
Expand Down Expand Up @@ -307,6 +319,133 @@ func (p Plugin) Exec() error {
return slack.PostWebhook(p.Config.Webhook, &payload)
}

func (p Plugin) UploadFile() error {

p.Config.FilePath = strings.TrimSpace(p.Config.FilePath)

api := slack.New(p.Config.AccessToken)
fileSize, err := GetFileSize(p.Config.FilePath)
if err != nil {
fmt.Printf("Error getting file size: %s\n", err.Error())
return err
}

if p.Config.FileName == "" {
fileName := filepath.Base(p.Config.FilePath)
p.Config.FileName = fileName
}

params := slack.UploadFileV2Parameters{
File: p.Config.FilePath,
Channel: p.Config.Channel,
Filename: p.Config.FileName,
Title: p.Config.Title,
InitialComment: p.Config.InitialComment,
FileSize: fileSize,
}

slackSummary, err := api.UploadFileV2(params)

if !p.Config.FailOnError && slackSummary == nil {
if err != nil {
fmt.Println("Bad Api ret val, upload file failed but passing build as PLUGIN_FAIL_ON_ERROR is false")
}
return nil
} else if p.Config.FailOnError && slackSummary == nil {
fmt.Println("Bad ret val, Failed to upload file, failing build")
_ = p.WriteFileUploadResult("", "", err)
return fmt.Errorf("Bad ret val, Failed to upload file %s ", p.Config.FilePath)
}

if !p.Config.FailOnError && err != nil {
if err != nil {
fmt.Println("Unable to upload file but passing build PLUGIN_FAIL_ON_ERROR is false")
}
return nil
} else if p.Config.FailOnError && err != nil {
fmt.Println("Upload API Failed to upload file, failing build")
_ = p.WriteFileUploadResult("", "", err)
return fmt.Errorf("Failed to upload file %s ", p.Config.FilePath)
}

err = p.WriteFileUploadResult(slackSummary.ID, slackSummary.Title, err)
if !p.Config.FailOnError {
if err != nil {
fmt.Println("Unable to Write output env var results for file upload " +
"but passing build PLUGIN_FAIL_ON_ERROR is false")
}
return nil
}

return nil
}

func (p Plugin) WriteFileUploadResult(slackFileId, title string, err error) error {

type EnvKvPair struct {
Key string
Value interface{}
}

resultStr := "Failed: Slack file upload failed"
if err == nil {
resultStr = "Success: Slack file upload successful"
}

var kvPairs = []EnvKvPair{
{Key: "UPLOAD_OK_STATUS", Value: resultStr},
{Key: "UPLOAD_FILE_PATH", Value: p.Config.FilePath},
}

var retErr error = nil

for _, kvPair := range kvPairs {
err := WriteEnvVariableAsString(kvPair.Key, kvPair.Value)
if err != nil {
retErr = err
}
}

return retErr
}

func WriteEnvVariableAsString(key string, value interface{}) error {

if GetOutputVariablesStorageFilePath() == "" {
return errors.New("Output file path is empty, check env var DRONE_OUTPUT")
}

outputFile, err := os.OpenFile(GetOutputVariablesStorageFilePath(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open output file: %w", err)
}
defer outputFile.Close()

valueStr := fmt.Sprintf("%v", value)

_, err = fmt.Fprintf(outputFile, "%s=%s\n", key, valueStr)
if err != nil {
return fmt.Errorf("failed to write to env: %w", err)
}

return nil
}

func GetOutputVariablesStorageFilePath() string {
if os.Getenv("DRONE_OUTPUT") == "" {
return "/tmp/drone-output"
}
return os.Getenv("DRONE_OUTPUT")
}

func GetFileSize(filePath string) (int, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
return 0, err
}
return int(fileInfo.Size()), nil
}

func templateMessage(t string, plugin Plugin) (string, error) {
c, err := contents(t)
if err != nil {
Expand Down
Loading