From 7fe5434e3a56f7ac2ac033e931a11a5669efa05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mitka?= <75326685+lukaszmitka-aptiv@users.noreply.github.com> Date: Tue, 12 Jan 2021 11:55:07 +0100 Subject: [PATCH] Add automatic status to color parsing --- README.md | 6 +++--- main.go | 14 +++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7d8eb44..1e853a9 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ SLACK_USERNAME | `rtBot` | Cust SLACK_MSG_AUTHOR | `$GITHUB_ACTOR` (The person who triggered action). | GitHub username of the person who has triggered the action. In case you want to modify it, please specify corrent GitHub username. SLACK_ICON | ![rtBot Avatar](https://github.com/rtBot.png?size=32) | User/Bot icon shown with Slack message. It uses the URL supplied to this env variable to display the icon in slack message. SLACK_ICON_EMOJI | - | User/Bot icon shown with Slack message, in case you do not wish to add a URL for slack icon as above, you can set slack emoji in this env variable. Example value: `:bell:` or any other valid slack emoji. -SLACK_COLOR | `good` (green) | You can pass an RGB value like `#efefef` which would change color on left side vertical line of Slack message. +SLACK_COLOR | `good` (green) | You can pass ${{job.status}} for automatic coloring or an RGB value like `#efefef` which would change color on left side vertical line of Slack message. SLACK_MESSAGE | Generated from git commit message. | The main Slack message in attachment. It is advised not to override this. SLACK_TITLE | Message | Title to use before main Slack message. SLACK_FOOTER | Powered By rtCamp's GitHub Actions Library | Slack message footer. @@ -61,7 +61,7 @@ You can see the action block with all variables as below: uses: rtCamp/action-slack-notify@v2 env: SLACK_CHANNEL: general - SLACK_COLOR: '#3278BD' + SLACK_COLOR: ${{ job.status }} SLACK_ICON: https://github.com/rtCamp.png?size=48 SLACK_MESSAGE: 'Post Content :rocket:' SLACK_TITLE: Post Title @@ -77,7 +77,7 @@ The `Site` and `SSH Host` details are only available if this action is run after ## Hashicorp Vault (Optional) -This GitHub action supports [Hashicorp Vault](https://www.vaultproject.io/). +This GitHub action supports [Hashicorp Vault](https://www.vaultproject.io/). To enable Hashicorp Vault support, please define following GitHub secrets: diff --git a/main.go b/main.go index a594ff1..0f02b1a 100644 --- a/main.go +++ b/main.go @@ -178,6 +178,18 @@ func main() { fields = append(newfields, fields...) } + color := "" + switch os.Getenv(EnvSlackColor) { + case "success": + color = "good" + case "cancelled": + color = "#808080" + case "failure": + color = "danger" + default: + color = envOr(EnvSlackColor, "good") + } + msg := Webhook{ UserName: os.Getenv(EnvSlackUserName), IconURL: os.Getenv(EnvSlackIcon), @@ -186,7 +198,7 @@ func main() { Attachments: []Attachment{ { Fallback: envOr(EnvSlackMessage, "GITHUB_ACTION="+os.Getenv("GITHUB_ACTION")+" \n GITHUB_ACTOR="+os.Getenv("GITHUB_ACTOR")+" \n GITHUB_EVENT_NAME="+os.Getenv("GITHUB_EVENT_NAME")+" \n GITHUB_REF="+os.Getenv("GITHUB_REF")+" \n GITHUB_REPOSITORY="+os.Getenv("GITHUB_REPOSITORY")+" \n GITHUB_WORKFLOW="+os.Getenv("GITHUB_WORKFLOW")), - Color: envOr(EnvSlackColor, "good"), + Color: color, AuthorName: envOr(EnvGithubActor, ""), AuthorLink: "http://github.com/" + os.Getenv(EnvGithubActor), AuthorIcon: "http://github.com/" + os.Getenv(EnvGithubActor) + ".png?size=32",