Google Cloud Build Slack通知消息 “no preview available”

huangapple go评论57阅读模式
英文:

Google Cloud Build Slack Notifier message "no preview available"

问题

问题:

我在Google Cloud Run中配置了一个“Slack Notifier”服务,按照这个指南进行配置:https://cloud.google.com/build/docs/configuring-notifications/configure-slack

一切都运行得很好,构建会自动触发,并且我按照配置会收到Slack通知。但问题是Slack中的通知弹出窗口(Windows桌面客户端应用程序)显示“无可用预览”,我无法弄清楚如何使这部分工作。

Google Cloud Build Slack通知消息 “no preview available”

我已经在Google上搜索了几个小时,但无济于事,所以现在我希望在这里寻求帮助,希望有人知道如何解决这个问题。

到目前为止,我发现的是,如果消息没有文本内容,将显示“无可用预览”,但我的消息中有文本,下面是完整消息内容的预览:

Google Cloud Build Slack通知消息 “no preview available”

更多细节:

这是我的Slack Notifier配置yaml:

apiVersion: cloud-build-notifiers/v1
kind: SlackNotifier
metadata:
  name: example-slack-notifier
spec:
  notification:
    filter: build.status in [Build.Status.SUCCESS, Build.Status.FAILURE, Build.Status.TIMEOUT]
    params:
      buildStatus: $(build.status)
    delivery:
      webhookUrl:
        secretRef: webhook-url
    template:
      type: golang
      uri: gs://path-to-json-template.json
  secrets:
  - name: webhook-url
    value: projects/.../versions/latest

这是JSON模板:

[
  {
    "type": "section",
    "text": {
      "type": "plain_text",
      "text": "Cloud Build Completed"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "*BUILD DETAILS:*"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Status: {{.Params.buildStatus}}\nEnvironment: {{.Build.Substitutions._ENVIRONMENT}}"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "*GITHUB DETAILS:*"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Repo: {{.Build.Substitutions.REPO_NAME}}\nBranch: {{.Build.Substitutions.BRANCH_NAME}}\nPR: #{{.Build.Substitutions._PR_NUMBER}}"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "You can access your service here: {{.Build.Substitutions._DEV_SERVICE_URL}}"
    }
  },
  {
    "type": "divider"
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "View Build Logs"
    },
    "accessory": {
      "type": "button",
      "text": {
        "type": "plain_text",
        "text": "Logs"
      },
      "value": "click_me_123",
      "url": "{{.Build.LogUrl}}",
      "action_id": "button-action"
    }
  }
]

如果有人可以帮助我解决如何将“无可用预览”替换为有意义的内容,比如“Cloud Build Completed!”那将不胜感激。

英文:

The problem:

I have configure a "Slack Notifier" service in Google Cloud Run, following this guide: https://cloud.google.com/build/docs/configuring-notifications/configure-slack

Every thing is working pretty well, builds are triggered automatically and I do get my Slack notifications as configured. But the problem is that the notification popup in Slack (Windows Desktop client app) says "no preview available" and I can't figure out how to make that part work.

Google Cloud Build Slack通知消息 “no preview available”

I've been "Googling" this for hours to no avail, so now I'm reaching out here in the hope that someone knows how to resolve this.

So far all I've found is that "no preview available" will show if the message has no text content, but mine has text, here's a preview of the full message content:

Google Cloud Build Slack通知消息 “no preview available”

Further details:

Here is my Slack Notifier configuration yaml:

apiVersion: cloud-build-notifiers/v1
kind: SlackNotifier
metadata:
  name: example-slack-notifier
spec:
  notification:
    filter: build.status in [Build.Status.SUCCESS, Build.Status.FAILURE, Build.Status.TIMEOUT]
    params:
      buildStatus: $(build.status)
    delivery:
      webhookUrl:
        secretRef: webhook-url
    template:
      type: golang
      uri: gs://path-to-json-template.json
  secrets:
  - name: webhook-url
    value: projects/.../versions/latest

And here is the JSON template:

[
  {
    "type": "section",
    "text": {
      "type": "plain_text",
      "text": "Cloud Build Completed"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "*BUILD DETAILS:*"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Status: {{.Params.buildStatus}}\nEnvironment: {{.Build.Substitutions._ENVIRONMENT}}"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "*GITHUB DETAILS:*"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Repo: {{.Build.Substitutions.REPO_NAME}}\nBranch: {{.Build.Substitutions.BRANCH_NAME}}\nPR: #{{.Build.Substitutions._PR_NUMBER}}"
    }
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "You can access your service here: {{.Build.Substitutions._DEV_SERVICE_URL}}"
    }
  },
  {
    "type": "divider"
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "View Build Logs"
    },
    "accessory": {
      "type": "button",
      "text": {
        "type": "plain_text",
        "text": "Logs"
      },
      "value": "click_me_123",
      "url": "{{.Build.LogUrl}}",
      "action_id": "button-action"
    }
  }
]

If anyone can help me figure out how to replace "no preview available" with something meaningful like "Cloud Build Completed!" that would be greatly appreciated.

答案1

得分: 1

看起来这是一个Slack集成+Windows的问题,似乎Windows通知不支持富文本元素,和/或Slack Windows客户端未正确处理此问题和/或正确方式。

我在另一个项目中找到了类似的错误:https://github.com/SmartAPI/smartAPI/issues/97

Slack假定在使用他们的API发送消息或发布Webhooks时,您不仅会提供正确的blocks(数组)或attachments(数组)负载,如您在第二个代码片段(也是第二个截图)中提供的,还会提供纯文本text字符串负载,Slack将在Webhook或chat.postMessage Web API调用时将其用作备用。

如果您使用并部署了来自您问题中提到的他们的GitHub存储库的通知器代码:https://github.com/GoogleCloudPlatform/cloud-build-notifiers/,它们会使用具有attachments元素(数组)但没有提供text元素的PostWebHook(https://github.com/GoogleCloudPlatform/cloud-build-notifiers/blob/603e6b41ad5b90d11d4b33d8493d4d69622d16b5/slack/main.go#L136C2-L136C98)。

return &slack.WebhookMessage{Attachments: []slack.Attachment{{Color: clr, Blocks: blocks}}}, nil

我猜这是问题的根本原因,这是开发者的问题,或者您可以自行修改代码并添加一个Text字符串(https://github.com/slack-go/slack/blob/c4095cb17cf21c2b2b9c459d5512d14ca5143f64/webhooks.go#L19)。

英文:

Looks like this is a Slack integration+Windows problem, looks like Windows Notifications don't support rich text elements, and/or Slack Windows client does not handle this properly and/or in correct way.

I found similar error in some other project https://github.com/SmartAPI/smartAPI/issues/97

Slack supposes that you while sending messages or posting webhooks with their API would supply it not with only blocks (array) or attachments (array) payload as you correctly provided in the second code snippet (also a second screenshot), but also a plain text string payload Slack will use as a fallback both for a webhook or chat.postMessage web API call.

If you use and deploy a notifier code from their GitHub repository mentioned in your question, they call a PostWebHook with attachments element (array) but no text element provided.

return &slack.WebhookMessage{Attachments: []slack.Attachment{{Color: clr, Blocks: blocks}}}, nil

I guess this is a root cause of the problem, this is on the developer side, or you could modify the code yourself and add a Text string.

huangapple
  • 本文由 发表于 2023年7月11日 04:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定