Skip to content

discussion-webhook #821

discussion-webhook

discussion-webhook #821

name: "discussion-webhook"
on:
discussion:
types: ["created", "answered", "unanswered"]
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: "Google Chat notification"
run: |
title="${{ github.event.discussion.title }}"
escaped_title="${title//\'/\\\'}"
data="{
\"thread\": {\"threadKey\": \"${{github.event.discussion.number}}\"},
\"cardsV2\": [
{
\"cardId\": \"discussion-card\",
\"card\": {
\"header\": {
\"title\": \"Discussion #${{ github.event.discussion.number }} is ${{ github.event.action }}\",
\"subtitle\": \"Discussion tracker bot\"
},
\"sections\": {
\"widgets\": [
{
\"decoratedText\": {
\"topLabel\": \"Title\",
\"text\": \"<b>${{ github.event.discussion.title }}</b>\",
\"bottomLabel\": \"User: ${{ github.event.discussion.user.login }}\",
}
},
{
\"decoratedText\": {
\"topLabel\": \"Category\",
\"text\": \"${{ github.event.discussion.category.name }}\"
}
},
{
\"buttonList\": {
\"buttons\": [
{
\"text\": \"OPEN DISCUSSION\",
\"onClick\": {
\"openLink\": {
\"url\": \"${{github.event.discussion.html_url}}\"
}
}
}
]
}
}
]
}
}
}
]
}"
[ $(curl --location -X POST '${{secrets.WEBHOOK_CHAT_URL}}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD' \
--header 'Content-Type: application/json' \
--data-raw "$data" -o /dev/null -w '%{http_code}') -eq 200 ]
- name: "Send Mia-Assistant suggestion"
if: ${{ github.event.action == 'created' }}
env:
WEBHOOK_CHAT_URL: ${{ secrets.WEBHOOK_CHAT_URL }}
ASSISTANT_API_URL: ${{ secrets.ASSISTANT_API_URL }}
ASSISTANT_M2M_AUTH: ${{ secrets.ASSISTANT_M2M_AUTH }}
ASSISTANT_M2M_URL: ${{ secrets.ASSISTANT_M2M_URL }}
run: |
# Login M2M
TOKEN=$(curl -X POST "$ASSISTANT_M2M_URL" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: Basic $ASSISTANT_M2M_AUTH" \
-d "grant_type=client_credentials" | jq -r '.access_token')
# Get discussion body
BODY=$(echo '${{ github.event.discussion.body }}' | sed 's/\\n//g')
DISCUSSION_BODY="title:${{ github.event.discussion.title }}\nbody:$BODY"
# Get suggestion from Mia-Assistant
QUERY=$(jq -n --arg chatQuery "$DISCUSSION_BODY" '{chat_query: $chatQuery, chat_history: []}')
RESPONSE=$(curl -v -X POST "$ASSISTANT_API_URL" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
--data-raw "$QUERY")
MESSAGE=$(echo "$RESPONSE" | jq -r '.message')
REFERENCE_LINKS=$(echo "$RESPONSE" | jq -r '[.references[].url] | join(", ")')
# Send suggestion to the discussion thread
DATA=$(jq -n --arg text "*This is a suggestion generated by Mia-Assistant.*\n\n*Please leave a feedback!!!*\n --- \n$MESSAGE\n\nReferences links:\n\n$REFERENCE_LINKS." '{thread: {"threadKey": "${{ github.event.discussion.number }}"}, text: $text}' | sed 's/\\\\n/\\n/g')
[ $(curl --location -X POST '${{secrets.WEBHOOK_CHAT_URL}}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD' \
--header 'Content-Type: application/json' \
--data-raw "$DATA" -o /dev/null -w '%{http_code}') -eq 200 ]