|  | 
| 1 |  | -name: "discussion-webhook" | 
|  | 1 | +name: discussion-webhook | 
| 2 | 2 | on: | 
| 3 | 3 |   discussion: | 
| 4 |  | -    types: ["created", "answered", "unanswered"] | 
|  | 4 | +    types: | 
|  | 5 | +    - created | 
|  | 6 | +    - answered | 
|  | 7 | +    - unanswered | 
|  | 8 | + | 
| 5 | 9 | jobs: | 
| 6 | 10 |   notify: | 
| 7 | 11 |     runs-on: ubuntu-latest | 
| 8 | 12 |     steps: | 
| 9 |  | -      - name: "Google Chat notification" | 
| 10 |  | -        run: | | 
| 11 |  | -          title="${{ github.event.discussion.title }}" | 
| 12 |  | -          escaped_title="${title//\'/\\\'}" | 
| 13 |  | -          data="{ | 
| 14 |  | -            \"thread\": {\"threadKey\": \"${{github.event.discussion.number}}\"}, | 
| 15 |  | -            \"cardsV2\": [ | 
| 16 |  | -                { | 
| 17 |  | -                    \"cardId\": \"discussion-card\", | 
| 18 |  | -                    \"card\": { | 
| 19 |  | -                        \"header\": { | 
| 20 |  | -                            \"title\": \"Discussion #${{ github.event.discussion.number }} is ${{ github.event.action }}\", | 
| 21 |  | -                            \"subtitle\": \"Discussion tracker bot\" | 
| 22 |  | -                        }, | 
| 23 |  | -                        \"sections\": { | 
| 24 |  | -                            \"widgets\": [ | 
| 25 |  | -                                { | 
| 26 |  | -                                    \"decoratedText\": { | 
| 27 |  | -                                    \"topLabel\": \"Title\", | 
| 28 |  | -                                    \"text\": \"<b>${{ github.event.discussion.title }}</b>\", | 
| 29 |  | -                                    \"bottomLabel\": \"User: ${{ github.event.discussion.user.login }}\", | 
| 30 |  | -                                    } | 
| 31 |  | -                                }, | 
| 32 |  | -                                { | 
| 33 |  | -                                    \"decoratedText\": { | 
| 34 |  | -                                    \"topLabel\": \"Category\", | 
| 35 |  | -                                    \"text\": \"${{ github.event.discussion.category.name }}\" | 
| 36 |  | -                                    } | 
| 37 |  | -                                }, | 
| 38 |  | -                                { | 
| 39 |  | -                                    \"buttonList\": { | 
| 40 |  | -                                    \"buttons\": [ | 
| 41 |  | -                                        { | 
| 42 |  | -                                        \"text\": \"OPEN DISCUSSION\", | 
| 43 |  | -                                        \"onClick\": { | 
| 44 |  | -                                            \"openLink\": { | 
| 45 |  | -                                            \"url\": \"${{github.event.discussion.html_url}}\" | 
| 46 |  | -                                            } | 
| 47 |  | -                                        } | 
| 48 |  | -                                        } | 
| 49 |  | -                                    ] | 
| 50 |  | -                                    } | 
| 51 |  | -                                } | 
| 52 |  | -                            ] | 
| 53 |  | -                        } | 
| 54 |  | -                    } | 
| 55 |  | -                } | 
| 56 |  | -            ] | 
| 57 |  | -          }" | 
| 58 |  | -          [ $(curl --location -X POST '${{secrets.WEBHOOK_CHAT_URL}}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD' \ | 
| 59 |  | -          --header 'Content-Type: application/json' \ | 
| 60 |  | -          --data-raw "$data" -o /dev/null -w '%{http_code}') -eq 200 ] | 
|  | 13 | +    - name: Google Chat notification | 
|  | 14 | +      env: | 
|  | 15 | +        TITLE: ${{ github.event.discussion.title }} | 
|  | 16 | +        THREAD_NUMBER: ${{ github.event.discussion.number }} | 
|  | 17 | +        USER: ${{ github.event.discussion.user.login }} | 
|  | 18 | +        ACTION: ${{ github.event.action }} | 
|  | 19 | +        CATEGORY: ${{ github.event.discussion.category.name }} | 
|  | 20 | +        DISCUSSION_URL: ${{ github.event.discussion.html_url }} | 
|  | 21 | +        CHAT_URL: ${{ secrets.WEBHOOK_CHAT_URL }} | 
|  | 22 | +      run: | | 
|  | 23 | +        jq -nS --arg title "${TITLE}" \ | 
|  | 24 | +          --arg threadNumber "${THREAD_NUMBER}" \ | 
|  | 25 | +          --arg user "${USER}" \ | 
|  | 26 | +          --arg action "${ACTION}" \ | 
|  | 27 | +          --arg category "${CATEGORY}" \ | 
|  | 28 | +          --arg discussionURL "${DISCUSSION_URL}" \ | 
|  | 29 | +          '{ | 
|  | 30 | +            thread: { | 
|  | 31 | +              threadKey: "\($threadNumber)", | 
|  | 32 | +            }, | 
|  | 33 | +            cardsV2: [ | 
|  | 34 | +              cardId: "discussion-card", | 
|  | 35 | +              card: { | 
|  | 36 | +                header: { | 
|  | 37 | +                  title: "Discussion #\($title) is \($action)", | 
|  | 38 | +                  subtitle: "Discussion tracker bot", | 
|  | 39 | +                }, | 
|  | 40 | +                sections: { | 
|  | 41 | +                  widgets: [ | 
|  | 42 | +                    { | 
|  | 43 | +                      decoratedText: { | 
|  | 44 | +                        topLabel: "Title", | 
|  | 45 | +                        text: "<b>\($title)</b>", | 
|  | 46 | +                        bottomLabel: "User: \($user)", | 
|  | 47 | +                      }, | 
|  | 48 | +                    }, | 
|  | 49 | +                    { | 
|  | 50 | +                      decoratedText: { | 
|  | 51 | +                        topLabel: "Category", | 
|  | 52 | +                        text: $category, | 
|  | 53 | +                      }, | 
|  | 54 | +                    }, | 
|  | 55 | +                    { | 
|  | 56 | +                      buttonList: { | 
|  | 57 | +                        buttons: [ | 
|  | 58 | +                          { | 
|  | 59 | +                            text: "OPEN DISCUSSION", | 
|  | 60 | +                            onClick: { | 
|  | 61 | +                              openLink: { | 
|  | 62 | +                                url: $discussionURL, | 
|  | 63 | +                              }, | 
|  | 64 | +                            }, | 
|  | 65 | +                          }, | 
|  | 66 | +                        ] | 
|  | 67 | +                      }, | 
|  | 68 | +                    }, | 
|  | 69 | +                  ], | 
|  | 70 | +                }, | 
|  | 71 | +              }, | 
|  | 72 | +            ], | 
|  | 73 | +          }' > data.json | 
| 61 | 74 | 
 | 
