diff --git a/README.md b/README.md index 6b2e10a..201898e 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,23 @@ # google-chat-job-failed + This sends messages to google chat when a workflow job fails. ## Usage + Pass the jobs that you need to track to `needs:`. -The action takes 2 mandatory variables: gchatURL and json. -Leave the json variable as is in the example. This is still a WIP. -Add your Google Chat webhook URL to the repo's secrets and send it to the gchatURL variable. +The action takes 2 mandatory variables: gchatURL and json. +Leave the json variable as is in the example. This is still a WIP. +Add your Google Chat webhook URL to the repo's secrets and send it to the gchatURL variable. + +Another optional variable `initiator` can be passed in to print out who should be responsible for this run. This is espcially useful when the workflow was started by another workflow through a bot. + +You can add more conditions to the `if:` after `always()`. `always()` is necessary for the job to run even if the workflow fails. ```YAML post-to-google-chat: needs: [ job1, job2, job3 ] - if: always() + if: always() runs-on: ubuntu-latest steps: - name: google-chat-job-failed @@ -20,4 +26,6 @@ post-to-google-chat: with: gchatURL: ${{ secrets.GOOGLE_CHAT_WEBHOOK }} json: ${{ toJSON(needs) }} + # Optional + initiator: example-user ``` diff --git a/action.py b/action.py index 460dc58..315e4c7 100644 --- a/action.py +++ b/action.py @@ -2,24 +2,20 @@ import urllib3 import json + +# Action input variables needs_context = json.loads(os.getenv('NEEDS_CONTEXT')) google_chat_webhook = os.getenv('GOOGLE_CHAT_WEBHOOK') initiator = os.getenv('INITIATOR') +# Other environment variables github_link = os.getenv('GITHUB_LINK') head = os.getenv('HEAD') repo_name = os.getenv('REPO_NAME') -print(needs_context) -print(google_chat_webhook) -print(github_link) -print(head) -print(repo_name) - # List to store failed jobs failed_list = [] # Add failed jobs to list if job's result is failure -print(type(needs_context)) for job_name in needs_context: job = needs_context.get(job_name) if job.get('result') == 'failure': @@ -32,19 +28,14 @@ else: TEXT = 'Failed jobs:
' + '
'.join(map(str, failed_list)) + '
' -TEXT_DICT = '' -init_by = '' -# no_initiator = {"cards": [{"header": {"title": "JOBS FAILED", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]} -# is_initiator = {"cards": [{"header": {"title": "JOBS FAILED", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": init_by}}, {"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]} - +# Choose JSON dict if initiator has been set or not. if initiator: - init_by = 'Initiated by: ' + initiator + '' - TEXT_DICT = {"cards": [{"header": {"title": "JOBS FAILED", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": init_by}}, {"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]} + INIT_BY = 'Initiated by: ' + initiator + '' + TEXT_DICT = {"cards": [{"header": {"title": "JOBS FAILED", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": INIT_BY}}, {"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]} else: - TEXT_DICT = no_initiator = {"cards": [{"header": {"title": "JOBS FAILED", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]} - -print(json.dumps(TEXT_DICT)) + TEXT_DICT = {"cards": [{"header": {"title": "JOBS FAILED", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]} +# Make POST request to webhook URL http = urllib3.PoolManager() r = http.request( 'POST',