I want to send a string to a Slack channel, and have this test code:
@Library('my-shared-library') _
import groovy.json.JsonOutput
final String diffs = """
diff --git a/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java b/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java
index e3c9c4c2741..211b08629ca 100644
--- a/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java
+++ b/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java
@@ -22,6 +22,12 @@ public enum SchemaVersionV2 implements CompanySchema {
JIRA_2295(2295,0,0, new String[] {"alter_JIRA_2295_1.sql"}, "create ai_expert_kb_sync table"),
JIRA_3693(3693, 1, 0, new String[]{"alter_JIRA_3693_1.sql"}, "Adding user-level tracking of a subscriptions' type (following vs connected)"),
JIRA_3689(3689,0,0, new String[] {"alter_JIRA_3689_1.sql"}, "create supplier message tables"),
+ JIRA_4773(4773,1,0, new String[]{"alter_JIRA_4773_1.sql"}, "Add index on partner_social_feed_post_approval table"),
+ JIRA_2339(DB_LOCATION.ZIFTDM,2339,0,0, new String[] {"alter_JIRA2339_1.sql"}, "Add lead source id"),
+ JIRA_3702(3702, 1, 0, new String[]{"alter_JIRA_3702_1.sql"}, "Adding z123_client ids to subscriptions"),
+ JIRA_4397(4397,1,0, new String[]{"alter_JIRA_4397.sql"}, "Add name, description, effective dates and thumbnail url to supplier_user_activity"),
+ JIRA_2317(2317,1,0, new String[]{"alter_JIRA_2317_1.sql"}, "Add column neverExpire to client table"),
+ JIRA_4374_1(4374, 1, 0, new String[]{"alter_JIRA4374_1.sql"}, "Adding partner filter to supplier event activity table"),
;
public static boolean isSuitableForSchemaUpdate(int major, int release) {
"""
def notifySlack(String text, String channel) {
def slackURL = ''
def payload = JsonOutput.toJson([text : text,
channel : channel,
username : "jenkins",
icon_emoji: ":jenkins:"])
try {
sh(script: "curl -X POST --data-urlencode 'payload=${payload}' ${slackURL}", returnStatus: true)
}
catch(Exception ex) {
println "Slack app may be down, error(if any): "+ex.toString()
}
}
node('build-node') {
stage("Notify Slack") {
final String testMsg = "<channel!> Schema changed:\n```${diffs}```"
notifySlack(testMsg, "#slack-channel")
}
}
When I run it, I get
/home/jenkins/workspace/Utilities/Playground/test-slack@tmp/durable-96cd2c08/script.sh.copy: 1: Syntax error: "(" unexpected
In my real code, the diffs
string is dynamically generated. Why am I getting the error and what's the workaround?
If I change the diffs
string to a simple Hello world!
it works.
I printed out the payload
in the notifySlack()
function and I get
{"text":"<!channel> Schema changed:\n```diff --git a/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java b/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java\nindex e3c9c4c2741..211b08629ca 100644\n--- a/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java\n+++ b/company-core/src/main/java/com/company/common/metadata/SchemaVersionV2.java\n@@ -22,6 +22,12 @@ public enum SchemaVersionV2 implements CompanySchema {\n JIRA_2295(2295,0,0, new String[] {\"alter_JIRA_2295_1.sql\"}, \"create ai_expert_kb_sync table\"),\n JIRA_3693(3693, 1, 0, new String[]{\"alter_JIRA_3693_1.sql\"}, \"Adding user-level tracking of a subscriptions' type (following vs connected)\"),\n JIRA_3689(3689,0,0, new String[] {\"alter_JIRA_3689_1.sql\"}, \"create supplier message tables\"),\n+ JIRA_4773(4773,1,0, new String[]{\"alter_JIRA_4773_1.sql\"}, \"Add index on partner_social_feed_post_approval table\"),\n+ JIRA_2339(DB_LOCATION.ZIFTDM,2339,0,0, new String[] {\"alter_JIRA2339_1.sql\"}, \"Add lead source id\"),\n+ JIRA_3702(3702, 1, 0, new String[]{\"alter_JIRA_3702_1.sql\"}, \"Adding z123_client ids to subscriptions\"),\n+ JIRA_4397(4397,1,0, new String[]{\"alter_JIRA_4397.sql\"}, \"Add name, description, effective dates and thumbnail url to supplier_user_activity\"),\n+ JIRA_2317(2317,1,0, new String[]{\"alter_JIRA_2317_1.sql\"}, \"Add column neverExpire to client table\"),\n+ JIRA_4374_1(4374, 1, 0, new String[]{\"alter_JIRA4374_1.sql\"}, \"Adding partner filter to supplier event activity table\"),\n ;\n\n public static boolean isSuitableForSchemaUpdate(int major, int release) {```","channel":"#ecs-feature","username":"jenkins","icon_emoji":":jenkins:"}