Skip to content

Commit bec8ea5

Browse files
authored
Merge pull request #6 from commitdev/slack-message
Slack message
2 parents 435acd2 + 3e5215c commit bec8ea5

5 files changed

Lines changed: 157 additions & 108 deletions

File tree

src/commitdev/slack-message/orb.yml

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ commands:
1919
Enter either your bot token here or use the CircleCI UI to add
2020
your token under the 'SLACK_BOT_TOKEN' env var. https://api.slack.com/docs/token-types#bot
2121
type: string
22-
channel:
22+
fallback-channel:
23+
default: ""
2324
description: |
2425
Channel to send default branch messages to, or messages where email lookup fails.
2526
type: string
@@ -44,6 +45,11 @@ commands:
4445
description: |
4546
Whether or not to include the Project field in the message
4647
type: boolean
48+
fail_only:
49+
default: false
50+
description: |
51+
Set to true to only send notifications on failed jobs.
52+
type: boolean
4753
steps:
4854
- run:
4955
name: Check Dependencies
@@ -92,59 +98,69 @@ commands:
9298
if [[ "$SUCCESS" == "true" && "$CIRCLE_BRANCH" != "<< parameters.default_branch >>" ]]; then
9399
USER_ID=$(echo "$LOOKUP_RESULT" | jq -r ".user.id")
94100
CHANNEL=$USER_ID
95-
MENTION="@${USER_ID} "
101+
NAME=$(echo "$LOOKUP_RESULT" | jq -r ".user.name")
102+
MENTION="<@${USER_ID}>"
96103
else
97104
echo "Couldn't find user with the email \"${AUTHOR_EMAIL}\". Your email in Github must match your email in slack."
98-
CHANNEL=<< parameters.channel >>
105+
CHANNEL=<< parameters.fallback-channel >>
106+
NAME=$CHANNEL
99107
fi
100108
101-
echo "Sending message to ${CHANNEL}"
102-
SEND_RESULT=$(curl -s -X POST -H 'Content-type: application/x-www-form-urlencoded' --data \
103-
"token=${SLACK_BOT_TOKEN}&\
104-
channel=${CHANNEL}&\
105-
text=${MENTION}<< parameters.message >>&\
106-
attachments=[{\
107-
\"fallback\": \"Your CircleCI build ${BUILD_RESULT}. ${CIRCLE_BUILD_URL}\", \
108-
\"author_name\": \"${CIRCLE_USERNAME}\", \
109-
\"author_link\": \"https://${VCS_URL}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commit/${CIRCLE_SHA1}\", \
110-
\"title\": \"CircleCI Build\", \
111-
\"title_link\": \"${CIRCLE_BUILD_URL}\", \
112-
\"fields\": [ \
113-
<<# parameters.include_project_field >>
114-
{ \
115-
\"title\": \"Project\", \
116-
\"value\": \"${CIRCLE_PROJECT_REPONAME}\", \
117-
\"short\": true \
118-
}, \
119-
<</ parameters.include_project_field >>
120-
<<# parameters.include_job_number_field >>
121-
{ \
122-
\"title\": \"Job Number\", \
123-
\"value\": \"${CIRCLE_BUILD_NUM}\", \
124-
\"short\": true \
125-
}, \
126-
<</ parameters.include_job_number_field >>
127-
{ \
128-
\"title\": \"Branch\", \
129-
\"value\": \"${CIRCLE_BRANCH}\", \
130-
\"short\": true \
131-
}, \
132-
{ \
133-
\"title\": \"Stage\", \
134-
\"value\": \"${CIRCLE_STAGE}\", \
135-
\"short\": true \
136-
} \
137-
], \
138-
\"color\": \"${COLOUR}\", \
139-
\"footer\": \"Commit Orb\", \
140-
\"footer_icon\": \"https://commit.dev/favicon.ico\", \
141-
}]" \
142-
"${SLACK_API_URL}/chat.postMessage?token=${SLACK_BOT_TOKEN}")
143-
SUCCESS=$(echo "$SEND_RESULT" | jq ".ok")
144-
if [ "$SUCCESS" == "true" ]; then
145-
echo "Message sent to user."
109+
if [[ "<< parameters.fail_only >>" == true && "$BUILD_RESULT" == "success" ]]; then
110+
echo "Build passing and fail_only is set. No message sent."
146111
else
147-
echo "Error sending message to user. ${SEND_RESULT}"
112+
if [[ "$CHANNEL" != "" ]]; then
113+
echo "Sending message to ${NAME}($CHANNEL)"
114+
SEND_RESULT=$(curl -s -X POST -H 'Content-type: application/x-www-form-urlencoded' --data \
115+
"token=${SLACK_BOT_TOKEN}&\
116+
channel=${CHANNEL}&\
117+
text=${MENTION} << parameters.message >>&\
118+
attachments=[{\
119+
\"fallback\": \"Your CircleCI build ${BUILD_RESULT}. ${CIRCLE_BUILD_URL}\", \
120+
\"author_name\": \"${CIRCLE_USERNAME}\", \
121+
\"author_link\": \"https://${VCS_URL}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commit/${CIRCLE_SHA1}\", \
122+
\"title\": \"CircleCI Build\", \
123+
\"title_link\": \"${CIRCLE_BUILD_URL}\", \
124+
\"fields\": [ \
125+
<<# parameters.include_project_field >>
126+
{ \
127+
\"title\": \"Project\", \
128+
\"value\": \"${CIRCLE_PROJECT_REPONAME}\", \
129+
\"short\": true \
130+
}, \
131+
<</ parameters.include_project_field >>
132+
<<# parameters.include_job_number_field >>
133+
{ \
134+
\"title\": \"Job Number\", \
135+
\"value\": \"${CIRCLE_BUILD_NUM}\", \
136+
\"short\": true \
137+
}, \
138+
<</ parameters.include_job_number_field >>
139+
{ \
140+
\"title\": \"Branch\", \
141+
\"value\": \"${CIRCLE_BRANCH}\", \
142+
\"short\": true \
143+
}, \
144+
{ \
145+
\"title\": \"Stage\", \
146+
\"value\": \"${CIRCLE_STAGE}\", \
147+
\"short\": true \
148+
} \
149+
], \
150+
\"color\": \"${COLOUR}\", \
151+
\"footer\": \"Commit Orb\", \
152+
\"footer_icon\": \"https://commit.dev/favicon.ico\", \
153+
}]" \
154+
"${SLACK_API_URL}/chat.postMessage?token=${SLACK_BOT_TOKEN}")
155+
SUCCESS=$(echo "$SEND_RESULT" | jq ".ok")
156+
if [ "$SUCCESS" == "true" ]; then
157+
echo "Message sent to user."
158+
else
159+
echo "Error sending message to user. ${SEND_RESULT}"
160+
fi
161+
else
162+
echo "No fallback-channel specified, message not sent."
163+
fi
148164
fi
149165
description: |
150166
Extended Slack message that does a lookup of the user by email and messages directly.

