@@ -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
149165description : |
150166 Extended Slack message that does a lookup of the user by email and messages directly.
0 commit comments