summaryrefslogtreecommitdiffstats
path: root/gitsrht-update-hook
blob: 77c3f8797ba05bba7ca2a85bb040f0b0d956cbb3 (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
#!/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
from gitsrht.submit import do_post_update
import sys

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

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)