Skip to content

Commit fc548f3

Browse files
Slack file upload plugin feature (#174)
* adding slack file send * added all file send params * adding slack file upload * adding test files * modifed plugin tests to check for request body similar to previous cases * adding output env vars * updating slack file upload instructions * handling filename arg is empty case * Update plugin.go done Co-authored-by: OP (oppenheimer) <[email protected]> * not creating any tmp files if DRONE_OUTPUT not set * Update README.md * Update README.md * Update plugin.go * Update README.md --------- Co-authored-by: OP (oppenheimer) <[email protected]>
1 parent 417a312 commit fc548f3

File tree

8 files changed

+235
-37
lines changed

8 files changed

+235
-37
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ vendor/
2828

2929
coverage.out
3030
drone-slack
31+
32+
.idea

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a -tags netgo -o release/linux/a
2626
docker build --rm -t plugins/slack .
2727
```
2828

29-
## Usage
29+
## Send Slack messages Usage
30+
31+
To Send Slack messages use the following
3032

3133
Execute from the working directory:
3234

@@ -50,7 +52,6 @@ docker run --rm \
5052
plugins/slack
5153
```
5254

53-
5455
Please note the following new environment variables:
5556

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

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

64+
65+
## Upload files to Slack
66+
67+
To Send Slack messages use the following
68+
69+
Execute from the working directory:
70+
```
71+
docker run --network host --rm \
72+
-e PLUGIN_ACCESS_TOKEN=your_access_token \
73+
-e PLUGIN_CHANNEL=C07TL1KNV8Q \
74+
-e PLUGIN_USERNAME=jenkinstest003app \
75+
-e PLUGIN_FILE_PATH='/home/hns/test/b.txt' \
76+
-e PLUGIN_INITIAL_COMMENT='some start of text' \
77+
-e PLUGIN_TITLE='Build OK now' \
78+
plugins/slack
79+
```
80+
81+
Please note the following new environment variables:
82+
83+
- `PLUGIN_ACCESS_TOKEN`: The access token for Slack API authentication.
84+
- `PLUGIN_CUSTOM_BLOCK`: Custom blocks in JSON format to include in the Slack message.
85+
86+
Make sure to replace `your_access_token` with your actual Slack access token and adjust
87+
88+
If you provide an access token, it will use the Slack API to send the message.
89+
90+
6391
## Release Preparation
6492

6593
Run the changelog generator.

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ go 1.20
55
require (
66
github.com/drone/drone-template-lib v1.0.0
77
github.com/joho/godotenv v1.5.1
8-
github.com/slack-go/slack v0.12.2
8+
github.com/slack-go/slack v0.15.0
99
github.com/urfave/cli v1.22.14
10-
github.com/urfave/cli/v2 v2.25.7
1110
gotest.tools/v3 v3.5.1
1211
)
1312

@@ -19,7 +18,7 @@ require (
1918
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
2019
github.com/google/go-cmp v0.5.9 // indirect
2120
github.com/google/uuid v1.1.0 // indirect
22-
github.com/gorilla/websocket v1.4.2 // indirect
21+
github.com/gorilla/websocket v1.5.3 // indirect
2322
github.com/huandu/xstrings v1.2.0 // indirect
2423
github.com/imdario/mergo v0.3.7 // indirect
2524
github.com/pkg/errors v0.8.0 // indirect

go.sum

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
21
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
32
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
43
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
@@ -19,15 +18,14 @@ github.com/drone/drone-template-lib v1.0.0 h1:PNBBfUhifRnrPCoWBlTitk3jipXdv8u8WL
1918
github.com/drone/drone-template-lib v1.0.0/go.mod h1:Hqy1tgqPH5mtbFOZmow19l4jOkZvp+WZ00cB4W3MJhg=
2019
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
2120
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
22-
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
23-
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
2421
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
2522
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
2623
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2724
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
2825
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
29-
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
3026
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
27+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
28+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3129
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
3230
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
3331
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
@@ -40,59 +38,34 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
4038
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4139
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
4240
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
43-
github.com/slack-go/slack v0.12.2 h1:x3OppyMyGIbbiyFhsBmpf9pwkUzMhthJMRNmNlA4LaQ=
44-
github.com/slack-go/slack v0.12.2/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
41+
github.com/slack-go/slack v0.15.0 h1:LE2lj2y9vqqiOf+qIIy0GvEoxgF1N5yLGZffmEZykt0=
42+
github.com/slack-go/slack v0.15.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
4543
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4644
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
4745
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
4846
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4947
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5048
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
51-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
52-
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
49+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
5350
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
5451
github.com/tkuchiki/faketime v0.0.0-20170607100027-a4500a4f4643 h1:ii/sHfgFMByozryLeiDmn1ClZ/Pena4NgpJ4P7UuX9o=
5552
github.com/tkuchiki/faketime v0.0.0-20170607100027-a4500a4f4643/go.mod h1:RXY/TXAwGGL36IKDjrHFMcjpUrEiyWSEtLhFPw3UWF0=
56-
github.com/urfave/cli v1.22.13 h1:wsLILXG8qCJNse/qAgLNf23737Cx05GflHg/PJGe1Ok=
57-
github.com/urfave/cli v1.22.13/go.mod h1:VufqObjsMTF2BBwKawpx9R8eAneNEWhoO0yx8Vd+FkE=
5853
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
5954
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
60-
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
61-
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
6255
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
6356
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
64-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
6557
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
6658
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
67-
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
6859
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
69-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
70-
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
71-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
72-
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
7360
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7461
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
75-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
76-
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7762
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
78-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
79-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
80-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
81-
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
82-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
83-
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8463
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
85-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
86-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8764
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8865
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
8966
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
9067
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9168
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9269
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
93-
gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o=
94-
gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g=
95-
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
96-
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
9770
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
9871
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=

main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,33 @@ func main() {
212212
Usage: "slack message. either this or the custom template must be set. ",
213213
EnvVar: "PLUGIN_MESSAGE",
214214
},
215+
216+
// File send params
217+
cli.StringFlag{
218+
Name: "filepath",
219+
Usage: "slack file path",
220+
EnvVar: "PLUGIN_FILE_PATH",
221+
},
222+
cli.StringFlag{
223+
Name: "filename",
224+
Usage: "slack file name",
225+
EnvVar: "PLUGIN_FILE_NAME",
226+
},
227+
cli.StringFlag{
228+
Name: "title",
229+
Usage: "slack title",
230+
EnvVar: "PLUGIN_TITLE",
231+
},
232+
cli.StringFlag{
233+
Name: "initial_comment",
234+
Usage: "slack initial comment",
235+
EnvVar: "PLUGIN_INITIAL_COMMENT",
236+
},
237+
cli.BoolFlag{
238+
Name: "fail_on_error",
239+
Usage: "fail build on error",
240+
EnvVar: "PLUGIN_FAIL_ON_ERROR",
241+
},
215242
}
216243

217244
if _, err := os.Stat("/run/drone/env"); err == nil {
@@ -271,8 +298,15 @@ func run(c *cli.Context) error {
271298
Mentions: c.String("mentions"),
272299
CustomTemplate: c.String("custom.template"),
273300
Message: c.String("message"),
301+
// File upload attributes
302+
FilePath: c.String("filepath"),
303+
FileName: c.String("filename"),
304+
Title: c.String("title"),
305+
InitialComment: c.String("initial_comment"),
306+
FailOnError: c.Bool("fail_on_error"),
274307
},
275308
}
309+
276310
if plugin.Build.Commit == "" {
277311
plugin.Build.Commit = "0000000000000000000000000000000000000000"
278312
}

plugin.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"os"
8+
"path/filepath"
89
"strings"
910
textTemplate "text/template"
1011

@@ -70,6 +71,12 @@ type (
7071
Mentions string
7172
CustomTemplate string
7273
Message string
74+
// File Upload attributes
75+
FilePath string
76+
FileName string
77+
InitialComment string
78+
Title string
79+
FailOnError bool
7380
}
7481

7582
Job struct {
@@ -111,6 +118,10 @@ func (p Plugin) Exec() error {
111118
var text string
112119
var fallbackText string
113120

121+
if p.Config.FilePath != "" {
122+
return p.UploadFile()
123+
}
124+
114125
// Determine the channel
115126
if p.Config.Recipient != "" {
116127
channel = prepend("@", p.Config.Recipient)
@@ -307,6 +318,120 @@ func (p Plugin) Exec() error {
307318
return slack.PostWebhook(p.Config.Webhook, &payload)
308319
}
309320

321+
func (p Plugin) UploadFile() error {
322+
323+
p.Config.FilePath = strings.TrimSpace(p.Config.FilePath)
324+
325+
api := slack.New(p.Config.AccessToken)
326+
fileSize, err := GetFileSize(p.Config.FilePath)
327+
if err != nil {
328+
fmt.Printf("Error getting file size: %s\n", err.Error())
329+
return err
330+
}
331+
332+
if p.Config.FileName == "" {
333+
fileName := filepath.Base(p.Config.FilePath)
334+
p.Config.FileName = fileName
335+
}
336+
337+
params := slack.UploadFileV2Parameters{
338+
File: p.Config.FilePath,
339+
Channel: p.Config.Channel,
340+
Filename: p.Config.FileName,
341+
Title: p.Config.Title,
342+
InitialComment: p.Config.InitialComment,
343+
FileSize: fileSize,
344+
}
345+
346+
slackSummary, err := api.UploadFileV2(params)
347+
348+
if !p.Config.FailOnError && slackSummary == nil {
349+
if err != nil {
350+
fmt.Println("Bad Api ret val, upload file failed but passing build as PLUGIN_FAIL_ON_ERROR is false")
351+
}
352+
return nil
353+
} else if p.Config.FailOnError && slackSummary == nil {
354+
fmt.Println("Bad ret val, Failed to upload file, failing build")
355+
_ = p.WriteFileUploadResult("", "", err)
356+
return fmt.Errorf("Bad ret val, Failed to upload file %s ", p.Config.FilePath)
357+
}
358+
359+
if !p.Config.FailOnError && err != nil {
360+
if err != nil {
361+
fmt.Println("Unable to upload file but passing build PLUGIN_FAIL_ON_ERROR is false")
362+
}
363+
return nil
364+
} else if p.Config.FailOnError && err != nil {
365+
fmt.Println("Upload API Failed to upload file, failing build")
366+
_ = p.WriteFileUploadResult("", "", err)
367+
return fmt.Errorf("Failed to upload file %s ", p.Config.FilePath)
368+
}
369+
370+
err = p.WriteFileUploadResult(slackSummary.ID, slackSummary.Title, err)
371+
if !p.Config.FailOnError {
372+
if err != nil {
373+
fmt.Println("Unable to Write output env var results for file upload " +
374+
"but passing build PLUGIN_FAIL_ON_ERROR is false")
375+
}
376+
return nil
377+
}
378+
379+
return nil
380+
}
381+
382+
func (p Plugin) WriteFileUploadResult(slackFileId, title string, err error) error {
383+
384+
type EnvKvPair struct {
385+
Key string
386+
Value string
387+
}
388+
389+
resultStr := "Failed: Slack file upload failed"
390+
if err == nil {
391+
resultStr = "Success: Slack file upload successful"
392+
}
393+
394+
var kvPairs = []EnvKvPair{
395+
{Key: "UPLOAD_OK_STATUS", Value: resultStr},
396+
{Key: "UPLOAD_FILE_PATH", Value: p.Config.FilePath},
397+
}
398+
399+
var retErr error = nil
400+
401+
for _, kvPair := range kvPairs {
402+
err := WriteEnvToOutputFile(kvPair.Key, kvPair.Value)
403+
if err != nil {
404+
retErr = err
405+
}
406+
}
407+
408+
return retErr
409+
}
410+
411+
func WriteEnvToOutputFile(key, value string) error {
412+
outputFile, err := os.OpenFile(os.Getenv("DRONE_OUTPUT"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
413+
if err != nil {
414+
return fmt.Errorf("failed to open output file: %w", err)
415+
}
416+
defer outputFile.Close()
417+
_, err = fmt.Fprintf(outputFile, "%s=%s\n", key, value)
418+
if err != nil {
419+
return fmt.Errorf("failed to write to env: %w", err)
420+
}
421+
return nil
422+
}
423+
424+
func GetFileSize(filePath string) (int, error) {
425+
fileInfo, err := os.Stat(filePath)
426+
if err != nil {
427+
return 0, fmt.Errorf("failed to get file info: %w", err)
428+
}
429+
if fileInfo.IsDir() {
430+
return 0, fmt.Errorf("path %s is a directory, not a file", filePath)
431+
}
432+
return int(fileInfo.Size()), nil
433+
}
434+
310435
func templateMessage(t string, plugin Plugin) (string, error) {
311436
c, err := contents(t)
312437
if err != nil {

0 commit comments

Comments
 (0)