Blob Blame Raw
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
 (c) 2018 - Copyright Red Hat Inc

 Authors:
   Patrick Uiterwijk <puiterwijk@redhat.com>

"""
from __future__ import print_function, absolute_import

import os
import sys

if os.environ.get("extra_internal_no_hooks", False):
    # we do this check before any pagure machinery is imported
    # and initialized to make sure this is very fast (used
    # when pushing code from original repos to forks)
    sys.exit(0)

# These fields get filled in by upload-repospanner-hooks
os.environ["PAGURE_CONFIG"] = "${config}"
PYPATH = "${pypath}"

# Prepare code imports
if PYPATH:
    sys.path.insert(0, PYPATH)

import pagure
import pagure.lib.model_base
import pagure.lib.query
from pagure.hooks import run_project_hooks, extract_changes
from pagure.config import config as pagure_config


# Get information from the environment
hooktype = os.path.basename(sys.argv[0])

is_internal = os.environ.get("extra_internal", False) == "yes"
pushuser = os.environ["extra_username"]
repotype = os.environ["extra_repotype"]
project_name = os.environ["extra_project_name"]
project_user = os.environ.get("extra_project_user", None) or None
project_namespace = os.environ.get("extra_project_namespace", None) or None
pruid = os.environ.get("extra_pull_request_uid", None)

changes = extract_changes(from_stdin=hooktype != "update")

session = pagure.lib.model_base.create_session(pagure_config["DB_URL"])
if not session:
    raise Exception("Unable to initialize db session")

gitdir = os.path.abspath(os.environ["GIT_DIR"])

project = pagure.lib.query._get_project(
    session, project_name, project_user, project_namespace
)
if not project:
    print("No project found")
    sys.exit(1)

pull_request = None
if pruid:
    pull_request = pagure.lib.query.get_request_by_uid(
        session, pruid
    )

run_project_hooks(
    session,
    pushuser,
    project,
    hooktype,
    repotype,
    gitdir,
    changes,
    is_internal,
    pull_request,
)