src/commitdev/slack-message/test-slack-message.sh

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
# To run the tests use:
1818
# export $(grep -v '^#' .env | xargs -d '\n') >/dev/null && ./test-slack-message.sh
1919

20+
SLACK_API_URL="https://slack.com/api/"
2021

21-
SLACK_API_URL="https://slack.com/api"
22+
# Only support Github for now.
23+
VCS="gh"
2224
VCS_URL="github.com"
23-
SHORT_SHA=$(echo "${CIRCLE_SHA1}" | cut -c1-7)
25+
# AUTHOR_EMAIL=$(git log -n1 --format='%ae' $CIRCLE_SHA1)
26+
MENTION=""
27+
2428
FAIL_COLOUR="#ae2d2d"
2529
PASS_COLOUR="#38ae2d"
2630

@@ -30,59 +34,57 @@ else
3034
COLOUR=${FAIL_COLOUR}
3135
fi
3236

33-
LOOKUP_RESULT=$(curl -s -H 'Content-type: application/x-www-form-urlencoded' "${SLACK_API_URL}/users.lookupByEmail?token=${SLACK_BOT_TOKEN}&email=${AUTHOR_EMAIL}")
37+
LOOKUP_RESULT=$(curl -s "${SLACK_API_URL}/users.lookupByEmail?token=${SLACK_BOT_TOKEN}&email=${AUTHOR_EMAIL}")
3438
SUCCESS=$(echo "$LOOKUP_RESULT" | jq ".ok")
35-
if [[ "$SUCCESS" == "true" && "$CIRCLE_BRANCH" != "master" ]]; then
39+
if [[ "$SUCCESS" == "true" && "$CIRCLE_BRANCH" != "$DEFAULT_BRANCH" ]]; then
3640
USER_ID=$(echo "$LOOKUP_RESULT" | jq -r ".user.id")
3741
CHANNEL=$USER_ID
38-
MENTION="@${USER_ID} "
42+
NAME=$(echo "$LOOKUP_RESULT" | jq -r ".user.name")
43+
MENTION="<@${USER_ID}>"
3944
else
40-
echo "Couldn't find user with the email ${AUTHOR_EMAIL}. Your email in Github must match your email in slack."
41-
echo "Response: ${LOOKUP_RESULT}"
42-
CHANNEL=staging-deploy
45+
echo "Couldn't find user with the email \"${AUTHOR_EMAIL}\". Your email in Github must match your email in slack."
46+
CHANNEL=$FALLBACK_CHANNEL
47+
NAME=$CHANNEL
4348
fi
4449