| 62 |  | -      - name: "Send Mia-Assistant suggestion" | 
| 63 |  | -        if: ${{ github.event.action == 'created' }} | 
| 64 |  | -        env: | 
| 65 |  | -            WEBHOOK_CHAT_URL: ${{ secrets.WEBHOOK_CHAT_URL }} | 
| 66 |  | -            ASSISTANT_API_URL: ${{ secrets.ASSISTANT_API_URL }} | 
| 67 |  | -            ASSISTANT_M2M_AUTH: ${{ secrets.ASSISTANT_M2M_AUTH }} | 
| 68 |  | -            ASSISTANT_M2M_URL: ${{ secrets.ASSISTANT_M2M_URL }} | 
| 69 |  | -        run: | | 
| 70 |  | -            # Login M2M | 
|  | 75 | +        curl --silent --show-error --location \ | 
|  | 76 | +          --proto '=https' \ | 
|  | 77 | +          --tlsv1.2 \ | 
|  | 78 | +          --request POST \ | 
|  | 79 | +          --header "Content-Type: application/json" \ | 
|  | 80 | +          --data "@data.json" \ | 
|  | 81 | +          "${CHAT_URL}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD" | 
| 71 | 82 | 
 | 
| 72 |  | -            TOKEN=$(curl -X POST "$ASSISTANT_M2M_URL" \ | 
| 73 |  | -            -H "Content-Type: application/x-www-form-urlencoded" \ | 
| 74 |  | -            -H "Authorization: Basic $ASSISTANT_M2M_AUTH" \ | 
| 75 |  | -            -d "grant_type=client_credentials" | jq -r '.access_token') | 
| 76 |  | -             | 
| 77 |  | -            # Get discussion body | 
|  | 83 | +    - name: Send Mia-Assistant suggestion | 
|  | 84 | +      if: ${{ github.event.action == 'created' }} | 
|  | 85 | +      env: | 
|  | 86 | +        ASSISTANT_M2M_AUTH: ${{ secrets.ASSISTANT_M2M_AUTH }} | 
|  | 87 | +        ASSISTANT_M2M_URL: ${{ secrets.ASSISTANT_M2M_URL }} | 
|  | 88 | +        DISCUSSION_TITLE: ${{ github.event.discussion.title }} | 
|  | 89 | +        DISCUSSION_BODY: ${{ github.event.discussion.body }} | 
|  | 90 | +        THREAD_NUMBER: ${{ github.event.discussion.number }} | 
|  | 91 | +        CHAT_URL: ${{ secrets.WEBHOOK_CHAT_URL }} | 
|  | 92 | +        ASSISTANT_API_URL: ${{ secrets.ASSISTANT_API_URL }} | 
|  | 93 | +      run: | | 
|  | 94 | +        # Login via Token | 
|  | 95 | +        curl --fail --silent --show-error --location \ | 
|  | 96 | +          --proto '=https' \ | 
|  | 97 | +          --tlsv1.2 \ | 
|  | 98 | +          --request POST \ | 
|  | 99 | +          --header "Content-Type: application/x-www-form-urlencoded" \ | 
|  | 100 | +          --header "AuthorizationBasic ${ASSISTANT_M2M_AUTH}" \ | 
|  | 101 | +          --data "grant_type=client_credentials" \ | 
|  | 102 | +          "${ASSISTANT_M2M_URL}" -o m2m_response.json || true | 
| 78 | 103 | 
 | 
