Skip to content

Commit 8203d32

Browse files
author
Nicholas Mathison
committed
Final Slack Channels edit
- Finishes up Issue #3 and PR #4
1 parent 2d43048 commit 8203d32

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

src/main/scripts/postToSlackAttachment.groovy

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final def slackUsername = props['username'];
2525
final def emoji = props['emoji'];
2626
final def slackAttachment = props['attachment'];
2727

28-
slackChannels.eachLine { slackChannel ->
28+
slackChannels.each { slackChannel ->
2929
slackChannel = URLDecoder.decode(slackChannel, "UTF-8" );
3030
if (!slackChannel.startsWith("@") && !slackChannel.startsWith("#")) {
3131
throw new RuntimeException("ERROR:: Invalid slack channel format passed: '${slackChannel}'. Must start with either # or @.")
@@ -53,7 +53,9 @@ attachmentJson.attachments.each { attachment ->
5353
attachment.ts = currentTime
5454
}
5555
}
56-
slackChannels.eachLine { slackChannel ->
56+
57+
int countFails = 0
58+
slackChannels.each { slackChannel ->
5759
// JSON message composition
5860
def json = new JsonBuilder();
5961
try {
@@ -66,8 +68,7 @@ slackChannels.eachLine { slackChannel ->
6668
println "DEBUG:: JSON Payload"
6769
println json.toPrettyString();
6870
} catch (Exception exception) {
69-
println "ERROR:: setting path: ${exception.message}"
70-
System.exit(1)
71+
throw new Exception("ERROR:: setting path: ${exception.message}")
7172
}
7273

7374
// HTTP POST to Slack
@@ -84,19 +85,21 @@ slackChannels.eachLine { slackChannel ->
8485
def status = http.executeMethod(post);
8586

8687
if (status == 200){
87-
println "Success: ${status}";
88-
System.exit(0);
88+
println "${status} Success at '${slackChannel}'";
8989
} else {
90-
println "Failure: ${status}"
91-
System.exit(3);
90+
println "${status} Failure at '${slackChannel}'"
91+
countFails++
9292
}
9393
} catch (Exception e) {
9494
println "ERROR:: Unable to set path: ${e.message}"
9595
println "[Possible Solution] Confirm the properties by running the Webhook with its associated JSON body in a REST Client."
9696
System.exit(2)
9797
}
9898
}
99-
99+
if (countFails > 0) {
100+
println "ERROR:: One of the messages failed to send. View the above logs to determine the source."
101+
System.exit(1)
102+
}
100103
void printSampleAttachmentPayload() {
101104
println "==== Sample Attachment JSON ===="
102105
println "{"

0 commit comments

Comments
 (0)