diff --git a/README.md b/README.md index 2814acf..2e51935 100644 --- a/README.md +++ b/README.md @@ -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 \ diff --git a/main.go b/main.go index 0c90f75..0cfbbaf 100644 --- a/main.go +++ b/main.go @@ -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"), }, diff --git a/plugin.go b/plugin.go index 6fede06..ad0604a 100644 --- a/plugin.go +++ b/plugin.go @@ -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 +}