summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-04-19 13:59:25 -0400
committerDrew DeVault <sir@cmpwn.com>2019-04-19 13:59:25 -0400
commit3ac4646627061166b68bc60fc3a6429fe7061fd5 (patch)
tree150c6309225d48be73a48d8733064f8e0d3659bc
parentd09df42802e2501d187d835c7e032cd12942a5cb (diff)
Set up scm.sr.ht webhooks
-rw-r--r--config.example.ini4
-rwxr-xr-xgitsrht-migrate1
-rw-r--r--gitsrht/alembic/versions/778f04602534_add_repo_webhook_table.py46
-rw-r--r--gitsrht/app.py1
-rw-r--r--gitsrht/webhooks.py11
5 files changed, 60 insertions, 3 deletions
diff --git a/config.example.ini b/config.example.ini
index 15146da..cb586b4 100644
--- a/config.example.ini
+++ b/config.example.ini
@@ -64,8 +64,8 @@ connection-string=postgresql://postgres@localhost/git.sr.ht
# Set to "yes" to automatically run migrations on package upgrade.
migrate-on-upgrade=yes
#
-# The redis connection used for the Celery worker
-redis=redis://localhost:6379/1
+# The redis connection used for the webhooks worker
+webhooks=redis://localhost:6379/1
#
# A post-update script which is installed in every git repo.
post-update-script=/usr/bin/gitsrht-update-hook
diff --git a/gitsrht-migrate b/gitsrht-migrate
index 0a92480..bda6b54 100755
--- a/gitsrht-migrate
+++ b/gitsrht-migrate
@@ -3,4 +3,3 @@ import gitsrht.alembic
import srht.alembic
from srht.database import alembic
alembic("git.sr.ht", gitsrht.alembic)
-alembic("git.sr.ht", srht.alembic)
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"))