构造JSON,可用于Jenkins的SlackNotification插件



给定以下JSON示例(来自Slack网站),我尝试了许多方法来构建该消息。我尝试了net.sf.json.JSONObjectnet.sf.json.JSONArray。但是我无法正确完成。

我将对象解释为:

1 net.sf.json.JSONObject,带1个键值对'附件'= array

1 net.sf.json.JSONArray.1带有hashmap作为字段

1个字段值是参考 net.sf.json.JSONArray.2

一个组成1个元素

net.sf.json.JSONArray.2

此元素是另一个嵌套net.sf.json.JSONObject

我在做什么错?我没有附上代码示例,因为我尝试了很多次,并且意识到我可能做错了什么。

{
"attachments": [
    {
        "fallback": "Required plain-text summary of the attachment.",
        "color": "#36a64f",
        "pretext": "Optional text that appears above the attachment block",
        "author_name": "Bobby Tables",
        "author_link": "http://flickr.com/bobby/",
        "author_icon": "http://flickr.com/icons/bobby.jpg",
        "title": "Slack API Documentation",
        "title_link": "https://api.slack.com/",
        "text": "Optional text that appears within the attachment",
        "fields": [
            {
                "title": "Priority",
                "value": "High",
                "short": false
            }
        ],
        "image_url": "http://my-website.com/path/to/image.jpg",
        "thumb_url": "http://example.com/path/to/thumb.png",
        "footer": "Slack API",
        "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
        "ts": 123456789
    }
]
}

这个jenkinsfile有效:

#!/bin/groovy
node ('mynode') {
    stage ("Build") {
        def attachments = """[ { "text": "And here’s an attachment!" } ]"""
        echo (attachments)
        slackSend (channel: "@rolf", color: "danger", message: "Test message", attachments: attachments)
    }
}

这不是:

#!/bin/groovy
node ('mynode') {
    stage ("Build") {
        def attachments = """
[ { "text": "And here’s an attachment!" } ]"""
        echo (attachments)
        slackSend (channel: "@rolf", color: "danger", message: "Test message", attachments: attachments)
    }
}

所以答案是:确保附件字符串不是以newline开头的。

您应该能够使用以下简单脚本来实现它:

def json = new groovy.json.JsonBuilder()
json {
    attachments ([ 
        {
            fallback 'Required plain-text summary of the attachment.'
            color '#36a64f'
            pretext 'Optional text that appears above the attachment block'
            author_name 'Bobby Tables'
            author_link 'http://flickr.com/bobby/'
            author_icon 'http://flickr.com/icons/bobby.jpg'
            title 'Slack API Documentation'
            title_link 'https://api.slack.com/'
            text 'Optional text that appears within the attachment'
            fields([
                {
                            title 'Priority'
                            value 'High'
                            'short' false
                }
            ])
            image_url 'http://my-website.com/path/to/image.jpg'
            thumb_url 'http://example.com/path/to/thumb.png'
            footer 'Slack API'
            footer_icon 'https://platform.slack-edge.com/img/default_application_icon.png'
            ts 123456789
        }
    ])
}
println json.toPrettyString()

您可以快速在线尝试 demo

相关内容

  • 没有找到相关文章

最新更新