45-
echo "Sending message to ${CHANNEL}"
46-
SEND_RESULT=$(curl -s -X POST -H 'Content-type: application/x-www-form-urlencoded' --data \
47-
"token=${SLACK_BOT_TOKEN}&\
48-
channel=${CHANNEL}&\
49-
text=Your CircleCI build ${BUILD_RESULT}.&\
50-
attachments=[{\
51-
\"fallback\": \"Your CircleCI build ${BUILD_RESULT}. ${CIRCLE_BUILD_URL}\", \
52-
\"author_name\": \"${SHORT_SHA}\", \
53-
\"author_link\": \"https://${VCS_URL}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commit/${CIRCLE_SHA1}\", \
54-
\"title\": \"CircleCI Build ${BUILD_RESULT}\", \
55-
\"title_link\": \"${CIRCLE_BUILD_URL}\", \
56-
\"fields\": [ \
57-
{ \
58-
\"title\": \"Project\", \
59-
\"value\": \"${CIRCLE_PROJECT_REPONAME}\", \
60-
\"short\": true \
61-
}, \
62-
{ \
63-
\"title\": \"Job Number\", \
64-
\"value\": \"${CIRCLE_BUILD_NUM}\", \
65-
\"short\": true \
66-
}, \
67-
{ \
68-
\"title\": \"Branch\", \
69-
\"value\": \"${CIRCLE_BRANCH}\", \
70-
\"short\": true \
71-
}, \
72-
{ \
73-
\"title\": \"Stage\", \
74-
\"value\": \"${CIRCLE_STAGE}\", \
75-
\"short\": true \
76-
} \
77-
], \
78-
\"color\": \"${COLOUR}\", \
79-
\"footer\": \"Commit Orb\", \
80-
\"footer_icon\": \"https://commit.dev/favicon.ico\", \
81-
}]" \
82-
"${SLACK_API_URL}/chat.postMessage?token=${SLACK_BOT_TOKEN}")
83-
SUCCESS=$(echo "$SEND_RESULT" | jq ".ok")
84-
if [ "$SUCCESS" == "true" ]; then
85-
echo "Message sent to user."
50+
if [[ "$FAIL_ONLY" == true && "$BUILD_RESULT" == "success" ]]; then
51+
echo "Build passing and fail_only is set. No message sent."
8652
else
87-
echo "Error sending message to user. ${SEND_RESULT}"
88-
fi
53+
if [[ "$CHANNEL" != "" ]]; then
54+
echo "Sending message to ${NAME}($CHANNEL)"
55+
SEND_RESULT=$(curl -s -X POST -H 'Content-type: application/x-www-form-urlencoded' --data \
56+
"token=${SLACK_BOT_TOKEN}&\
57+
channel=${CHANNEL}&\
58+
text=${MENTION}%20${MESSAGE}&\
59+
attachments=[{\
60+
\"fallback\": \"Your CircleCI build result: ${BUILD_RESULT}. ${CIRCLE_BUILD_URL}\", \
61+
\"author_name\": \"${CIRCLE_USERNAME}\", \
62+
\"author_link\": \"https://${VCS_URL}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commit/${CIRCLE_SHA1}\", \
63+
\"title\": \"CircleCI Build\", \
64+
\"title_link\": \"${CIRCLE_BUILD_URL}\", \
65+
\"fields\": [ \
66+
{ \
67+
\"title\": \"Branch\", \
68+
\"value\": \"${CIRCLE_BRANCH}\", \
69+
\"short\": true \
70+
}, \
71+
{ \
72+
\"title\": \"Stage\", \
73+
\"value\": \"${CIRCLE_STAGE}\", \
74+
\"short\": true \
75+
} \
76+
], \
77+
\"color\": \"${COLOUR}\", \
78+
\"footer\": \"Commit Orb\", \
79+
\"footer_icon\": \"https://commit.dev/favicon.ico\", \
80+
}]" "${SLACK_API_URL}/chat.postMessage?token=${SLACK_BOT_TOKEN}")
81+
SUCCESS=$(echo "$SEND_RESULT" | jq ".ok")
82+
if [ "$SUCCESS" == "true" ]; then
83+
echo "Message sent to user."
84+
else
85+
echo "Error sending message to user. ${SEND_RESULT}"
86+
fi
87+
else
88+
echo "No fallback-channel specified, message not sent."
89+
fi
90+
fi
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.2
1+
0.0.3

