name: Comment on Discussion When sdata/dist-arch/ Changes

on:
  push:
    paths:
      - 'sdata/dist-arch/**'
#  workflow_dispatch:

jobs:
  comment_on_discussion:
    runs-on: ubuntu-latest
    steps:
      - name: Create comment on discussion #2140
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          DISCUSSION_NUMBER: 2140
        # https://docs.github.com/en/graphql/guides/using-the-graphql-api-for-discussions
        # https://docs.github.com/en/graphql/reference/mutations#adddiscussioncomment
        run: |
          MESSAGE="**Auto notification:**\n"
          MESSAGE+="Directory \`sdata/dist-arch\` has been updated.\n"
          MESSAGE+="Commit HASH: ${{ github.sha }}\n"
          MESSAGE+="Commit message: ${{ github.event.head_commit.message }}"
          REPO_OWNER="${{ github.repository_owner }}"
          REPO_NAME="${{ github.event.repository.name }}"

          DISCUSSION_NODE_ID=$(gh api graphql -f query='
            query {
              repository( owner: "'${REPO_OWNER}'", name: "'${REPO_NAME}'" )
                { discussion(number: '${DISCUSSION_NUMBER}') { id } } 
              }' | \
            jq -r '.data.repository.discussion.id')
          gh api graphql -f query='
            mutation {
              addDiscussionComment(input:{
                discussionId: "'$DISCUSSION_NODE_ID'",
                body: "'"$MESSAGE"'",
              }) {
                clientMutationId
                comment {
                  id
                  body
                }
              }
            }
          '
