Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GHA: Notify Mattermost #5274

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
70 changes: 70 additions & 0 deletions .github/actions/scripts/send_chat_message.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env -S bash -euo pipefail

#
# Copyright (c) 2024 DuckDuckGo
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if [ -z "${MM_AUTH_TOKEN:-}" ]; then
echo 'MM_AUTH_TOKEN is not set!'
exit 1
fi

if [ -z "${MM_TEAM_ID:-}" ]; then
echo 'MM_TEAM_ID is not set!'
exit 1
fi

message="${1:-test}"
channel_name="david-test-channel"
username="dax"
priority="empty" # empty, important, or urgent
request_ack="false"
# Same icon as https://dub.duckduckgo.com/orgs/duckduckgo/teams/ci/edit. Uploaded with `s3cmd put --acl-public dax-ci-avatar.png 's3://ddg-chef/dax-ci-avatar.png`
icon_url='https://ddg-chef.s3.amazonaws.com/dax-ci-avatar.png'

# API docs at https://api.mattermost.com/#tag/posts/operation/CreatePost
MM_API='https://chat.duckduckgo.com/api/v4'
MM_AUTH="Authorization: Bearer ${MM_AUTH_TOKEN}"

echo "> Getting id of https://chat.duckduckgo.com/ddg/channels/${channel_name}..."
channel_id=$(curl -sS -H "$MM_AUTH" "${MM_API}/teams/${MM_TEAM_ID}/channels/name/${channel_name}" | jq -r '.id')
echo "> Found https://chat.duckduckgo.com/ddg/channels/${channel_id}"

if [ "${GITHUB_ACTIONS:-}" ]; then
now=$(date '+%H:%M %Z')
single_line_message=$(echo -n "${message}" | sed -z 's_\n_%0A_g')
echo "::notice title=${username} to ~${channel_name} at ${now}::${single_line_message}"
fi

json_message=$(echo -n "${message}" | jq -Rs .) # includes double-quotes around message
echo "> Sending ${json_message} with priority '${priority}'..."
res=$(curl -sS --fail -H "$MM_AUTH" -X POST "${MM_API}/posts" -d '{
"channel_id":"'"${channel_id}"'",
"message":'"${json_message}"',
"metadata": {
"priority": {
"priority": "'"${priority}"'",
"requested_ack": '"${request_ack}"'
}
},
"props":{
"override_username": "'"${username}"'",
"override_icon_url":"'"${icon_url}"'",
"from_webhook": "true"
}
}')

post_id=$(echo "$res" | jq -r '.id')
echo "✔ Sent https://chat.duckduckgo.com/ddg/pl/${post_id}"
56 changes: 41 additions & 15 deletions .github/workflows/release_report_error.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,50 @@ on:
workflow_dispatch:

env:
ASANA_PAT: ${{ secrets.GH_ASANA_SECRET }}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
MM_AUTH_TOKEN: ${{ secrets.MM_AUTH_TOKEN }}
MM_TEAM_ID: ${{ secrets.MM_TEAM_ID }}
APP_VERSION: "5.220.0"
emoji_start: ":flight_departure:" # 🛫 or ':soon:' 🔜
emoji_info: ":information_source:" # ℹ️

jobs:
report-release-error:
name: Create Asana Task
runs-on: ubuntu-20.04
name: Notify Mattermost
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./.github/actions/scripts

steps:
- name: Create Asana task when workflow failed
uses: honeycombio/gha-create-asana-task@main
- name: Checkout repository
uses: actions/checkout@v4
with:
asana-secret: ${{ secrets.GH_ASANA_SECRET }}
asana-workspace-id: ${{ secrets.GH_ASANA_WORKSPACE_ID }}
asana-project-id: ${{ secrets.GH_ASANA_AOR_PROJECT_ID }}
asana-section-id: ${{ secrets.GH_ASANA_INCOMING_ID }}
asana-task-name: GH Workflow Failure - Production Release
asana-task-description: The end to end workflow has failed. See https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}
submodules: recursive
token: ${{ secrets.GT_DAXMOBILE }}
fetch-depth: 0
ref: feature/david/11-14-gha_notify_mattermost
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ref parameter is hardcoded to a specific feature branch, which will prevent this workflow from running on other branches. Consider removing this line to allow the workflow to execute on the current branch where it's triggered.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.


- name: Make sure script can be executed
run: |
chmod +x send_chat_message.sh

- name: Send Release started message
run: |
./send_chat_message.sh '${{env.emoji_start}}'" Android Release ${{ env.APP_VERSION }} started by @${{ github.actor }}. https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}"

- name: Send Release task created message
run: |
./send_chat_message.sh '${{env.emoji_info}}'" Release task created https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}"

- name: Send Release tag created
run: |
./send_chat_message.sh '${{env.emoji_info}}'" Release task created https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}"

- name: Send Release task created message
run: |
echo -n '::notice title=Inputs::' && echo '${{ toJson(inputs) }}' | sed -z 's_\n_%0A_g'; echo
message="Release ${{ env.APP_VERSION }} task created"
message+="See https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}"
./send_chat_message.sh '${{env.emoji_info}}'" ${message}"
./send_chat_message.sh '${{env.emoji_info}}'" Release task created https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}"
Comment on lines +39 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There appear to be several duplicate message sends in this workflow. Specifically:

  1. The Send Release tag created step sends the exact same message as the previous step
  2. The final step sends the "Release task created" message twice

Consider consolidating these into single message sends to avoid duplicate notifications in the chat channel. This will keep the notifications clean and meaningful for the team.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

27 changes: 27 additions & 0 deletions .github/workflows/release_report_error_old.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Report Workflow Failed

on:
workflow_dispatch:

env:
ASANA_PAT: ${{ secrets.GH_ASANA_SECRET }}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
report-release-error:
name: Create Asana Task
runs-on: ubuntu-20.04

steps:
- name: Create Asana task when workflow failed
uses: honeycombio/gha-create-asana-task@main
with:
asana-secret: ${{ secrets.GH_ASANA_SECRET }}
asana-workspace-id: ${{ secrets.GH_ASANA_WORKSPACE_ID }}
asana-project-id: ${{ secrets.GH_ASANA_AOR_PROJECT_ID }}
asana-section-id: ${{ secrets.GH_ASANA_INCOMING_ID }}
asana-task-name: GH Workflow Failure - Production Release
asana-task-description: The end to end workflow has failed. See https://github.com/duckduckgo/Android/actions/runs/${{ github.run_id }}