src/commitdev/version-tag/orb.yml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
version: 2.1
44

55
commands:
6-
set:
6+
create:
77
parameters:
88
format:
99
default: 'number-tag'
@@ -60,17 +60,48 @@ commands:
6060
echo "Saving version-tag to << parameters.workspace >>/<< parameters.versionfile >>"
6161
echo "$VERSION_TAG" >> << parameters.workspace >>/<< parameters.versionfile >>
6262
echo "export VERSION_TAG=\"$CIRCLE_BUILD_NUM-$COMMIT_TAG\"" >> $BASH_ENV
63+
64+
echo "Created version tag: VERSION_TAG=${VERSION_TAG}
65+
This will be available in all steps in this job,
66+
but not in other jobs as part of the workflow.
67+
To use in other jobs, add the version-tag/get command in them."
6368
fi
6469
- persist_to_workspace:
6570
root: << parameters.workspace >>
6671
paths:
6772
- << parameters.versionfile >>
73+
74+
get:
75+
parameters:
76+
workspace:
77+
default: /tmp/workspace
78+
description: |
79+
Specify a custom workspace for this script to use. If you're using another workspace throughout your
80+
jobs you can add it here to. This is where we will save the file with the version-tag to access in
81+
other jobs in your workflow.
82+
type: string
83+
versionfile:
84+
default: version.txt
85+
description: |
86+
Plain-text file in which the version-tag is stored. To access this in other steps, attach the workspace
87+
and cat the contents of the file where you need it. E.g. export VERSION_TAG=$(cat version.txt)
88+
type: string
89+
steps:
90+
- attach_workspace:
91+
at: << parameters.workspace >>
6892
- run:
69-
name: Print Version
93+
name: Get Version Tag
7094
command: |
71-
echo "Created version tag: VERSION_TAG=${VERSION_TAG}
72-
This will be available in all steps in this job,
73-
but not in other jobs as part of the workflow.
74-
To use in other jobs, add the version-tag command to them."
95+
if [ -f << parameters.workspace >>/<< parameters.versionfile >> ]; then
96+
echo "<< parameters.versionfile >> already present in your workspace, setting enviroment variable."
97+
echo "export VERSION_TAG=$(cat << parameters.workspace >>/<< parameters.versionfile >>)" >> $BASH_ENV
98+
echo "Got version tag: VERSION_TAG=$(cat << parameters.workspace >>/<< parameters.versionfile >>)
99+
This will be available in all steps in this job,
100+
but not in other jobs as part of the workflow.
101+
To use in other jobs, add the version-tag/get command in them."
102+
else
103+
echo "Unabled to file version tag in << parameters.workspace >>/<< parameters.versionfile >>, run the create command first."
104+
exit 1
105+
fi
75106
description: |
76107
Common commands used by Commit in our build pipelines. See this orb's source: https://github.com/commitdev/circleci-orbs
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.2
1+
0.0.3

0 commit comments

Comments
 (0)