Skip to content

Commit

Permalink
frontend: store payload & make webhook UUID clickable to show payload
Browse files Browse the repository at this point in the history
  • Loading branch information
jaitjacob committed Oct 8, 2024
1 parent 916f82f commit f77e9df
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
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 @@ <h3>Webhook History</h3>
{% 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 @@ <h3>Webhook History</h3>
</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

0 comments on commit f77e9df

Please sign in to comment.