From d6986a0d93c85a61cb3796057bcc36edeb7d8721 Mon Sep 17 00:00:00 2001 From: Arthur Meyre Date: Tue, 21 Sep 2021 17:13:30 +0200 Subject: [PATCH] chore: make sure global notification color makes sense --- .github/workflows/continuous-integration.yaml | 31 ++++++++++---- .../actions_utils/actions_combine_status.py | 42 +++++++++++++++++++ 2 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 script/actions_utils/actions_combine_status.py diff --git a/.github/workflows/continuous-integration.yaml b/.github/workflows/continuous-integration.yaml index c4336cb29..1e49e1019 100644 --- a/.github/workflows/continuous-integration.yaml +++ b/.github/workflows/continuous-integration.yaml @@ -52,7 +52,7 @@ jobs: with: format: 'space-delimited' - name: Should rebuild docker check - run : | + run: | set +e echo "${{ steps.files.outputs.all }}" | grep "${ENV_DOCKERFILE}" DOCKERFILE_CHANGED=$? @@ -434,17 +434,30 @@ jobs: send-report: if: ${{ always() }} - needs: [ - build-preflight-docker, - build, - publish-docs, - push-docker-image, - package-release, - ] + needs: + [ + build-preflight-docker, + build, + publish-docs, + push-docker-image, + package-release, + ] name: Send Slack notification runs-on: ubuntu-20.04 steps: + - uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f + - name: Prepare whole job status + if: ${{ always() }} + continue-on-error: true + env: + NEEDS_JSON: ${{ toJSON(needs) }} + run: | + echo "${NEEDS_JSON}" > /tmp/needs_context.json + JOB_STATUS=$(python3 ./script/actions_utils/actions_combine_status.py \ + --needs_context_json /tmp/needs_context.json) + echo "JOB_STATUS=${JOB_STATUS}" >> "$GITHUB_ENV" + - name: Slack Notification if: ${{ always() }} continue-on-error: true @@ -452,7 +465,7 @@ jobs: env: SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }} SLACK_ICON: https://pbs.twimg.com/profile_images/1274014582265298945/OjBKP9kn_400x400.png - SLACK_COLOR: ${{ job.status }} + SLACK_COLOR: ${{ env.JOB_STATUS || 'failure' }} SLACK_MESSAGE: "Full run here: ${{ env.ACTION_RUN_URL }}\n\ - build-preflight-docker: ${{ needs.build-preflight-docker.outputs.report || 'Did not run.' }}\n\n\ - build: ${{ needs.build.outputs.report || 'Did not run.' }}\n\n\ diff --git a/script/actions_utils/actions_combine_status.py b/script/actions_utils/actions_combine_status.py new file mode 100644 index 000000000..abcb41c95 --- /dev/null +++ b/script/actions_utils/actions_combine_status.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +"""Helper script for github actions to combine job statuses""" +import argparse +import json + +RESULTS_TO_DISPLAY_LEVEL = { + "failure": 0, + "cancelled": 1, + "success": 2, + "skipped": 3, +} + +DISPLAY_LEVEL_TO_RESULTS = {val: key for key, val in RESULTS_TO_DISPLAY_LEVEL.items()} + + +def main(args): + """Entry point""" + + need_context_data = None + with open(args.needs_context_json, encoding="utf-8") as f: + need_context_data = json.load(f) + + display_level = min( + RESULTS_TO_DISPLAY_LEVEL[job_object["result"]] for job_object in need_context_data.values() + ) + + print(DISPLAY_LEVEL_TO_RESULTS[display_level]) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser("Combine github actions statuses", allow_abbrev=False) + + parser.add_argument( + "--needs_context_json", + type=str, + help="Pass the json file path containing the workflow needs context", + ) + + cli_args = parser.parse_args() + + main(cli_args)