| 79 |  | -            BODY=$(echo '${{ github.event.discussion.body }}' | sed 's/\\n//g') | 
| 80 |  | -            DISCUSSION_BODY="title:${{ github.event.discussion.title }}\nbody:$BODY" | 
|  | 104 | +        if [ ! -f "m2m_response.json" ]; then | 
|  | 105 | +          echo "Login via m2m has failed..." | 
|  | 106 | +          exit 0 | 
|  | 107 | +        fi | 
| 81 | 108 | 
 | 
| 82 |  | -            # Get suggestion from Mia-Assistant | 
|  | 109 | +        # Ask assistant for a response | 
|  | 110 | +        jq -nS --arg title "${DISCUSSION_TITLE}" --arg body "${DISCUSSION_BODY}" '{ | 
|  | 111 | +          chat_query: "title:\($title)\nbody:\($body)", | 
|  | 112 | +          chat_history: [], | 
|  | 113 | +        }' > data.json | 
| 83 | 114 | 
 | 
| 84 |  | -            QUERY=$(jq -n --arg chatQuery "$DISCUSSION_BODY" '{chat_query: $chatQuery, chat_history: []}') | 
| 85 |  | -            RESPONSE=$(curl -v -X POST "$ASSISTANT_API_URL" \ | 
| 86 |  | -                -H "Authorization: Bearer $TOKEN" \ | 
| 87 |  | -                -H 'Content-Type: application/json' \ | 
| 88 |  | -                --data-raw "$QUERY") | 
| 89 |  | -                 | 
| 90 |  | -            MESSAGE=$(echo "$RESPONSE" | jq -r '.message') | 
| 91 |  | -            REFERENCE_LINKS=$(echo "$RESPONSE" | jq -r '[.references[].url] | join(", ")') | 
|  | 115 | +        curl --fail --silent --show-error --location \ | 
|  | 116 | +          --proto '=https' \ | 
|  | 117 | +          --tlsv1.2 \ | 
|  | 118 | +          --request POST \ | 
|  | 119 | +          --header "Authorization: Bearer: $(jq -r '.access_token' m2m_response.json)" \ | 
|  | 120 | +          --header "Content-Type: application/json" \ | 
|  | 121 | +          --data "@data.json" \ | 
|  | 122 | +          "${ASSISTANT_API_URL}" -o assistant_response.json || true | 
| 92 | 123 | 
 | 
| 93 |  | -            # Send suggestion to the discussion thread | 
|  | 124 | +        if [ ! -f "assistant_response.json" ]; then | 
|  | 125 | +          echo "Assistant request has failed..." | 
|  | 126 | +          exit 0 | 
|  | 127 | +        fi | 
| 94 | 128 | 
 | 
| 95 |  | -            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') | 
|  | 129 | +        # Send suggestion to the discussion thread | 
|  | 130 | +        jq -nS --arg threadNumber "${THREAD_NUMBER}" \ | 
|  | 131 | +          --arg message "$(jq -r '.message' assistant_response.json)" \ | 
|  | 132 | +          --arg referenceLinks "$(jq -r '[.references[].url] | join(\", \")' assistant_response.json)" \ | 
|  | 133 | +          '{ | 
|  | 134 | +            thread: { | 
|  | 135 | +              threadKey: "\($threadNumber)", | 
|  | 136 | +            }, | 
|  | 137 | +            text: "*This is a suggestion generated by Mia-Assistant.*\n\n*Please leave a feedback!*\n --- \n\($message)\n\nReferences links:\n\n\($referenceLinks).", | 
|  | 138 | +          }' > data.json | 
| 96 | 139 | 
 | 
| 97 |  | -            [ $(curl --location -X POST '${{secrets.WEBHOOK_CHAT_URL}}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD' \ | 
| 98 |  | -            --header 'Content-Type: application/json' \ | 
| 99 |  | -            --data-raw "$DATA" -o /dev/null -w '%{http_code}') -eq 200 ] | 
|  | 140 | +        curl --silent --show-error --location \ | 
|  | 141 | +          --proto '=https' \ | 
|  | 142 | +          --tlsv1.2 \ | 
|  | 143 | +          --request POST \ | 
|  | 144 | +          --header "Content-Type: application/json" \ | 
|  | 145 | +          --data "@data.json" \ | 
|  | 146 | +          "${CHAT_URL}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD" | 
0 commit comments