Set up scm.sr.ht webhooks

This commit is contained in:
Drew DeVault 2019-04-19 13:59:25 -04:00
parent d09df42802
commit 3ac4646627
5 changed files with 60 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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')

View File

@ -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()

11
gitsrht/webhooks.py Normal file
View File

@ -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"))