Skip to content

Commit d13f9aa

Browse files
authored
Merge pull request #43 from jbsil/adaptive_cards
convert message to adaptive cards with colored headers
2 parents 122363a + 4b24b41 commit d13f9aa

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed

Dockerfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
FROM ubuntu:22.04
22

3-
COPY check /opt/resource/check
4-
COPY in /opt/resource/in
5-
COPY out /opt/resource/out
6-
7-
RUN chmod +x /opt/resource/out /opt/resource/in /opt/resource/check && \
8-
apt-get update && \
3+
RUN apt-get update && \
94
DEBIAN_FRONTEND=noninteractive apt-get install -y curl jq
105

6+
COPY check /opt/resource/check
7+
COPY in /opt/resource/in
8+
COPY out /opt/resource/out
9+
COPY card.jq /opt/resource/card.jq
10+
11+
RUN chmod +x /opt/resource/out /opt/resource/in /opt/resource/check

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Example of an alert in a pull-request job:
101101
put: alert
102102
params:
103103
text: |
104-
pull request tested: with no errors
104+
pull request tested: success
105105
title: {{mypipeline}} Pull Request Tested
106106
actionName: {{mypipeline}} Pipeline
107107
actionTarget: $ATC_EXTERNAL_URL/teams/$BUILD_TEAM_NAME/pipelines/$BUILD_PIPELINE_NAME/jobs/$BUILD_JOB_NAME/builds/$BUILD_NAME
@@ -119,9 +119,10 @@ Example of an alert in a pull-request job:
119119
* `text`: *Required.* Text of the message to send - markdown supported (higher precedence over `text_file`)
120120
* `text_file`: *Required.* A location of text file of the message to send, usually an output from a previous task - markdown supported
121121
* `title`: *Optional.*
122-
* `color`: *Optional.*
123122
* `actionName`: *Optional.* Text on the button/link (shows up as a link though the Teams docs show a button)
124123
* `actionTarget`: *Optional.* URL to connect to the button/link
124+
* `style`: *Optional.* Adaptive Card header style. Can be one of 'good', 'attention', 'warning'. If not provided, `title` and `text` are scanned for keywords to determine the style. If no keywords are found, 'default' is used.
125+
* ~~`color`~~: *Deprecated*
125126

126127
# MORE EXAMPLES
127128

card.jq

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"type": "message",
3+
"attachments": [
4+
{
5+
"contentType": "application/vnd.microsoft.card.adaptive",
6+
"contentUrl": null,
7+
"content": {
8+
"type": "AdaptiveCard",
9+
"body": [
10+
{
11+
"type": "Container",
12+
"items": [
13+
{
14+
"type": "TextBlock",
15+
"text": $title,
16+
"wrap": true,
17+
"size": "Large",
18+
"weight": "Bolder"
19+
}
20+
],
21+
"style": $style
22+
},
23+
{
24+
"type": "TextBlock",
25+
"text": $text,
26+
"wrap": true
27+
}
28+
],
29+
"actions": [
30+
{
31+
"type": "Action.OpenUrl",
32+
"title": $actionName,
33+
"url": $actionTarget
34+
}
35+
],
36+
"msteams": {
37+
"width": "Full"
38+
},
39+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
40+
"version": "1.5"
41+
}
42+
}
43+
]
44+
}

out

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ payload=$(mktemp /tmp/resource-in.XXXXXX)
2121
cat > "${payload}" <&0
2222

2323
format="markdown"
24-
color="$(jq -r '.params.color // "00EA43"' < "${payload}")"
2524

2625
actionName=$(evaluate "$(jq -r '.params.actionName // "Open Concourse"' < "${payload}")")
2726

@@ -48,7 +47,31 @@ if [[ -n "${text_file}" ]]; then
4847
fi
4948
[[ -n "${text}" ]] && text_output="${text}"
5049

51-
body="{\"TextFormat\": \"${format}\", \"text\": \"${text_output}\", \"title\": \"${title}\", \"themeColor\": \"${color}\", \"potentialAction\": [{\"@context\": \"https://schema.org\", \"@type\": \"ViewAction\", \"name\": \"${actionName}\", \"target\": [\"${actionTarget}\"]}]}"
50+
# style should be one of 'good', 'attention', 'warning', 'default'
51+
# good (green) if the build succeeded
52+
# attention (red) if the build failed
53+
# warning (yellow) if the build errored
54+
# default (gray) by default
55+
# look for pass/fail/error in the title
56+
style=$(evaluate "$(jq -r '.params.style // "default"' < "${payload}")")
57+
[[ "${title}" =~ (pass|succeed|success) ]] && style="good"
58+
[[ "${title}" =~ (fail) ]] && style="attention"
59+
[[ "${title}" =~ (error) ]] && style="warning"
60+
# or in the text_output if we didn't find it in the title
61+
[[ "${style}" == "default" ]] && [[ "${text_output}" =~ (pass|succeed|success) ]] && style="good"
62+
[[ "${style}" == "default" ]] && [[ "${text_output}" =~ (fail) ]] && style="attention"
63+
[[ "${style}" == "default" ]] && [[ "${text_output}" =~ (error) ]] && style="warning"
64+
65+
DIR="${BASH_SOURCE%/*}"
66+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
67+
68+
body=$(jq -n --arg format "${format}" \
69+
--arg actionName "${actionName}" \
70+
--arg actionTarget "${actionTarget}" \
71+
--arg title "${title}" \
72+
--arg text "${text_output}" \
73+
--arg style "${style}" \
74+
-f ${DIR}/card.jq)
5275

5376
webhook_url="$(jq -r '.source.url // empty' < "${payload}")"
5477

0 commit comments

Comments
 (0)