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

Store payload in Webhook History & make webhook ID clickable to show corresponding payload #3467

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
add new column to store Webhook payload

Revision ID: 046fb5ed2cf3
Create Date: 2024-10-07 05:53:50.891339
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '046fb5ed2cf3'
down_revision = 'bb52d9f878f5'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('webhook_history', sa.Column('payload', sa.Text()))

Check warning

Code scanning / vcs-diff-lint

Trailing whitespace Warning

Trailing whitespace


def downgrade():
op.drop_column('webhook_history', 'payload')
1 change: 1 addition & 0 deletions frontend/coprs_frontend/coprs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,7 @@ class WebhookHistory(db.Model):
# Null values are possible via custom webhook implementation that do not pass a UUID or User Agent.
user_agent = db.Column(db.Text,nullable=True)
webhook_uuid = db.Column(db.Text, nullable=True)
payload = db.Column (db.Text, nullable=True)


class DistGitBranch(db.Model, helpers.Serializer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@
{% for webhook in webhook_history %}
<tr>
<td class="webhook_timestamp" data-toggle="tooltip" title="{{webhook.created_on|localized_time(g.user.timezone)}}">{{webhook.created_on|localized_time(g.user.timezone)}} ({{webhook.created_on|time_ago()}})</td>
<td>{{webhook.webhook_uuid}} </td>
<td data-toggle="tooltip" title="Show JSON payload">
{% if webhook.payload is not none %}
<a href="#" onclick="showJson(this);" data-json="{{webhook.payload}}">
{{webhook.webhook_uuid}}
</a>
{% else %}
{{webhook.webhook_uuid}}
{% endif %}
</td>
<td>{{webhook.user_agent}}</td>
</tr>
{% endfor %}
Expand All @@ -95,4 +103,14 @@
</div>
</div>

<script>
function showJson(element) {
const jsonData = element.getAttribute('data-json');
if(jsonData === "null") return;
const newTab = window.open();
newTab.document.write('<pre>' + JSON.stringify(JSON.parse(jsonData), null, 2) + '</pre>');

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.
newTab.document.close();
}
</script>

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import time
import shutil
import json
from typing import Optional

import flask
Expand Down Expand Up @@ -83,7 +84,7 @@ def decorated_function(copr, **kwargs):


def add_webhook_history_record(webhook_uuid, user_agent='Not Set',
builds_initiated_via_hook=None):
builds_initiated_via_hook=None, payload=None):
"""
This methods adds info of an intercepted webhook to webhook_history db
along with the initiated build number(s).
Expand All @@ -94,7 +95,7 @@ def add_webhook_history_record(webhook_uuid, user_agent='Not Set',

webhookRecord = models.WebhookHistory(created_on=int(time.time()),
webhook_uuid=webhook_uuid,
user_agent=user_agent)
user_agent=user_agent, payload=payload)
db.session.add(webhookRecord)
db.session.commit()

Expand Down Expand Up @@ -198,7 +199,7 @@ def webhooks_git_push(copr_id: int, uuid, pkg_name: Optional[str] = None):
builds_initiated_via_webhook.append(build)

add_webhook_history_record(webhook_uuid, user_agent,
builds_initiated_via_webhook)
builds_initiated_via_webhook, json.dumps(payload))
db.session.commit()

return "OK", 200
Expand Down