Skip to content

Commit

Permalink
Removed prints for debugging. Added comments to code. Added `initiato…
Browse files Browse the repository at this point in the history
…r` description to README
  • Loading branch information
ddmtr-dtml committed Nov 3, 2022
1 parent bf561a9 commit 318e88c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,4 +26,6 @@ post-to-google-chat:
with:
gchatURL: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
json: ${{ toJSON(needs) }}
# Optional
initiator: example-user
```
25 changes: 8 additions & 17 deletions action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -32,19 +28,14 @@
else:
TEXT = 'Failed jobs: <br><b>' + '<br>'.join(map(str, failed_list)) + '</b>'

TEXT_DICT = ''
init_by = ''
# no_initiator = {"cards": [{"header": {"title": "<b>JOBS FAILED</b>", "subtitle": repo_name+":"+head}, "sections": [{"widgets": [{"textParagraph": {"text": TEXT}}, {"buttons": [{"textButton": {"text": "VIEW RUN", "onClick": {"openLink": {"url": github_link}}}}]}]}]}]}
# is_initiator = {"cards": [{"header": {"title": "<b>JOBS FAILED</b>", "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: <b>' + initiator + '<b>'
TEXT_DICT = {"cards": [{"header": {"title": "<b>JOBS FAILED</b>", "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: <b>' + initiator + '<b>'
TEXT_DICT = {"cards": [{"header": {"title": "<b>JOBS FAILED</b>", "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": "<b>JOBS FAILED</b>", "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": "<b>JOBS FAILED</b>", "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',
Expand Down

0 comments on commit 318e88c

Please sign in to comment.