From 3ac4646627061166b68bc60fc3a6429fe7061fd5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 19 Apr 2019 13:59:25 -0400 Subject: Set up scm.sr.ht webhooks --- .../778f04602534_add_repo_webhook_table.py | 46 ++++++++++++++++++++++ gitsrht/app.py | 1 + gitsrht/webhooks.py | 11 ++++++ 3 files changed, 58 insertions(+) create mode 100644 gitsrht/alembic/versions/778f04602534_add_repo_webhook_table.py create mode 100644 gitsrht/webhooks.py (limited to 'gitsrht') diff --git a/gitsrht/alembic/versions/778f04602534_add_repo_webhook_table.py b/gitsrht/alembic/versions/778f04602534_add_repo_webhook_table.py new file mode 100644 index 0000000..2b2f650 --- /dev/null +++ b/gitsrht/alembic/versions/778f04602534_add_repo_webhook_table.py @@ -0,0 +1,46 @@ +"""Add repo webhook table + +Revision ID: 778f04602534 +Revises: 69b1f39fdca7 +Create Date: 2019-04-19 11:41:54.626104 + +""" + +# revision identifiers, used by Alembic. +revision = '778f04602534' +down_revision = '69b1f39fdca7' + +from alembic import op +import sqlalchemy as sa +import sqlalchemy_utils as sau + + +def upgrade(): + op.create_table('repo_webhook_subscription', + sa.Column("id", sa.Integer, primary_key=True), + sa.Column("created", sa.DateTime, nullable=False), + sa.Column("url", sa.Unicode(2048), nullable=False), + sa.Column("events", sa.Unicode, nullable=False), + sa.Column("user_id", sa.Integer, sa.ForeignKey("user.id")), + sa.Column("token_id", sa.Integer, sa.ForeignKey("oauthtoken.id")), + sa.Column("repo_id", sa.Integer, sa.ForeignKey("repository.id")), + ) + op.create_table('repo_webhook_delivery', + sa.Column("id", sa.Integer, primary_key=True), + sa.Column("uuid", sau.UUIDType, nullable=False), + sa.Column("created", sa.DateTime, nullable=False), + sa.Column("event", sa.Unicode(256), nullable=False), + sa.Column("url", sa.Unicode(2048), nullable=False), + sa.Column("payload", sa.Unicode(65536), nullable=False), + sa.Column("payload_headers", sa.Unicode(16384), nullable=False), + sa.Column("response", sa.Unicode(65536)), + sa.Column("response_status", sa.Integer, nullable=False), + sa.Column("response_headers", sa.Unicode(16384)), + sa.Column("subscription_id", sa.Integer, + sa.ForeignKey('repo_webhook_subscription.id'), nullable=False), + ) + + +def downgrade(): + op.drop_table('repo_webhook_delivery') + op.drop_table('repo_webhook_subscription') diff --git a/gitsrht/app.py b/gitsrht/app.py index 19d580e..d67e0db 100644 --- a/gitsrht/app.py +++ b/gitsrht/app.py @@ -11,6 +11,7 @@ from gitsrht.types import Access, Redirect, Repository, User from scmsrht.flask import ScmSrhtFlask from srht.config import cfg from srht.database import DbSession +import gitsrht.webhooks # makes valid the global db = DbSession(cfg("git.sr.ht", "connection-string")) db.init() diff --git a/gitsrht/webhooks.py b/gitsrht/webhooks.py new file mode 100644 index 0000000..e0fd0df --- /dev/null +++ b/gitsrht/webhooks.py @@ -0,0 +1,11 @@ +from srht.config import cfg +from srht.database import DbSession, db +if not hasattr(db, "session"): + # Initialize the database if not already configured (for running daemon) + db = DbSession(cfg("git.sr.ht", "connection-string")) + import gitsrht.types + db.init() +from srht.webhook.celery import make_worker +from scmsrht.webhooks import RepoWebhook + +worker = make_worker(broker=cfg("git.sr.ht", "webhooks")) -- cgit v1.2.3