takafumi blog

日々の勉強メモ

github discussionのコメントをslackに投稿するaction

2022/07/24 現在、まだslack integrationが本家で対応していないのでざっくり書いてみた。

Get notifications of Discussions in slack (or equivalent) · Discussion #2844 · github-community/community · GitHub

ただしインジェクション対応など適当なので、privateで信頼できるところ以外では使わない方がよい。

詳しくはこの辺参照。

https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions

で以下がYAML

# .github/workflows/discussions_comment_to_slack_api.yml
on:
  discussion_comment:
    types: [created]
jobs:
  on_comment:
    runs-on: ubuntu-latest
    if: github.event.comment
    # outputs:
    #   formatted_body: ${{ steps.format.outputs.body }}
    steps:
      - name: 'format comment body & title'
        id: format
        run: |
          title='${{ github.event.discussion.title}}'
          body='${{ github.event.comment.body }}'
          # all newline convert to \n
          body=${body//$'\r'/'%0A'}
          body=${body//$'\n'/'%0A'}
          # escape double quote
          title=${title//\"/'%5C%22'}
          body=${body//\"/'%5C%22'}
          echo "title: $title"
          echo "body: $body"
          echo "::set-output name=title::${title}"
          echo "::set-output name=body::${body}"
      - name: 'request to slack'
        id: request
        run: |
          curl -X POST 'https://slack.com/api/chat.postMessage' \
            -d 'token=${{ secrets.SLACK_API_TOKEN_FOR_CHAT_WRITE}}' \
            -d 'channel=general' \
            -d 'blocks=[
                 {
                   "type": "section",
                   "text": {
                     "type": "mrkdwn",
                     "text": "<${{ github.event.comment.user.html_url}}|${{ github.event.comment.user.login }}> commented <${{ github.event.comment.html_url }}|${{ github.event.discussion.number }}/discussioncomment-${{ github.event.comment.id }}>"
                   }
                 },
                 {
                   "type": "divider"
                 },
                 {
                   "type": "section",
                   "text": {
                     "type": "mrkdwn",
                     "text": "*POST* ${{ github.event.discussion.category.emoji}}"
                   }
                 },
                 {
                   "type": "section",
                   "text": {
                     "type": "mrkdwn",
                     "text": "${{ steps.format.outputs.title }} #${{ github.event.discussion.number}}"
                   }
                 },
                 {
                   "type": "section",
                   "text": {
                     "type": "mrkdwn",
                     "text": "*COMMENT :speech_balloon:*"
                   }
                 },
                 {
                   "type": "section",
                   "text": {
                     "type": "mrkdwn",
                     "text": "${{ steps.format.outputs.body }}"
                   }
                 }
               ]'

後は、Slack側のBotchat:write の権限つけて、GitHubのsecretにtokenを入れてやる。

出力はこんな感じ

デザインを変更したい場合は、https://app.slack.com/block-kit-builder/ とかで blocks のところのデザイン作って書き換える。

そのうち本家で対応してくれるまでの間に合わせという事で。

github.com