mirror of
https://github.com/drone-plugins/drone-webhook.git
synced 2026-06-04 18:24:05 +08:00
Introduce Valid Response Codes setting (#36)
In this pull-request I wanted to introduce valid-response-codes settings, that returns failed result, when response code of webhook is not among the list of valid codes. For goals of backwards compatibility, if this slice of valid-response-codes is empty, check is not performed.
This commit is contained in:
committed by
Bo-Yi Wu
parent
8dcc9e1d5d
commit
b7cac30ca6
@@ -34,6 +34,7 @@ docker run --rm \
|
||||
-e PLUGIN_HEADERS="HEADER1=value1" \
|
||||
-e PLUGIN_USERNAME=drone \
|
||||
-e PLUGIN_PASSWORD=password \
|
||||
-e PLUGIN_VALID_RESPONSE_CODES="200,202,404" \
|
||||
-e DRONE_REPO_OWNER=octocat \
|
||||
-e DRONE_REPO_NAME=hello-world \
|
||||
-e DRONE_COMMIT_SHA=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d \
|
||||
|
||||
@@ -57,6 +57,11 @@ func main() {
|
||||
Usage: "list of urls to perform the action on",
|
||||
EnvVar: "PLUGIN_URLS,WEBHOOK_URLS",
|
||||
},
|
||||
cli.IntSliceFlag{
|
||||
Name: "valid-response-codes",
|
||||
Usage: "list of valid http response codes",
|
||||
EnvVar: "PLUGIN_VALID_RESPONSE_CODES",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "enable debug information",
|
||||
@@ -184,6 +189,7 @@ func run(c *cli.Context) error {
|
||||
Template: c.String("template"),
|
||||
Headers: c.StringSlice("headers"),
|
||||
URLs: c.StringSlice("urls"),
|
||||
ValidCodes: c.IntSlice("valid-response-codes"),
|
||||
Debug: c.Bool("debug"),
|
||||
SkipVerify: c.Bool("skip-verify"),
|
||||
},
|
||||
|
||||
@@ -43,6 +43,7 @@ type (
|
||||
Template string
|
||||
Headers []string
|
||||
URLs []string
|
||||
ValidCodes []int
|
||||
Debug bool
|
||||
SkipVerify bool
|
||||
}
|
||||
@@ -169,7 +170,22 @@ func (p Plugin) Exec() error {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if len(p.Config.ValidCodes) > 0 && !intInSlice(p.Config.ValidCodes, resp.StatusCode) {
|
||||
return fmt.Errorf("Error: Response code %d not found among valid response codes", resp.StatusCode)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Function checks if int is in slice of ints
|
||||
func intInSlice(s []int, e int) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user