Skip to content

Commit

Permalink
better webhook structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lfkdev committed Jun 26, 2024
1 parent 4cee5c1 commit 48f67ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ main ]

jobs:
test:
ansible-link-ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -28,11 +28,16 @@ jobs:
touch test_inventory.ini
- name: Create test playbook
run: |
echo "- hosts: localhost" > test_playbooks/test_playbook.yml
echo " tasks:" >> test_playbooks/test_playbook.yml
echo " - name: Output OK" >> test_playbooks/test_playbook.yml
echo " debug:" >> test_playbooks/test_playbook.yml
echo " msg: PLAYBOOK RUN OK" >> test_playbooks/test_playbook.yml
cat << EOF > test_playbooks/test_playbook.yml
---
- name: Test Playbook
hosts: localhost
connection: local
tasks:
- name: Print a message
debug:
msg: "This is a test playbook"
EOF
- name: Set PYTHONPATH and ANSIBLE_API_CONFIG
run: |
echo "PYTHONPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV
Expand All @@ -42,6 +47,8 @@ jobs:
ls -R
echo "PYTHONPATH: $PYTHONPATH"
echo "ANSIBLE_API_CONFIG: $ANSIBLE_API_CONFIG"
pip list
cat test_playbooks/test_playbook.yml
- name: Update import in ansible_link.py
run: |
sed -i 's/from webhook import WebhookSender/from ansible_link.webhook import WebhookSender/' ansible_link/ansible_link.py
Expand Down
33 changes: 24 additions & 9 deletions ansible_link/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,43 @@ def format_payload(self, event_type, job_data):
"timestamp": datetime.now().isoformat()
}

if 'error' in job_data:
base_payload['error'] = job_data['error']

if self.webhook_type == 'slack':
color = "#36a64f" if job_data['status'] in ['completed', 'started'] else "#ff0000"
fields = [
{"title": "Status", "value": job_data['status'], "short": True},
{"title": "Job ID", "value": job_data['job_id'], "short": True}
]
if 'error' in job_data:
fields.append({"title": "Error", "value": job_data['error'], "short": False})

return {
"attachments": [{
"color": color,
"title": f"Ansible {event_type.replace('_', ' ').title()}",
"text": f"Playbook: {job_data['playbook']}\nStatus: {job_data['status']}",
"footer": f"Ansible-Link | {job_data['job_id']}",
"title": job_data['playbook'],
"text": f"Event: {event_type.replace('_', ' ').title()}",
"fields": fields,
"footer": "Ansible-Link",
"ts": int(datetime.now().timestamp())
}]
}
elif self.webhook_type == 'discord':
color = 0x36a64f if job_data['status'] in ['completed', 'started'] else 0xff0000
fields = [
{"name": "Status", "value": job_data['status'], "inline": True},
{"name": "Job ID", "value": job_data['job_id'], "inline": True},
{"name": "Event", "value": event_type.replace('_', ' ').title(), "inline": False}
]
if 'error' in job_data:
fields.append({"name": "Error", "value": job_data['error'], "inline": False})

return {
"embeds": [{
"title": f"Ansible {event_type.replace('_', ' ').title()}",
"title": job_data['playbook'],
"color": color,
"fields": [
{"name": "Playbook", "value": job_data['playbook'], "inline": True},
{"name": "Status", "value": job_data['status'], "inline": True},
{"name": "Job ID", "value": job_data['job_id'], "inline": False}
],
"fields": fields,
"footer": {"text": "Ansible-Link"},
"timestamp": datetime.now().isoformat()
}]
Expand Down

0 comments on commit 48f67ea

Please sign in to comment.