summaryrefslogtreecommitdiffstats
path: root/gitsrht-update-hook
blob: 8286342e122a47a39ed905fea32e1cf3c28eab79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
from srht.config import cfg
from srht.database import DbSession
db = DbSession(cfg("git.sr.ht", "connection-string"))
from gitsrht.types import Repository, RepoVisibility
db.init()
from configparser import ConfigParser
from datetime import datetime, timedelta
from gitsrht.submit import do_post_update
from scmsrht.redis import redis
import os
import sys

op = sys.argv[0]
origin = cfg("git.sr.ht", "origin")

if op == "hooks/update":
    # Stash updated refs for later processing
    refname = sys.argv[1]
    old = sys.argv[2]
    new = sys.argv[3]

    push_uuid = os.environ.get("SRHT_PUSH")
    if not push_uuid:
        sys.exit(0)
    redis.setex(f"update.{push_uuid}.{refname}",
            timedelta(minutes=10), f"{old}:{new}")

if op == "hooks/post-update":
    refs = sys.argv[1:]

    config = ConfigParser()
    with open("config") as f:
        config.read_file(f)

    repo_id = config.get("srht", "repo-id")
    if not repo_id:
        sys.exit(0)
    repo_id = int(repo_id)

    repo = Repository.query.get(repo_id)
    if not repo:
        sys.exit(0)

    if repo.visibility == RepoVisibility.autocreated:
        print("\n\t\033[93mNOTICE\033[0m")
        print("\tWe saved your changes, but this repository does not exist.")
        print("\tClick here to create it:")
        print("\t{}/create?name={}".format(origin, repo.name))
        print("\tYour changes will be discarded in 20 minutes.\n")

    repo.updated = datetime.utcnow()
    db.session.commit()

    do_post_update(repo, refs)