~edwargix/git.sr.ht

24b1fe334f9107f79635c0f0f3785083123efcc8 — Drew DeVault 8 years ago 172ebc4
Update manifests with appropriate ref
2 files changed, 23 insertions(+), 12 deletions(-)

M git-srht-update-hook
M gitsrht/worker.py
M git-srht-update-hook => git-srht-update-hook +12 -6
@@ 11,6 11,7 @@ from pygit2 import Repository as GitRepository
import shlex
import subprocess
import sys
import re

op = sys.argv[0]
builds_sr_ht = cfg("network", "builds", default=None)


@@ 27,7 28,7 @@ if op == "hooks/post-update":
        sys.exit(0)
    repo_id = int(repo_id)

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



@@ 37,9 38,14 @@ if op == "hooks/post-update":
    git_repo = GitRepository(repo.path)
    for ref in refs:
        try:
            target = git_repo.lookup_reference(ref).target
            if builds_sr_ht:
                from gitsrht.worker import do_post_update
                do_post_update(repo, git_repo, target)
            if re.match(r"^[0-9a-z]{40}$", ref): # commit
                ref = git_repo.get(ref)
            elif ref.startswith("refs/"): # ref
                ref = git_repo.lookup_reference(ref).target
            else:
                continue
        except:
            pass
            continue
        if builds_sr_ht:
            from gitsrht.worker import do_post_update
            do_post_update(repo, git_repo, ref)

M gitsrht/worker.py => gitsrht/worker.py +11 -6
@@ 7,12 7,14 @@ if not hasattr(db, "session"):
    import gitsrht.types
    db.init()

import requests
import html
from celery import Celery
from pygit2 import Commit
from srht.oauth import OAuthScope
#from buildsrht.manifest import Manifest
from buildsrht.manifest import Manifest
import requests
import html
import yaml
import os

worker = Celery('git', broker=cfg("git.sr.ht", "redis"))
builds_sr_ht = cfg("network", "builds")


@@ 48,8 50,11 @@ def do_post_update(repo, git_repo, ref):
        if manifest:
            manifest = git_repo.get(manifest.id)
            manifest = manifest.data.decode()
            # TODO: parse manifest and print errors here, and update the repo URL to match the ref
            #manifest = Manifest(manifest)
            manifest = Manifest(yaml.load(manifest))
            manifest.sources = [
                source if os.path.basename(source) != repo.name else source + "#" + str(ref)
                for source in manifest.sources
            ]
            token = repo.owner.oauth_token
            scopes = repo.owner.oauth_token_scopes
            scopes = [OAuthScope(s) for s in scopes.split(",")]


@@ 58,7 63,7 @@ def do_post_update(repo, git_repo, ref):
                print("Warning: log out and back in on the website to enable builds integration")
            else:
                do_webhook.delay(builds_sr_ht + "/api/jobs", {
                    "manifest": manifest,
                    "manifest": yaml.dump(manifest.to_dict(), default_flow_style=False),
                    # TODO: orgs
                    "tags": [repo.name],
                    "note": "{}\n\n[{}]({}) — [{}](mailto:{})".format(