Skip to content

Commit

Permalink
Adds default, started alert statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
arbourd committed Jun 14, 2018
1 parent 296d1cf commit cbcd39f
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 31 deletions.
57 changes: 53 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

A structured and opinionated Slack notification resource for [Concourse](https://concourse.ci/).

<img src="./img/default.png" width="60%">

The message is built by using Concourse's [resource metadata](https://concourse-ci.org/implementing-resources.html#resource-metadata) to show the pipeline, job, build number and a URL.

## Installing

Use this resource by adding the following to the resource_types section of a pipeline config:
Expand All @@ -20,8 +24,9 @@ See the [Concourse docs](https://concourse-ci.org/resource-types.html) for more
## Source Configuration

* `url`: *Required.* Slack webhook URL.
* `username`: *Optional.* Concourse basic auth username. Required if using `alert_type: fixed`
* `password`: *Optional.* Concourse basic auth password. Required if using `alert_type: fixed`
* `concourse_url`: *Optional.* The external URL that points to Concourse. Defaults to the env variable `ATC_EXTERNAL_URL`.
* `username`: *Optional.* Concourse basic auth username. Required if using alert type `fixed`
* `password`: *Optional.* Concourse basic auth password. Required if using alert type `fixed`

## Behavior

Expand All @@ -35,13 +40,54 @@ Sends a structured message to Slack based on the alert type.

#### Parameters

* `alert_type`: *Required.* The type of alert to send to Slack. There are 4 options: `success`, `failed`, `aborted` and `fixed`.
- `alert_type`: *Optional.* The type of alert to send to Slack. Defaults to `default`.

`default`

<img src="./img/default.png" width="50%">

`success`

<img src="./img/success.png" width="50%">

`failed`

<img src="./img/failed.png" width="50%">

`started`

<img src="./img/started.png" width="50%">

`aborted`

<img src="./img/aborted.png" width="50%">

`fixed`

Fixed is a special alert type that requires both `username` and `password` to be set in Source and will only alert if the previous build was a failure.

<img src="./img/fixed.png" width="50%">

## Examples

### Out

Using build hooks with equivilent alert types:
Using the default alert type:

```yaml
resources:
- name: notify
type: slack-alert
source:
url: https://hooks.slack.com/services/ANER808F/SKDCVS3B/vvPBAWQVPHDKejdeThDiE4wrg
jobs:
# ...
plan:
- put: notify
```

Using built-in alert types with appropriate build hooks:

```yaml
resources:
Expand All @@ -53,6 +99,9 @@ resources:
jobs:
# ...
plan:
- put: notify
params:
alert_type: started
- put: some-other-task
on_success:
put: notify
Expand Down
Binary file added img/aborted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/failed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/fixed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/started.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 39 additions & 27 deletions out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,6 @@ type alertType struct {
Message string
}

var alertTypes = map[string]*alertType{
"success": &alertType{
Color: "#32cd32",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-succeeded.png",
Message: "Passed",
},
"failed": &alertType{
Color: "#d00000",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-failed.png",
Message: "Failed",
},
"aborted": &alertType{
Color: "#8d4b32",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-aborted.png",
Message: "Aborted",
},
"fixed": &alertType{
Color: "#32cd32",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-succeeded.png",
Message: "Fixed",
},
}

func main() {
var input *concourse.OutRequest
err := json.NewDecoder(os.Stdin).Decode(&input)
Expand All @@ -53,9 +30,44 @@ func main() {
log.Fatalln("slack url cannot be blank")
}

alert := alertTypes[input.Params.AlertType]
if alert == nil {
log.Fatalf("invalid alert type: '%s'\n", input.Params.AlertType)
var alert *alertType
switch input.Params.AlertType {
case "success":
alert = &alertType{
Color: "#32cd32",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-succeeded.png",
Message: "Success",
}
case "failed":
alert = &alertType{
Color: "#d00000",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-failed.png",
Message: "Failed",
}
case "started":
alert = &alertType{
Color: "#f7cd42",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-started.png",
Message: "Started",
}
case "aborted":
alert = &alertType{
Color: "#8d4b32",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-aborted.png",
Message: "Aborted",
}
case "fixed":
alert = &alertType{
Color: "#32cd32",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-succeeded.png",
Message: "Fixed",
}
default:
alert = &alertType{
Color: "#35495c",
IconURL: "https://ci.concourse-ci.org/public/images/favicon-pending.png",
Message: "",
}
}

metadata := &concourse.BuildMetadata{
Expand All @@ -70,7 +82,7 @@ func main() {
}

var sendMessage = true
if alert.Message == "Fixed" {
if input.Params.AlertType == "fixed" {
sendMessage, err = checkPreviousBuild(input, metadata)
if err != nil {
log.Fatalln(err)
Expand Down

0 comments on commit cbcd39f

Please sign in to comment.