#3672 Port the plugins to the new runner architecture
Merged 5 years ago by pingou. Opened 5 years ago by pingou.

file modified
+3 -2
@@ -73,12 +73,13 @@ 

              username (string): The user performing a push

              project (model.Project): The project this call is made for

              repotype (string): Value of lib.REPOTYPES indicating for which

-                 repo the currnet call is

+                 repo the current call is

              repodir (string): Directory where a clone of the specified repo is

                  located. Do note that this might or might not be a writable

                  clone.

              changes (dict): A dict with keys being the ref to update, values

                  being a tuple of (from, to).

+                 For example: {'refs/heads/master': (hash_from, hash_to), ...}

          """

          if hooktype == "pre-receive":

              cls.pre_receive(
@@ -327,7 +328,7 @@ 

              sys.exit(1)

  

      # Now we run the hooks for plugins

-     for plugin, _ in get_enabled_plugins(project):

+     for plugin, _ in get_enabled_plugins(project, with_default=True):

          if not plugin.runner:

              if debug:

                  print(

file modified
+263 -27
@@ -8,9 +8,13 @@ 

  

  """

  

- from __future__ import unicode_literals

+ from __future__ import unicode_literals, print_function

  

+ import logging

+ 

+ import pygit2

  import sqlalchemy as sa

+ import six

  import wtforms

  

  try:
@@ -20,9 +24,17 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.hooks import BaseHook

+ import pagure.config

+ import pagure.exceptions

+ import pagure.lib.tasks

+ import pagure.lib.tasks_services

+ import pagure.utils

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

- from pagure.utils import get_repo_path

+ 

+ 

+ _config = pagure.config.reload_config()

+ _log = logging.getLogger(__name__)

  

  

  class DefaultTable(BASE):
@@ -55,6 +67,253 @@ 

      )

  

  

+ def send_fedmsg_notifications(project, topic, msg):

+     """ If the user asked for fedmsg notifications on commit, this will

+     do it.

+     """

+     import fedmsg

+ 

+     config = fedmsg.config.load_config([], None)

+     config["active"] = True

+     config["endpoints"]["relay_inbound"] = config["relay_inbound"]

+     fedmsg.init(name="relay_inbound", **config)

+ 

+     pagure.lib.notify.log(

+         project=project,

+         topic=topic,

+         msg=msg,

+         redis=None,  # web-hook notification are handled separately

+     )

+ 

+ 

+ def send_webhook_notifications(project, topic, msg):

+     """ If the user asked for webhook notifications on commit, this will

+     do it.

+     """

+ 

+     pagure.lib.tasks_services.webhook_notification.delay(

+         topic=topic,

+         msg=msg,

+         namespace=project.namespace,

+         name=project.name,

+         user=project.user.username if project.is_fork else None,

+     )

+ 

+ 

+ def send_notifications(session, project, repodir, user, refname, revs, forced):

+     """ Send out-going notifications about the commits that have just been

+     pushed.

+     """

+ 

+     auths = set()

+     for rev in revs:

+         email = pagure.lib.git.get_author_email(rev, repodir)

+         name = pagure.lib.git.get_author(rev, repodir)

+         author = pagure.lib.search_user(session, email=email) or name

+         auths.add(author)

+ 

+     authors = []

+     for author in auths:

+         if not isinstance(author, six.string_types):

+             author = author.to_json(public=True)

+         authors.append(author)

+ 

+     if revs:

+         revs.reverse()

+         print("* Publishing information for %i commits" % len(revs))

+ 

+         topic = "git.receive"

+         msg = dict(

+             total_commits=len(revs),

+             start_commit=revs[0],

+             end_commit=revs[-1],

+             branch=refname,

+             forced=forced,

+             authors=list(authors),

+             agent=user,

+             repo=project.to_json(public=True)

+             if not isinstance(project, six.string_types)

+             else project,

+         )

+ 

+         fedmsg_hook = pagure.lib.plugins.get_plugin("Fedmsg")

+         fedmsg_hook.db_object()

+ 

+         always_fedmsg = _config.get("ALWAYS_FEDMSG_ON_COMMITS") or None

+ 

+         if always_fedmsg or (

+             project.fedmsg_hook and project.fedmsg_hook.active

+         ):

+             try:

+                 print("  - to fedmsg")

+                 send_fedmsg_notifications(project, topic, msg)

+             except Exception:

+                 _log.exception(

+                     "Error sending fedmsg notifications on commit push"

+                 )

+         if project.settings.get("Web-hooks") and not project.private:

+             try:

+                 print("  - to web-hooks")

+                 send_webhook_notifications(project, topic, msg)

+             except Exception:

+                 _log.exception(

+                     "Error sending web-hook notifications on commit push"

+                 )

+ 

+         if (

+             _config.get("PAGURE_CI_SERVICES")

+             and project.ci_hook

+             and project.ci_hook.active_commit

+             and not project.private

+         ):

+             pagure.lib.tasks_services.trigger_ci_build.delay(

+                 project_name=project.fullname,

+                 cause=revs[-1],

+                 branch=refname,

+                 ci_type=project.ci_hook.ci_type,

+             )

+ 

+ 

+ def inform_pull_request_urls(

+     session, project, commits, refname, default_branch

+ ):

+     """ Inform the user about the URLs to open a new pull-request or visit

+     the existing one.

+     """

+     target_repo = project

+     if project.is_fork:

+         target_repo = project.parent

+ 

+     if (

+         commits

+         and refname != default_branch

+         and target_repo.settings.get("pull_requests", True)

+     ):

+         print()

+         prs = pagure.lib.search_pull_requests(

+             session,

+             project_id_from=project.id,

+             status="Open",

+             branch_from=refname,

+         )

+         # Link to existing PRs if there are any

+         seen = len(prs) != 0

+         for pr in prs:

+             # Link tickets with pull-requests if the commit mentions it

+             pagure.lib.tasks.link_pr_to_ticket.delay(pr.uid)

+ 

+             # Inform the user about the PR

+             print("View pull-request for %s" % refname)

+             print(

+                 "   %s/%s/pull-request/%s"

+                 % (_config["APP_URL"].rstrip("/"), pr.project.url_path, pr.id)

+             )

+         # If no existing PRs, provide the link to open one

+         if not seen:

+             print("Create a pull-request for %s" % refname)

+             print(

+                 "   %s/%s/diff/%s..%s"

+                 % (

+                     _config["APP_URL"].rstrip("/"),

+                     project.url_path,

+                     default_branch,

+                     refname,

+                 )

+             )

+         print()

+ 

+ 

+ class DefaultRunner(BaseRunner):

+     """ Runner for the default hook."""

+ 

+     @staticmethod

+     def post_receive(session, username, project, repotype, repodir, changes):

+         """ Run the default post-receive hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+         if repotype != "main":

+             if _config.get("HOOK_DEBUG", False):

+                 print("Default hook only runs on the main project repository")

+                 return

+ 

+         if changes:

+             # Retrieve the default branch

+             repo_obj = pygit2.Repository(repodir)

+             default_branch = None

+             if not repo_obj.is_empty and not repo_obj.head_is_unborn:

+                 default_branch = repo_obj.head.shorthand

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             forced = False

+             if set(newrev) == set(["0"]):

+                 print(

+                     "Deleting a reference/branch, so we won't run the "

+                     "pagure hook"

+                 )

+                 return

+             elif set(oldrev) == set(["0"]):

+                 oldrev = "^%s" % oldrev

+             elif pagure.lib.git.is_forced_push(oldrev, newrev, repodir):

+                 forced = True

+                 base = pagure.lib.git.get_base_revision(

+                     oldrev, newrev, repodir

+                 )

+                 if base:

+                     oldrev = base[0]

+ 

+             refname = refname.replace("refs/heads/", "")

+             commits = pagure.lib.git.get_revs_between(

+                 oldrev, newrev, repodir, refname

+             )

+ 

+             if refname == default_branch:

+                 print(

+                     "Sending to redis to log activity and send commit "

+                     "notification emails"

+                 )

+             else:

+                 print("Sending to redis to send commit notification emails")

+ 

+             # This is logging the commit to the log table in the DB so we can

+             # render commits in the calendar heatmap.

+             # It is also sending emails about commits to people using the

+             # 'watch' feature to be made aware of new commits.

+             pagure.lib.tasks_services.log_commit_send_notifications.delay(

+                 name=project.name,

+                 commits=commits,

+                 abspath=repodir,

+                 branch=refname,

+                 default_branch=default_branch,

+                 namespace=project.namespace,

+                 username=project.user.user if project.is_fork else None,

+             )

+ 

+             # This one is sending fedmsg and web-hook notifications for project

+             # that set them up

+             send_notifications(

+                 session, project, repodir, username, refname, commits, forced

+             )

+ 

+             # Now display to the user if this isn't the default branch links to

+             # open a new pr or review the existing one

+             inform_pull_request_urls(

+                 session, project, commits, refname, default_branch

+             )

+ 

+         # Schedule refresh of all opened PRs

+         parent = project.parent or project

+         pagure.lib.tasks.refresh_pr_cache.delay(

+             parent.name,

+             parent.namespace,

+             parent.user.user if parent.is_fork else None,

+         )

+ 

+         session.remove()

+ 

+ 

  class DefaultForm(FlaskForm):

      """ Form to configure the default hook. """

  
@@ -80,27 +339,4 @@ 

      db_object = DefaultTable

      backref = "default_hook"

      form_fields = ["active"]

- 

-     @classmethod

-     def install(cls, project, dbobj):

-         """ Method called to install the hook for a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

- 

-         cls.base_install(repopaths, dbobj, "default", "default_hook.py")

- 

-     @classmethod

-     def remove(cls, project):

-         """ Method called to remove the hook of a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

- 

-         cls.base_remove(repopaths, "default")

+     runner = DefaultRunner

file modified
+10 -1
@@ -20,7 +20,7 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.hooks import BaseHook

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

  

  
@@ -55,6 +55,14 @@ 

      )

  

  

+ class FedmsgRunner(BaseRunner):

+     """ Runner for the fedmsg hook, it does nothing as all the magic is

+     part of the default hook/runner.

+     """

+ 

+     pass

+ 

+ 

  class FedmsgForm(FlaskForm):

      """ Form to configure the fedmsg hook. """

  
@@ -82,6 +90,7 @@ 

      db_object = FedmsgTable

      backref = "fedmsg_hook"

      form_fields = ["active"]

+     runner = FedmsgRunner

  

      @classmethod

      def install(cls, project, dbobj):

@@ -1,294 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to be added to all projects in pagure by default.

- """

- from __future__ import print_function, unicode_literals

- 

- import os

- import logging

- import sys

- 

- 

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- 

- import pygit2  # noqa: E402

- import six  # noqa: E402

- 

- import pagure  # noqa: E402

- import pagure.flask_app  # noqa: E402

- import pagure.exceptions  # noqa: E402

- import pagure.lib.link  # noqa: E402

- import pagure.lib.tasks  # noqa: E402

- import pagure.lib.tasks_services  # noqa: E402

- 

- 

- _config = pagure.config.reload_config()

- _log = logging.getLogger(__name__)

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

- 

- 

- def send_fedmsg_notifications(project, topic, msg):

-     """ If the user asked for fedmsg notifications on commit, this will

-     do it.

-     """

-     import fedmsg

- 

-     config = fedmsg.config.load_config([], None)

-     config["active"] = True

-     config["endpoints"]["relay_inbound"] = config["relay_inbound"]

-     fedmsg.init(name="relay_inbound", **config)

- 

-     pagure.lib.notify.log(

-         project=project,

-         topic=topic,

-         msg=msg,

-         redis=None,  # web-hook notification are handled separately

-     )

- 

- 

- def send_webhook_notifications(project, topic, msg):

-     """ If the user asked for webhook notifications on commit, this will

-     do it.

-     """

- 

-     pagure.lib.tasks_services.webhook_notification.delay(

-         topic=topic,

-         msg=msg,

-         namespace=project.namespace,

-         name=project.name,

-         user=project.user.username if project.is_fork else None,

-     )

- 

- 

- def send_notifications(session, project, refname, revs, forced):

-     """ Send out-going notifications about the commits that have just been

-     pushed.

-     """

- 

-     auths = set()

-     for rev in revs:

-         email = pagure.lib.git.get_author_email(rev, abspath)

-         name = pagure.lib.git.get_author(rev, abspath)

-         author = pagure.lib.search_user(session, email=email) or name

-         auths.add(author)

- 

-     authors = []

-     for author in auths:

-         if not isinstance(author, six.string_types):

-             author = author.to_json(public=True)

-         authors.append(author)

- 

-     if revs:

-         revs.reverse()

-         print("* Publishing information for %i commits" % len(revs))

- 

-         topic = "git.receive"

-         msg = dict(

-             total_commits=len(revs),

-             start_commit=revs[0],

-             end_commit=revs[-1],

-             branch=refname,

-             forced=forced,

-             authors=list(authors),

-             agent=os.environ["GL_USER"],

-             repo=project.to_json(public=True)

-             if not isinstance(project, six.string_types)

-             else project,

-         )

- 

-         fedmsg_hook = pagure.lib.plugins.get_plugin("Fedmsg")

-         fedmsg_hook.db_object()

- 

-         always_fedmsg = _config.get("ALWAYS_FEDMSG_ON_COMMITS") or None

- 

-         if always_fedmsg or (

-             project.fedmsg_hook and project.fedmsg_hook.active

-         ):

-             try:

-                 print("  - to fedmsg")

-                 send_fedmsg_notifications(project, topic, msg)

-             except Exception:

-                 _log.exception(

-                     "Error sending fedmsg notifications on commit push"

-                 )

-         if project.settings.get("Web-hooks") and not project.private:

-             try:

-                 print("  - to web-hooks")

-                 send_webhook_notifications(project, topic, msg)

-             except Exception:

-                 _log.exception(

-                     "Error sending web-hook notifications on commit push"

-                 )

- 

-         if (

-             _config.get("PAGURE_CI_SERVICES")

-             and project.ci_hook

-             and project.ci_hook.active_commit

-             and not project.private

-         ):

-             pagure.lib.tasks_services.trigger_ci_build.delay(

-                 project_name=project.fullname,

-                 cause=revs[-1],

-                 branch=refname,

-                 ci_type=project.ci_hook.ci_type,

-             )

- 

- 

- def inform_pull_request_urls(

-     session, project, commits, refname, default_branch

- ):

-     """ Inform the user about the URLs to open a new pull-request or visit

-     the existing one.

-     """

-     target_repo = project

-     if project.is_fork:

-         target_repo = project.parent

- 

-     if (

-         commits

-         and refname != default_branch

-         and target_repo.settings.get("pull_requests", True)

-     ):

-         print()

-         prs = pagure.lib.search_pull_requests(

-             session,

-             project_id_from=project.id,

-             status="Open",

-             branch_from=refname,

-         )

-         # Link to existing PRs if there are any

-         seen = len(prs) != 0

-         for pr in prs:

-             # Link tickets with pull-requests if the commit mentions it

-             pagure.lib.tasks.link_pr_to_ticket.delay(pr.uid)

- 

-             # Inform the user about the PR

-             print("View pull-request for %s" % refname)

-             print(

-                 "   %s/%s/pull-request/%s"

-                 % (_config["APP_URL"].rstrip("/"), pr.project.url_path, pr.id)

-             )

-         # If no existing PRs, provide the link to open one

-         if not seen:

-             print("Create a pull-request for %s" % refname)

-             print(

-                 "   %s/%s/diff/%s..%s"

-                 % (

-                     _config["APP_URL"].rstrip("/"),

-                     project.url_path,

-                     default_branch,

-                     refname,

-                 )

-             )

-         print()

- 

- 

- def run_as_post_receive_hook():

- 

-     repo = pagure.lib.git.get_repo_name(abspath)

-     username = pagure.lib.git.get_username(abspath)

-     namespace = pagure.lib.git.get_repo_namespace(abspath)

-     if _config.get("HOOK_DEBUG", False):

-         print("repo:", repo)

-         print("user:", username)

-         print("namespace:", namespace)

- 

-     session = pagure.lib.create_session(_config["DB_URL"])

- 

-     project = pagure.lib._get_project(

-         session, repo, user=username, namespace=namespace

-     )

- 

-     for line in sys.stdin:

-         if _config.get("HOOK_DEBUG", False):

-             print(line)

-         (oldrev, newrev, refname) = line.strip().split(" ", 2)

- 

-         if _config.get("HOOK_DEBUG", False):

-             print("  -- Old rev")

-             print(oldrev)

-             print("  -- New rev")

-             print(newrev)

-             print("  -- Ref name")

-             print(refname)

- 

-         # Retrieve the default branch

-         repo_obj = pygit2.Repository(abspath)

-         default_branch = None

-         if not repo_obj.is_empty and not repo_obj.head_is_unborn:

-             default_branch = repo_obj.head.shorthand

- 

-         forced = False

-         if set(newrev) == set(["0"]):

-             print(

-                 "Deleting a reference/branch, so we won't run the "

-                 "pagure hook"

-             )

-             return

-         elif set(oldrev) == set(["0"]):

-             oldrev = "^%s" % oldrev

-         elif pagure.lib.git.is_forced_push(oldrev, newrev, abspath):

-             forced = True

-             base = pagure.lib.git.get_base_revision(oldrev, newrev, abspath)

-             if base:

-                 oldrev = base[0]

- 

-         refname = refname.replace("refs/heads/", "")

-         commits = pagure.lib.git.get_revs_between(

-             oldrev, newrev, abspath, refname

-         )

- 

-         if refname == default_branch:

-             print(

-                 "Sending to redis to log activity and send commit "

-                 "notification emails"

-             )

-         else:

-             print("Sending to redis to send commit notification emails")

- 

-         # This is logging the commit to the log table in the DB so we can

-         # render commits in the calendar heatmap.

-         # It is also sending emails about commits to people using the

-         # 'watch' feature to be made aware of new commits.

-         pagure.lib.tasks_services.log_commit_send_notifications.delay(

-             name=repo,

-             commits=commits,

-             abspath=abspath,

-             branch=refname,

-             default_branch=default_branch,

-             namespace=namespace,

-             username=username,

-         )

- 

-         # This one is sending fedmsg and web-hook notifications for project

-         # that set them up

-         send_notifications(session, project, refname, commits, forced)

- 

-         # Now display to the user if this isn't the default branch links to

-         # open a new pr or review the existing one

-         inform_pull_request_urls(

-             session, project, commits, refname, default_branch

-         )

- 

-     # Schedule refresh of all opened PRs

-     parent = project.parent or project

-     pagure.lib.tasks.refresh_pr_cache.delay(

-         parent.name,

-         parent.namespace,

-         parent.user.user if parent.is_fork else None,

-     )

- 

-     session.remove()

- 

- 

- def main(args):

-     run_as_post_receive_hook()

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -11,6 +11,11 @@ 

  import os

  import sys

  

+ if "PAGURE_CONFIG" not in os.environ and os.path.exists(

+     "/etc/pagure/pagure.cfg"

+ ):

+     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

+ 

  import pagure.lib

  from pagure.hooks import run_hook_file

  

@@ -1,63 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to mirror a repo to another location.

- """

- from __future__ import unicode_literals, print_function

- 

- 

- import logging

- import os

- import sys

- 

- 

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- 

- import pagure.config  # noqa: E402

- import pagure.exceptions  # noqa: E402

- import pagure.lib  # noqa: E402

- import pagure.lib.tasks_mirror  # noqa: E402

- import pagure.ui.plugins  # noqa: E402

- 

- 

- _log = logging.getLogger(__name__)

- _config = pagure.config.config

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

- 

- 

- def main(args):

- 

-     repo = pagure.lib.git.get_repo_name(abspath)

-     username = pagure.lib.git.get_username(abspath)

-     namespace = pagure.lib.git.get_repo_namespace(abspath)

-     if _config.get("HOOK_DEBUG", False):

-         print("repo:", repo)

-         print("user:", username)

-         print("namespace:", namespace)

- 

-     session = pagure.lib.create_session(_config["DB_URL"])

-     project = pagure.lib._get_project(

-         session, repo, user=username, namespace=namespace

-     )

- 

-     if not project:

-         print("Could not find a project corresponding to this git repo")

-         session.close()

-         return 1

- 

-     pagure.lib.tasks_mirror.mirror_project.delay(

-         username=project.user.user if project.is_fork else None,

-         namespace=project.namespace,

-         name=project.name,

-     )

- 

-     session.close()

-     return 0

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -1,76 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to block commit not having a 'Signed-off-by'

- statement.

- """

- 

- from __future__ import print_function, unicode_literals

- 

- import os

- import sys

- 

- 

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- 

- import pagure  # noqa: E402

- import pagure.exceptions  # noqa: E402

- import pagure.lib.link  # noqa: E402

- import pagure.ui.plugins  # noqa: E402

- 

- _config = pagure.config.config

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

- 

- 

- def run_as_pre_receive_hook():

- 

-     for line in sys.stdin:

-         if _config.get("HOOK_DEBUG", False):

-             print(line)

-         (oldrev, newrev, refname) = line.strip().split(" ", 2)

- 

-         if _config.get("HOOK_DEBUG", False):

-             print("  -- Old rev")

-             print(oldrev)

-             print("  -- New rev")

-             print(newrev)

-             print("  -- Ref name")

-             print(refname)

- 

-         if set(newrev) == set(["0"]):

-             print(

-                 "Deleting a reference/branch, so we won't run the "

-                 "hook to block unsigned commits"

-             )

-             return

- 

-         commits = pagure.lib.git.get_revs_between(

-             oldrev, newrev, abspath, refname

-         )

-         for commit in commits:

-             if _config.get("HOOK_DEBUG", False):

-                 print("Processing commit: %s" % commit)

-             signed = False

-             for line in pagure.lib.git.read_git_lines(

-                 ["log", "--no-walk", commit], abspath

-             ):

-                 if line.lower().strip().startswith("signed-off-by"):

-                     signed = True

-                     break

-             if _config.get("HOOK_DEBUG", False):

-                 print(" - Commit: %s is signed: %s" % (commit, signed))

-             if not signed:

-                 print("Commit %s is not signed" % commit)

-                 sys.exit(1)

- 

- 

- def main(args):

-     run_as_pre_receive_hook()

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -1,94 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to block non-fastforward pushes.

- """

- 

- from __future__ import print_function, unicode_literals

- 

- import os

- import sys

- 

- 

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- 

- import pagure  # noqa: E402

- import pagure.exceptions  # noqa: E402

- import pagure.lib.link  # noqa: E402

- import pagure.lib.plugins  # noqa: E402

- 

- 

- _config = pagure.config.config

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

- 

- 

- def run_as_pre_receive_hook():

-     reponame = pagure.lib.git.get_repo_name(abspath)

-     namespace = pagure.lib.git.get_repo_namespace(abspath)

-     username = pagure.lib.git.get_username(abspath)

-     session = pagure.lib.create_session(_config["DB_URL"])

-     if _config.get("HOOK_DEBUG", False):

-         print("repo:     ", reponame)

-         print("user:     ", username)

-         print("namspaces:", namespace)

- 

-     repo = pagure.lib._get_project(

-         session, reponame, user=username, namespace=namespace

-     )

- 

-     if not repo:

-         print(

-             "Unknown repo %s of username: %s in namespace %s"

-             % (reponame, username, namespace)

-         )

-         session.close()

-         sys.exit(1)

- 

-     plugin = pagure.lib.plugins.get_plugin("Block non fast-forward pushes")

-     plugin.db_object()

-     # Get the list of branches

-     branches = []

-     if repo.pagure_force_commit_hook:

-         branches = [

-             branch.strip()

-             for branch in repo.pagure_force_commit_hook.branches.split(",")

-             if branch.strip()

-         ]

- 

-     for line in sys.stdin:

-         if _config.get("HOOK_DEBUG", False):

-             print(line)

-         (oldrev, newrev, refname) = line.strip().split(" ", 2)

- 

-         refname = refname.replace("refs/heads/", "")

-         if refname in branches or branches == ["*"]:

-             if _config.get("HOOK_DEBUG", False):

-                 print("  -- Old rev")

-                 print(oldrev)

-                 print("  -- New rev")

-                 print(newrev)

-                 print("  -- Ref name")

-                 print(refname)

- 

-             if set(newrev) == set(["0"]):

-                 print("Deletion is forbidden")

-                 session.close()

-                 sys.exit(1)

-             elif pagure.lib.git.is_forced_push(oldrev, newrev, abspath):

-                 print("Non fast-forward push are forbidden")

-                 session.close()

-                 sys.exit(1)

- 

-     session.close()

- 

- 

- def main(args):

-     run_as_pre_receive_hook()

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -1,239 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to add comment on issues if the commits fixes or

- relates to an issue.

- """

- 

- from __future__ import print_function, unicode_literals

- 

- import logging

- import os

- import sys

- 

- import pygit2

- 

- from sqlalchemy.exc import SQLAlchemyError

- 

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- 

- import pagure.config  # noqa: E402

- import pagure.exceptions  # noqa: E402

- import pagure.lib.link  # noqa: E402

- 

- 

- _log = logging.getLogger(__name__)

- _config = pagure.config.config

- 

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

- 

- 

- def generate_revision_change_log(new_commits_list):

- 

-     print("Detailed log of new commits:\n\n")

-     commitid = None

-     for line in pagure.lib.git.read_git_lines(

-         ["log", "--no-walk"] + new_commits_list + ["--"], abspath

-     ):

-         if line.startswith("commit"):

-             commitid = line.split("commit ")[-1]

- 

-         line = line.strip()

-         session = pagure.lib.create_session(_config["DB_URL"])

-         print("*", line)

-         for relation in pagure.lib.link.get_relation(

-             session,

-             pagure.lib.git.get_repo_name(abspath),

-             pagure.lib.git.get_username(abspath),

-             pagure.lib.git.get_repo_namespace(abspath),

-             line,

-             "fixes",

-             include_prs=True,

-         ):

-             if _config.get("HOOK_DEBUG", False):

-                 print(commitid, relation)

-             fixes_relation(commitid, relation, session, _config.get("APP_URL"))

- 

-         for issue in pagure.lib.link.get_relation(

-             session,

-             pagure.lib.git.get_repo_name(abspath),

-             pagure.lib.git.get_username(abspath),

-             pagure.lib.git.get_repo_namespace(abspath),

-             line,

-             "relates",

-         ):

-             if _config.get("HOOK_DEBUG", False):

-                 print(commitid, issue)

-             relates_commit(commitid, issue, session, _config.get("APP_URL"))

- 

-         session.close()

- 

- 

- def relates_commit(commitid, issue, session, app_url=None):

-     """ Add a comment to an issue that this commit relates to it. """

- 

-     url = "../%s" % commitid[:8]

-     if app_url:

-         if app_url.endswith("/"):

-             app_url = app_url[:-1]

-         project = issue.project.fullname

-         if issue.project.is_fork:

-             project = "fork/%s" % project

-         url = "%s/%s/c/%s" % (app_url, project, commitid[:8])

- 

-     comment = """ Commit [%s](%s) relates to this ticket""" % (

-         commitid[:8],

-         url,

-     )

- 

-     user = os.environ.get(

-         "GL_USER", pagure.lib.git.get_author_email(commitid, abspath)

-     )

- 

-     try:

-         pagure.lib.add_issue_comment(

-             session,

-             issue=issue,

-             comment=comment,

-             user=user,

-             ticketfolder=_config["TICKETS_FOLDER"],

-         )

-         session.commit()

-     except pagure.exceptions.PagureException as err:

-         print(err)

-     except SQLAlchemyError as err:  # pragma: no cover

-         session.rollback()

-         _log.exception(err)

- 

- 

- def fixes_relation(commitid, relation, session, app_url=None):

-     """ Add a comment to an issue or PR that this commit fixes it and update

-     the status if the commit is in the master branch. """

- 

-     url = "../c/%s" % commitid[:8]

-     if app_url:

-         if app_url.endswith("/"):

-             app_url = app_url[:-1]

-         project = relation.project.fullname

-         if relation.project.is_fork:

-             project = "fork/%s" % project

-         url = "%s/%s/c/%s" % (app_url, project, commitid[:8])

- 

-     comment = """ Commit [%s](%s) fixes this %s""" % (

-         commitid[:8],

-         url,

-         relation.isa,

-     )

- 

-     user = os.environ.get(

-         "GL_USER", pagure.lib.git.get_author_email(commitid, abspath)

-     )

- 

-     try:

-         if relation.isa == "issue":

-             pagure.lib.add_issue_comment(

-                 session,

-                 issue=relation,

-                 comment=comment,

-                 user=user,

-                 ticketfolder=_config["TICKETS_FOLDER"],

-             )

-         elif relation.isa == "pull-request":

-             pagure.lib.add_pull_request_comment(

-                 session,

-                 request=relation,

-                 commit=None,

-                 tree_id=None,

-                 filename=None,

-                 row=None,

-                 comment=comment,

-                 user=user,

-                 requestfolder=_config["REQUESTS_FOLDER"],

-             )

-         session.commit()

-     except pagure.exceptions.PagureException as err:

-         print(err)

-     except SQLAlchemyError as err:  # pragma: no cover

-         session.rollback()

-         _log.exception(err)

- 

-     try:

-         if relation.isa == "issue":

-             pagure.lib.edit_issue(

-                 session,

-                 relation,

-                 ticketfolder=_config["TICKETS_FOLDER"],

-                 user=user,

-                 status="Closed",

-                 close_status="Fixed",

-             )

-         elif relation.isa == "pull-request":

-             pagure.lib.close_pull_request(

-                 session,

-                 relation,

-                 requestfolder=_config["REQUESTS_FOLDER"],

-                 user=user,

-                 merged=True,

-             )

-         session.commit()

-     except pagure.exceptions.PagureException as err:

-         print(err)

-     except SQLAlchemyError as err:  # pragma: no cover

-         session.rollback()

-         print("ERROR", err)

-         _log.exception(err)

- 

- 

- def run_as_post_receive_hook():

- 

-     for line in sys.stdin:

-         if _config.get("HOOK_DEBUG", False):

-             print(line)

-         (oldrev, newrev, refname) = line.strip().split(" ", 2)

- 

-         if _config.get("HOOK_DEBUG", False):

-             print("  -- Old rev")

-             print(oldrev)

-             print("  -- New rev")

-             print(newrev)

-             print("  -- Ref name")

-             print(refname)

- 

-         # Retrieve the default branch

-         repo_obj = pygit2.Repository(abspath)

-         default_branch = None

-         if not repo_obj.is_empty and not repo_obj.head_is_unborn:

-             default_branch = repo_obj.head.shorthand

- 

-         # Skip all branch but the default one

-         refname = refname.replace("refs/heads/", "")

-         if refname != default_branch:

-             continue

- 

-         if set(newrev) == set(["0"]):

-             print(

-                 "Deleting a reference/branch, so we won't run the "

-                 "pagure hook"

-             )

-             return

- 

-         generate_revision_change_log(

-             pagure.lib.git.get_revs_between(oldrev, newrev, abspath, refname)

-         )

- 

-     if _config.get("HOOK_DEBUG", False):

-         print("ns  :", pagure.lib.git.get_repo_namespace(abspath))

-         print("repo:", pagure.lib.git.get_repo_name(abspath))

-         print("user:", pagure.lib.git.get_username(abspath))

- 

- 

- def main(args):

-     run_as_post_receive_hook()

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -1,98 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to update pull-requests stored in the database

- based on the information pushed in the requests git repository.

- """

- 

- from __future__ import print_function, unicode_literals

- 

- import os

- import sys

- 

- 

- # We need to access the database

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- 

- import pagure.config  # noqa: E402

- import pagure.lib.tasks_services  # noqa: E402

- 

- 

- _config = pagure.config.config

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

- 

- 

- def get_files_to_load(new_commits_list):

- 

-     print("Files changed by new commits:\n")

-     file_list = []

-     new_commits_list.reverse()

-     for commit in new_commits_list:

-         filenames = pagure.lib.git.read_git_lines(

-             ["diff-tree", "--no-commit-id", "--name-only", "-r", commit],

-             abspath,

-         )

-         for line in filenames:

-             if line.strip():

-                 file_list.append(line.strip())

- 

-     return file_list

- 

- 

- def run_as_post_receive_hook():

- 

-     repo = pagure.lib.git.get_repo_name(abspath)

-     username = pagure.lib.git.get_username(abspath)

-     namespace = pagure.lib.git.get_repo_namespace(

-         abspath, gitfolder=_config["TICKETS_FOLDER"]

-     )

-     if _config.get("HOOK_DEBUG", False):

-         print("repo:", repo)

-         print("user:", username)

-         print("namespace:", namespace)

- 

-     for line in sys.stdin:

-         if _config.get("HOOK_DEBUG", False):

-             print(line)

-         (oldrev, newrev, refname) = line.strip().split(" ", 2)

- 

-         if _config.get("HOOK_DEBUG", False):

-             print("  -- Old rev")

-             print(oldrev)

-             print("  -- New rev")

-             print(newrev)

-             print("  -- Ref name")

-             print(refname)

- 

-         if set(newrev) == set(["0"]):

-             print(

-                 "Deleting a reference/branch, so we won't run the "

-                 "pagure hook"

-             )

-             return

- 

-         commits = pagure.lib.git.get_revs_between(

-             oldrev, newrev, abspath, refname

-         )

- 

-         pagure.lib.tasks_services.load_json_commits_to_db.delay(

-             name=repo,

-             commits=commits,

-             abspath=abspath,

-             data_type="pull-request",

-             agent=os.environ.get("GL_USER"),

-             namespace=namespace,

-             username=username,

-         )

- 

- 

- def main(args):

-     run_as_post_receive_hook()

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -1,79 +0,0 @@ 

- #!/usr/bin/env python

- 

- 

- """Pagure specific hook to update tickets stored in the database based on

- the information pushed in the tickets git repository.

- """

- from __future__ import print_function, unicode_literals

- 

- import os

- import sys

- 

- 

- # We need to access the database

- if "PAGURE_CONFIG" not in os.environ and os.path.exists(

-     "/etc/pagure/pagure.cfg"

- ):

-     os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

- 

- import pagure.config  # noqa: E402

- import pagure.lib.tasks_services  # noqa: E402

- 

- 

- _config = pagure.config.config

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

- 

- 

- def run_as_post_receive_hook():

- 

-     repo = pagure.lib.git.get_repo_name(abspath)

-     username = pagure.lib.git.get_username(abspath)

-     namespace = pagure.lib.git.get_repo_namespace(

-         abspath, gitfolder=_config["TICKETS_FOLDER"]

-     )

-     if _config.get("HOOK_DEBUG", False):

-         print("repo:", repo)

-         print("user:", username)

-         print("namespace:", namespace)

- 

-     for line in sys.stdin:

-         if _config.get("HOOK_DEBUG", False):

-             print(line)

-         (oldrev, newrev, refname) = line.strip().split(" ", 2)

- 

-         if _config.get("HOOK_DEBUG", False):

-             print("  -- Old rev")

-             print(oldrev)

-             print("  -- New rev")

-             print(newrev)

-             print("  -- Ref name")

-             print(refname)

- 

-         if set(newrev) == set(["0"]):

-             print(

-                 "Deleting a reference/branch, so we won't run the "

-                 "pagure hook"

-             )

-             return

- 

-         commits = pagure.lib.git.get_revs_between(

-             oldrev, newrev, abspath, refname

-         )

- 

-         pagure.lib.tasks_services.load_json_commits_to_db.delay(

-             name=repo,

-             commits=commits,

-             abspath=abspath,

-             data_type="ticket",

-             agent=os.environ.get("GL_USER"),

-             namespace=namespace,

-             username=username,

-         )

- 

- 

- def main(args):

-     run_as_post_receive_hook()

- 

- 

- if __name__ == "__main__":

-     main(sys.argv[1:])

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

@@ -1,12 +0,0 @@ 

- #!/bin/bash

- # adapted from https://stackoverflow.com/a/29880516

- 

- retcode=0

- while read -r old new refname; do

-     if [[ $refname == refs/heads/* && $old != *[^0]* ]]; then

-         retcode=1

-         echo "Creating new branches is disabled on this project."

-     fi

- done

- 

- exit $retcode

@@ -0,0 +1,1 @@ 

+ /does/not/exist 

\ No newline at end of file

file modified
+30 -1
@@ -19,12 +19,16 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

+ import pagure.config

  import pagure.lib.tasks_mirror

- from pagure.hooks import BaseHook, RequiredIf

+ from pagure.hooks import BaseHook, BaseRunner, RequiredIf

  from pagure.lib.model import BASE, Project

  from pagure.utils import get_repo_path, ssh_urlpattern

  

  

+ _config = pagure.config.reload_config()

+ 

+ 

  class MirrorTable(BASE):

      """ Stores information about the mirroring hook deployed on a project.

  
@@ -60,6 +64,30 @@ 

      )

  

  

+ class MirrorRunner(BaseRunner):

+     """ Runner for the mirror hook. """

+ 

+     @staticmethod

+     def post_receive(session, username, project, repotype, repodir, changes):

+         """ Run the default post-receive hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+         print("Running the default hook")

+         if repotype != "main":

+             if _config.get("HOOK_DEBUG", False):

+                 print("Default hook only runs on the main project repository")

+                 return

+ 

+         pagure.lib.tasks_mirror.mirror_project.delay(

+             username=project.user.user if project.is_fork else None,

+             namespace=project.namespace,

+             name=project.name,

+         )

+ 

+         session.close()

+ 

+ 

  class CustomRegexp(wtforms.validators.Regexp):

      def __init__(self, *args, **kwargs):

          self.optional = kwargs.get("optional") or False
@@ -120,6 +148,7 @@ 

      backref = "mirror_hook"

      form_fields = ["active", "target", "public_key", "last_log"]

      form_fields_readonly = ["public_key", "last_log"]

+     runner = MirrorRunner

  

      @classmethod

      def install(cls, project, dbobj):

file modified
+11 -1
@@ -22,7 +22,7 @@ 

  from sqlalchemy.orm import backref

  

  import pagure.lib

- from pagure.hooks import BaseHook, RequiredIf

+ from pagure.hooks import BaseHook, BaseRunner, RequiredIf

  from pagure.lib.model import BASE, Project

  

  
@@ -62,6 +62,15 @@ 

      )

  

  

+ class PagureCIRunner(BaseRunner):

+     """ Runner for the pagure-ci hook, it does nothing as the magic is part

+     of the CI system itself (to see if there was a commit made and build if

+     so).

+     """

+ 

+     pass

+ 

+ 

  tmpl = """

  {% if repo | hasattr('ci_hook') and repo.ci_hook and

      repo.ci_hook.pagure_ci_token %}
@@ -154,6 +163,7 @@ 

      db_object = PagureCITable

      backref = "ci_hook"

      form_fields = ["ci_type", "ci_url", "ci_job", "active_commit", "active_pr"]

+     runner = PagureCIRunner

  

      @classmethod

      def set_up(cls, project):

@@ -10,8 +10,9 @@ 

  

  from __future__ import unicode_literals

  

+ import sys

+ 

  import sqlalchemy as sa

- import pygit2

  import wtforms

  

  try:
@@ -21,9 +22,9 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.hooks import BaseHook, RequiredIf

+ import pagure.lib.git

+ from pagure.hooks import BaseHook, BaseRunner, RequiredIf

  from pagure.lib.model import BASE, Project

- from pagure.utils import get_repo_path

  

  

  class PagureForceCommitTable(BASE):
@@ -60,6 +61,43 @@ 

      )

  

  

+ class PagureForceCommitRunner(BaseRunner):

+     """ Runner for the hook blocking force push. """

+ 

+     @staticmethod

+     def pre_receive(session, username, project, repotype, repodir, changes):

+         """ Run the pre-receive tasks of a hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+ 

+         # Get the list of branches

+         branches = []

+         if project.pagure_force_commit_hook:

+             branches = [

+                 branch.strip()

+                 for branch in project.pagure_force_commit_hook.branches.split(

+                     ","

+                 )

+                 if branch.strip()

+             ]

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             refname = refname.replace("refs/heads/", "")

+             if refname in branches or branches == ["*"]:

+ 

+                 if set(newrev) == set(["0"]):

+                     print("Deletion is forbidden")

+                     session.close()

+                     sys.exit(1)

+                 elif pagure.lib.git.is_forced_push(oldrev, newrev, repodir):

+                     print("Non fast-forward push are forbidden")

+                     session.close()

+                     sys.exit(1)

+ 

+ 

  class PagureForceCommitForm(FlaskForm):

      """ Form to configure the pagure hook. """

  
@@ -83,33 +121,4 @@ 

      backref = "pagure_force_commit_hook"

      form_fields = ["branches", "active"]

      hook_type = "pre-receive"

- 

-     @classmethod

-     def install(cls, project, dbobj):

-         """ Method called to install the hook for a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         # Init the git repo in case

-         repopaths = [get_repo_path(project)]

-         pygit2.Repository(repopaths[0])

- 

-         cls.base_install(

-             repopaths,

-             dbobj,

-             "pagureforcecommit",

-             "pagure_force_commit_hook.py",

-         )

- 

-     @classmethod

-     def remove(cls, project):

-         """ Method called to remove the hook of a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

-         cls.base_remove(repopaths, "pagureforcecommit")

+     runner = PagureForceCommitRunner

file modified
+204 -41
@@ -1,7 +1,7 @@ 

  # -*- coding: utf-8 -*-

  

  """

-  (c) 2014-2016 - Copyright Red Hat Inc

+  (c) 2014-2018 - Copyright Red Hat Inc

  

   Authors:

     Pierre-Yves Chibon <pingou@pingoured.fr>
@@ -10,8 +10,9 @@ 

  

  from __future__ import unicode_literals

  

- import os

+ import logging

  

+ import pygit2

  import sqlalchemy as sa

  import wtforms

  
@@ -19,13 +20,18 @@ 

      from flask_wtf import FlaskForm

  except ImportError:

      from flask_wtf import Form as FlaskForm

+ from sqlalchemy.exc import SQLAlchemyError

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.config import config as pagure_config

- from pagure.hooks import BaseHook

+ import pagure.config

+ import pagure.lib.git

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

- from pagure.utils import get_repo_path

+ 

+ 

+ _log = logging.getLogger(__name__)

+ pagure_config = pagure.config.reload_config()

  

  

  class PagureTable(BASE):
@@ -59,6 +65,198 @@ 

      )

  

  

+ def generate_revision_change_log(

+     session, project, username, repodir, new_commits_list

+ ):

+ 

+     print("Detailed log of new commits:\n\n")

+     commitid = None

+     for line in pagure.lib.git.read_git_lines(

+         ["log", "--no-walk"] + new_commits_list + ["--"], repodir

+     ):

+         if line.startswith("commit"):

+             commitid = line.split("commit ")[-1]

+ 

+         line = line.strip()

+         print("*", line)

+         for issue_or_pr in pagure.lib.link.get_relation(

+             session,

+             project.name,

+             project.username if project.is_fork else None,

+             project.namespace,

+             line,

+             "fixes",

+             include_prs=True,

+         ):

+             if pagure_config.get("HOOK_DEBUG", False):

+                 print(commitid, relation)

+             fixes_relation(

+                 session,

+                 username,

+                 commitid,

+                 issue_or_pr,

+                 pagure_config.get("APP_URL"),

+             )

+ 

+         for issue in pagure.lib.link.get_relation(

+             session,

+             project.name,

+             project.username if project.is_fork else None,

+             project.namespace,

+             line,

+             "relates",

+         ):

+             if pagure_config.get("HOOK_DEBUG", False):

+                 print(commitid, issue)

+             relates_commit(

+                 session,

+                 username,

+                 commitid,

+                 issue,

+                 pagure_config.get("APP_URL"),

+             )

+ 

+ 

+ def relates_commit(session, username, commitid, issue, app_url=None):

+     """ Add a comment to an issue that this commit relates to it. """

+ 

+     url = "../%s" % commitid[:8]

+     if app_url:

+         if app_url.endswith("/"):

+             app_url = app_url[:-1]

+         project = issue.project.fullname

+         if issue.project.is_fork:

+             project = "fork/%s" % project

+         url = "%s/%s/c/%s" % (app_url, project, commitid[:8])

+ 

+     comment = """ Commit [%s](%s) relates to this ticket""" % (

+         commitid[:8],

+         url,

+     )

+ 

+     try:

+         pagure.lib.add_issue_comment(

+             session, issue=issue, comment=comment, user=username

+         )

+         session.commit()

+     except pagure.exceptions.PagureException as err:

+         print(err)

+     except SQLAlchemyError as err:  # pragma: no cover

+         session.rollback()

+         _log.exception(err)

+ 

+ 

+ def fixes_relation(session, username, commitid, relation, app_url=None):

+     """ Add a comment to an issue or PR that this commit fixes it and update

+     the status if the commit is in the master branch. """

+ 

+     url = "../c/%s" % commitid[:8]

+     if app_url:

+         if app_url.endswith("/"):

+             app_url = app_url[:-1]

+         project = relation.project.fullname

+         if relation.project.is_fork:

+             project = "fork/%s" % project

+         url = "%s/%s/c/%s" % (app_url, project, commitid[:8])

+ 

+     comment = """ Commit [%s](%s) fixes this %s""" % (

+         commitid[:8],

+         url,

+         relation.isa,

+     )

+ 

+     try:

+         if relation.isa == "issue":

+             pagure.lib.add_issue_comment(

+                 session, issue=relation, comment=comment, user=username

+             )

+         elif relation.isa == "pull-request":

+             pagure.lib.add_pull_request_comment(

+                 session,

+                 request=relation,

+                 commit=None,

+                 tree_id=None,

+                 filename=None,

+                 row=None,

+                 comment=comment,

+                 user=username,

+             )

+         session.commit()

+     except pagure.exceptions.PagureException as err:

+         print(err)

+     except SQLAlchemyError as err:  # pragma: no cover

+         session.rollback()

+         _log.exception(err)

+ 

+     try:

+         if relation.isa == "issue":

+             pagure.lib.edit_issue(

+                 session,

+                 relation,

+                 user=username,

+                 status="Closed",

+                 close_status="Fixed",

+             )

+         elif relation.isa == "pull-request":

+             pagure.lib.close_pull_request(

+                 session, relation, user=username, merged=True

+             )

+         session.commit()

+     except pagure.exceptions.PagureException as err:

+         print(err)

+     except SQLAlchemyError as err:  # pragma: no cover

+         session.rollback()

+         print("ERROR", err)

+         _log.exception(err)

+ 

+ 

+ class PagureRunner(BaseRunner):

+     """ Runner for the pagure's specific git hook. """

+ 

+     @staticmethod

+     def post_receive(session, username, project, repotype, repodir, changes):

+         """ Run the default post-receive hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+ 

+         if repotype != "main":

+             print("The pagure hook only runs on the main git repo.")

+             return

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             # Retrieve the default branch

+             repo_obj = pygit2.Repository(repodir)

+             default_branch = None

+             if not repo_obj.is_empty and not repo_obj.head_is_unborn:

+                 default_branch = repo_obj.head.shorthand

+ 

+             # Skip all branch but the default one

+             refname = refname.replace("refs/heads/", "")

+             if refname != default_branch:

+                 continue

+ 

+             if set(newrev) == set(["0"]):

+                 print(

+                     "Deleting a reference/branch, so we won't run the "

+                     "pagure hook"

+                 )

+                 return

+ 

+             generate_revision_change_log(

+                 session,

+                 project,

+                 username,

+                 repodir,

+                 pagure.lib.git.get_revs_between(

+                     oldrev, newrev, repodir, refname

+                 ),

+             )

+             session.close()

+ 

+ 

  class PagureForm(FlaskForm):

      """ Form to configure the pagure hook. """

  
@@ -109,39 +307,4 @@ 

      db_object = PagureTable

      backref = "pagure_hook"

      form_fields = ["active"]

- 

-     @classmethod

-     def install(cls, project, dbobj):

-         """ Method called to install the hook for a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

-         for folder in [

-             pagure_config.get("DOCS_FOLDER"),

-             pagure_config.get("REQUESTS_FOLDER"),

-         ]:

-             if folder:

-                 repopaths.append(os.path.join(folder, project.path))

- 

-         cls.base_install(repopaths, dbobj, "pagure", "pagure_hook.py")

- 

-     @classmethod

-     def remove(cls, project):

-         """ Method called to remove the hook of a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

-         for folder in [

-             pagure_config.get("DOCS_FOLDER"),

-             pagure_config.get("REQUESTS_FOLDER"),

-         ]:

-             if folder:

-                 repopaths.append(os.path.join(folder, project.path))

- 

-         cls.base_remove(repopaths, "pagure")

+     runner = PagureRunner

@@ -9,6 +9,7 @@ 

  """

  

  from __future__ import unicode_literals

+ import sys

  

  import sqlalchemy as sa

  import wtforms
@@ -20,9 +21,8 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.hooks import BaseHook

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

- from pagure.utils import get_repo_path

  

  

  class PagureNoNewBranchesTable(BASE):
@@ -56,6 +56,27 @@ 

      )

  

  

+ class PagureNoNewBranchRunner(BaseRunner):

+     """ Runner for the hook blocking new branches from being created. """

+ 

+     @staticmethod

+     def pre_receive(session, username, project, repotype, repodir, changes):

+         """ Run the pre-receive tasks of a hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             if set(oldrev) == set(["0"]):

+                 print(

+                     "Creating a new reference/branch is not allowed in this"

+                     " project."

+                 )

+                 sys.exit(1)

+ 

+ 

  class PagureNoNewBranchesForm(FlaskForm):

      """ Form to configure the pagure hook. """

  
@@ -72,32 +93,4 @@ 

      backref = "pagure_hook_no_new_branches"

      form_fields = ["active"]

      hook_type = "pre-receive"

- 

-     @classmethod

-     def install(cls, project, dbobj):

-         """ Method called to install the hook for a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

- 

-         cls.base_install(

-             repopaths,

-             dbobj,

-             "pagure_no_new_branches",

-             "pagure_no_new_branches",

-         )

- 

-     @classmethod

-     def remove(cls, project):

-         """ Method called to remove the hook of a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

- 

-         cls.base_remove(repopaths, "pagure_no_new_branches")

+     runner = PagureNoNewBranchRunner

@@ -10,9 +10,6 @@ 

  

  from __future__ import unicode_literals

  

- import os

- 

- import flask

  import sqlalchemy as sa

  import wtforms

  
@@ -23,8 +20,8 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.config import config as pagure_config

- from pagure.hooks import BaseHook

+ import pagure.lib.git

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

  

  
@@ -60,6 +57,50 @@ 

      )

  

  

+ class PagureRequestRunner(BaseRunner):

+     """ Runner for the hook updating the db about requests on push to the

+     git repo containing the meta-data about pull-requests.

+     """

+ 

+     @staticmethod

+     def post_receive(session, username, project, repotype, repodir, changes):

+         """ Run the default post-receive hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+ 

+         if repotype != "requests":

+             print(

+                 "The pagure requests hook only runs on the requests "

+                 "git repo."

+             )

+             return

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             if set(newrev) == set(["0"]):

+                 print(

+                     "Deleting a reference/branch, so we won't run the "

+                     "pagure hook"

+                 )

+                 return

+ 

+             commits = pagure.lib.git.get_revs_between(

+                 oldrev, newrev, repodir, refname

+             )

+ 

+             pagure.lib.tasks_services.load_json_commits_to_db.delay(

+                 name=project.name,

+                 commits=commits,

+                 abspath=repodir,

+                 data_type="pull-request",

+                 agent=username,

+                 namespace=project.namespace,

+                 username=project.user.user if project.is_fork else None,

+             )

+ 

+ 

  class PagureRequestsForm(FlaskForm):

      """ Form to configure the pagure hook. """

  
@@ -79,57 +120,4 @@ 

      db_object = PagureRequestsTable

      backref = "pagure_hook_requests"

      form_fields = ["active"]

- 

-     @classmethod

-     def set_up(cls, project):

-         """ Install the generic post-receive hook that allow us to call

-         multiple post-receive hooks as set per plugin.

-         """

-         repopath = os.path.join(pagure_config["REQUESTS_FOLDER"], project.path)

-         if not os.path.exists(repopath):

-             flask.abort(404, "No git repo found")

- 

-         hook_files = os.path.join(

-             os.path.dirname(os.path.realpath(__file__)), "files"

-         )

- 

-         # Make sure the hooks folder exists

-         hookfolder = os.path.join(repopath, "hooks")

-         if not os.path.exists(hookfolder):

-             os.makedirs(hookfolder)

- 

-         # Install the main post-receive file

-         postreceive = os.path.join(hookfolder, "post-receive")

-         hook_file = os.path.join(hook_files, "post-receive")

-         if not os.path.exists(postreceive):

-             os.symlink(hook_file, postreceive)

- 

-     @classmethod

-     def install(cls, project, dbobj):

-         """ Method called to install the hook for a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [

-             os.path.join(pagure_config["REQUESTS_FOLDER"], project.path)

-         ]

- 

-         cls.base_install(

-             repopaths, dbobj, "pagure-requests", "pagure_hook_requests.py"

-         )

- 

-     @classmethod

-     def remove(cls, project):

-         """ Method called to remove the hook of a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [

-             os.path.join(pagure_config["REQUESTS_FOLDER"], project.path)

-         ]

- 

-         cls.base_remove(repopaths, "pagure-requests")

+     runner = PagureRequestRunner

@@ -23,8 +23,9 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

+ import pagure.lib

  from pagure.config import config as pagure_config

- from pagure.hooks import BaseHook

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

  

  
@@ -59,6 +60,45 @@ 

      )

  

  

+ class PagureTicketRunner(BaseRunner):

+     """ Runner for the git hook updating the DB of tickets on push. """

+ 

+     @staticmethod

+     def pre_receive(session, username, project, repotype, repodir, changes):

+         """ Run the pre-receive tasks of a hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+ 

+         if repotype != "tickets":

+             print("The ticket hook only runs on the ticket git repository.")

+             return

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             if set(newrev) == set(["0"]):

+                 print(

+                     "Deleting a reference/branch, so we won't run the "

+                     "pagure hook"

+                 )

+                 return

+ 

+             commits = pagure.lib.git.get_revs_between(

+                 oldrev, newrev, repodir, refname

+             )

+ 

+             pagure.lib.tasks_services.load_json_commits_to_db.delay(

+                 name=project.name,

+                 commits=commits,

+                 abspath=repodir,

+                 data_type="ticket",

+                 agent=username,

+                 namespace=project.namespace,

+                 username=project.user.user if project.is_fork else None,

+             )

+ 

+ 

  class PagureTicketsForm(FlaskForm):

      """ Form to configure the pagure hook. """

  
@@ -78,6 +118,7 @@ 

      db_object = PagureTicketsTable

      backref = "pagure_hook_tickets"

      form_fields = ["active"]

+     runner = PagureTicketRunner

  

      @classmethod

      def set_up(cls, project):

@@ -10,6 +10,8 @@ 

  

  from __future__ import unicode_literals

  

+ import sys

+ 

  import sqlalchemy as sa

  import wtforms

  
@@ -20,9 +22,12 @@ 

  from sqlalchemy.orm import relation

  from sqlalchemy.orm import backref

  

- from pagure.hooks import BaseHook

+ import pagure.config

+ from pagure.hooks import BaseHook, BaseRunner

  from pagure.lib.model import BASE, Project

- from pagure.utils import get_repo_path

+ 

+ 

+ _config = pagure.config.reload_config()

  

  

  class PagureUnsignedCommitTable(BASE):
@@ -57,6 +62,46 @@ 

      )

  

  

+ class PagureUnsignerRunner(BaseRunner):

+     """ Runner for the hook blocking unsigned commits. """

+ 

+     @staticmethod

+     def pre_receive(session, username, project, repotype, repodir, changes):

+         """ Run the pre-receive tasks of a hook.

+ 

+         For args, see BaseRunner.runhook.

+         """

+ 

+         for refname in changes:

+             (oldrev, newrev) = changes[refname]

+ 

+             if set(newrev) == set(["0"]):

+                 print(

+                     "Deleting a reference/branch, so we won't run the "

+                     "hook to block unsigned commits"

+                 )

+                 return

+ 

+             commits = pagure.lib.git.get_revs_between(

+                 oldrev, newrev, repodir, refname

+             )

+             for commit in commits:

+                 if _config.get("HOOK_DEBUG", False):

+                     print("Processing commit: %s" % commit)

+                 signed = False

+                 for line in pagure.lib.git.read_git_lines(

+                     ["log", "--no-walk", commit], repodir

+                 ):

+                     if line.lower().strip().startswith("signed-off-by"):

+                         signed = True

+                         break

+                 if _config.get("HOOK_DEBUG", False):

+                     print(" - Commit: %s is signed: %s" % (commit, signed))

+                 if not signed:

+                     print("Commit %s is not signed" % commit)

+                     sys.exit(1)

+ 

+ 

  class PagureUnsignedCommitForm(FlaskForm):

      """ Form to configure the pagure hook. """

  
@@ -76,32 +121,4 @@ 

      backref = "pagure_unsigned_commit_hook"

      form_fields = ["active"]

      hook_type = "pre-receive"

- 

-     @classmethod

-     def install(cls, project, dbobj):

-         """ Method called to install the hook for a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

- 

-         cls.base_install(

-             repopaths,

-             dbobj,

-             "pagureunsignedcommit",

-             "pagure_block_unsigned.py",

-         )

- 

-     @classmethod

-     def remove(cls, project):

-         """ Method called to remove the hook of a project.

- 

-         :arg project: a ``pagure.model.Project`` object to which the hook

-             should be installed

- 

-         """

-         repopaths = [get_repo_path(project)]

- 

-         cls.base_remove(repopaths, "pagureunsignedcommit")

+     runner = PagureUnsignerRunner

file modified
+7 -15
@@ -16,7 +16,6 @@ 

  import logging

  import os

  

- import celery

  import flask

  import pygit2

  
@@ -227,18 +226,18 @@ 

              )

          except pygit2.GitError as err:

              response = flask.jsonify(

-                 {"code": "CONFLICTS", "message": "%s" % err})

+                 {"code": "CONFLICTS", "message": "%s" % err}

+             )

              response.status_code = 409

              return response

          except pagure.exceptions.PagureException as err:

              response = flask.jsonify(

-                 {"code": "CONFLICTS", "message": "%s" % err})

+                 {"code": "CONFLICTS", "message": "%s" % err}

+             )

              response.status_code = 500

              return response

  

-     return flask.jsonify(

-         pagure.utils.get_merge_options(request, merge_status)

-     )

+     return flask.jsonify(pagure.utils.get_merge_options(request, merge_status))

  

  

  @PV.route("/pull-request/ready", methods=["POST"])
@@ -297,17 +296,10 @@ 

              response.status_code = 400

              return response

      task = pagure.lib.tasks.pull_request_ready_branch.delay(

-         namespace=args_namespace,

-         name=args_reponame,

-         user=args_user,

+         namespace=args_namespace, name=args_reponame, user=args_user

      )

  

-     return flask.jsonify(

-         {

-             "code": "OK",

-             "task": task.id,

-         }

-     )

+     return flask.jsonify({"code": "OK", "task": task.id})

  

  

  @PV.route("/<repo>/issue/template", methods=["POST"])

file modified
+1 -1
@@ -2237,7 +2237,7 @@ 

          backref=backref(

              "flags",

              order_by=str("(pull_request_flags.c.date_created).desc()"),

-             cascade="delete, delete-orphan"

+             cascade="delete, delete-orphan",

          ),

          foreign_keys=[pull_request_uid],

          remote_side=[PullRequest.uid],

file modified
+6 -1
@@ -50,17 +50,22 @@ 

              return plugin

  

  

- def get_enabled_plugins(project):

+ def get_enabled_plugins(project, with_default=False):

      """ Returns a list of plugins enabled for a specific project.

  

      Args:

          project (model.Project): The project to look for.

+         with_default (boolean): Wether or not the returned list should

+             include the default hook/plugin.

      Returns: (list): A  list of tuples (pluginclass, dbobj) with the plugin

          classess and dbobjects for plugins enabled for the project.

      """

      from pagure.hooks import BaseHook

+     from pagure.hooks.default import Default

  

      enabled = []

+     if with_default:

+         enabled = [(Default(), None)]

      for plugin in load("pagure.hooks", subclasses=BaseHook):

          if plugin.db_object and hasattr(project, plugin.backref):

              dbobj = getattr(project, plugin.backref)

file modified
+2 -1
@@ -383,7 +383,8 @@ 

  @conn.task(queue=pagure_config.get("CI_CELERY_QUEUE", None), bind=True)

  @pagure_task

  def trigger_ci_build(

-         self, session, cause, branch, ci_type, project_name=None, pr_uid=None):

+     self, session, cause, branch, ci_type, project_name=None, pr_uid=None

+ ):

  

      """ Triggers a new run of the CI system on the specified pull-request.

  

file modified
+3 -2
@@ -526,12 +526,13 @@ 

              "code": "CONFLICTS",

              "short_code": "Conflicts",

              "message": "The pull-request must be rebased before merging",

-         }

+         },

      }

  

      if merge_status == "MERGE":

          if request.project.settings.get(

-                 "disable_non_fast-forward_merges", False):

+             "disable_non_fast-forward_merges", False

+         ):

              merge_status += "-non-ff-bad"

          else:

              merge_status += "-non-ff-ok"

file modified
+4 -2
@@ -28,6 +28,8 @@ 

  RUNNING = []

  FAILED = []

  NUMPROCS = multiprocessing.cpu_count() - 1

+ if os.environ.get('BUILD_ID'):

+     NUMPROCS = multiprocessing.cpu_count()

  

  

  def setup_parser():
@@ -321,7 +323,7 @@ 

                  continue

              suites.append(fname.replace(".py", ""))

  

-     _run_test_suites(args, suites)

+     return _run_test_suites(args, suites)

  

  

  def do_rerun(args):
@@ -360,7 +362,7 @@ 

          if suite.startswith(("py2-", "py3-")):

              suites.append(suite[4:])

  

-     _run_test_suites(args, set(suites))

+     return _run_test_suites(args, set(suites))

  

  

  def _get_pyvers(args):

@@ -484,10 +484,10 @@ 

          request = pagure.lib.search_pull_requests(

              self.session, project_id=1, requestid=1)

          self.assertEqual(len(request.flags), 2)

-         self.assertEqual(request.flags[0].comment, 'Tests passed')

-         self.assertEqual(request.flags[0].percent, 100)

-         self.assertEqual(request.flags[1].comment, 'Tests running again')

-         self.assertEqual(request.flags[1].percent, None)

+         self.assertEqual(request.flags[0].comment, 'Tests running again')

+         self.assertEqual(request.flags[0].percent, None)

+         self.assertEqual(request.flags[1].comment, 'Tests passed')

+         self.assertEqual(request.flags[1].percent, 100)

  

      @patch.dict('pagure.config.config',

                  {

@@ -36,6 +36,8 @@ 

  class PagureFlaskInternaltests(tests.Modeltests):

      """ Tests for flask Internal controller of pagure """

  

+     maxDiff = None

+ 

      def setUp(self):

          """ Set up the environnment, ran before every tests. """

          super(PagureFlaskInternaltests, self).setUp()
@@ -2382,12 +2384,9 @@ 

          js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

              sorted(js_data.keys()),

-             ['code', 'message']

+             ['code', 'task']

          )

          self.assertEqual(js_data['code'], 'OK')

-         self.assertEqual(

-             js_data['message'],

-             {'branch_w_pr': {}, 'new_branch': {}})

  

      def test_get_pull_request_ready_branch_on_fork(self):

          '''Test the get_pull_request_ready_branch from the internal API on
@@ -2433,18 +2432,19 @@ 

          js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

              sorted(js_data.keys()),

-             ['code', 'message']

+             ['code', 'task']

          )

          self.assertEqual(js_data['code'], 'OK')

-         self.assertListEqual(

-             sorted(js_data['message'].keys()),

-             ['branch_w_pr', 'new_branch'])

-         self.assertEqual(js_data['message']['branch_w_pr'], {})

-         self.assertListEqual(

-             list(js_data['message']['new_branch']), ['feature'])

-         self.assertEqual(js_data['message']['new_branch']['feature']['commits'], 2)

+         output = self.app.get('/pv/task/' + js_data['task'])

+         self.assertEqual(output.status_code, 200)

+         js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

-             js_data['message']['new_branch']['feature']['target_branch'], 'master')

+             js_data,

+             {'results': {

+                 'branch_w_pr': {},

+                 'new_branch': {'feature':

+                     {'commits': 2, 'target_branch': 'master'}}}}

+         )

  

      def test_get_pull_request_ready_branch_on_fork_no_parent_no_pr(self):

          '''Test the get_pull_request_ready_branch from the internal API on
@@ -2561,18 +2561,19 @@ 

          js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

              sorted(js_data.keys()),

-             ['code', 'message']

+             ['code', 'task']

          )

          self.assertEqual(js_data['code'], 'OK')

+         output = self.app.get('/pv/task/' + js_data['task'])

+         self.assertEqual(output.status_code, 200)

+         js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

-             sorted(js_data['message'].keys()),

-             ['branch_w_pr', 'new_branch'])

-         self.assertEqual(js_data['message']['branch_w_pr'], {})

-         self.assertListEqual(

-             list(js_data['message']['new_branch']), ['feature'])

-         self.assertEqual(js_data['message']['new_branch']['feature']['commits'], 2)

-         self.assertEqual(

-             js_data['message']['new_branch']['feature']['target_branch'], 'master')

+             js_data,

+             {'results': {

+                 'branch_w_pr': {},

+                 'new_branch': {'feature':

+                     {'commits': 2, 'target_branch': 'master'}}}}

+         )

  

      def test_get_pull_request_ready_branch_matching_target_off(self):

          '''Test the get_pull_request_ready_branch from the internal API on
@@ -2625,18 +2626,19 @@ 

          js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

              sorted(js_data.keys()),

-             ['code', 'message']

+             ['code', 'task']

          )

          self.assertEqual(js_data['code'], 'OK')

-         self.assertListEqual(

-             sorted(js_data['message'].keys()),

-             ['branch_w_pr', 'new_branch'])

-         self.assertEqual(js_data['message']['branch_w_pr'], {})

-         self.assertListEqual(

-             list(js_data['message']['new_branch']), ['feature'])

-         self.assertEqual(js_data['message']['new_branch']['feature']['commits'], 2)

+         output = self.app.get('/pv/task/' + js_data['task'])

+         self.assertEqual(output.status_code, 200)

+         js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

-             js_data['message']['new_branch']['feature']['target_branch'], 'master')

+             js_data,

+             {'results': {

+                 'branch_w_pr': {},

+                 'new_branch': {'feature':

+                     {'commits': 2, 'target_branch': 'master'}}}}

+         )

  

      @patch.dict('pagure.config.config', {'PR_TARGET_MATCHING_BRANCH': True})

      def test_get_pull_request_ready_branch_matching_target_on(self):
@@ -2690,18 +2692,19 @@ 

          js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

              sorted(js_data.keys()),

-             ['code', 'message']

+             ['code', 'task']

          )

          self.assertEqual(js_data['code'], 'OK')

-         self.assertListEqual(

-             sorted(js_data['message'].keys()),

-             ['branch_w_pr', 'new_branch'])

-         self.assertEqual(js_data['message']['branch_w_pr'], {})

-         self.assertListEqual(

-             list(js_data['message']['new_branch']), ['feature'])

-         self.assertEqual(js_data['message']['new_branch']['feature']['commits'], 1)

+         output = self.app.get('/pv/task/' + js_data['task'])

+         self.assertEqual(output.status_code, 200)

+         js_data = json.loads(output.get_data(as_text=True))

          self.assertEqual(

-             js_data['message']['new_branch']['feature']['target_branch'], 'feature')

+             js_data,

+             {'results': {

+                 'branch_w_pr': {},

+                 'new_branch': {'feature':

+                     {'commits': 1, 'target_branch': 'feature'}}}}

+         )

  

      def test_task_info_task_running(self):

          """ Test the task_info internal API endpoint when the task isn't

@@ -3115,18 +3115,8 @@ 

              )

              self.assertEqual(output.status_code, 200)

              data = json.loads(output.get_data(as_text=True))

-             self.assertEqual(

-                 data,

-                 {

-                   "code": "OK",

-                   "message": {

-                     "branch_w_pr": {

-                       "feature": "test/pull-request/1"

-                     },

-                     "new_branch": {}

-                   }

-                 }

-             )

+             self.assertEqual(sorted(data.keys()), ['code', 'task'])

+             self.assertEqual(data['code'], 'OK')

  

      @patch('pagure.lib.notify.send_email')

      def test_fork_edit_file(self, send_email):

@@ -70,8 +70,6 @@ 

                            'namespace': None})

  

          self.assertTrue(os.path.exists(os.path.join(

-             self.path, 'repos', 'test.git', 'hooks', 'post-receive.default')))

-         self.assertTrue(os.path.exists(os.path.join(

              self.path, 'repos', 'test.git', 'hooks', 'post-receive')))

  

      def test_plugin_default_remove(self):

@@ -190,9 +190,6 @@ 

  

              self.assertTrue(os.path.exists(os.path.join(

                  self.path, 'repos', 'test.git', 'hooks',

-                 'post-receive.mirror')))

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

                  'post-receive')))

  

      def test_plugin_mirror_deactivate(self):

@@ -182,10 +182,6 @@ 

                  '<input checked class="form-check-input mt-2" id="active" name="active" '

                  'type="checkbox" value="y">', output_text)

  

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

-                 'pre-receive.pagureforcecommit')))

- 

              # De-Activate hook

              data = {'csrf_token': csrf_token}

              output = self.app.post(

@@ -166,9 +166,6 @@ 

  

              self.assertTrue(os.path.exists(os.path.join(

                  self.path, 'repos', 'test.git', 'hooks',

-                 'post-receive.pagure')))

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

                  'post-receive')))

              self.assertTrue(os.path.exists(os.path.join(

                  self.path, 'repos', 'docs', 'test.git', 'hooks',
@@ -241,9 +238,6 @@ 

  

              self.assertTrue(os.path.exists(os.path.join(

                  self.path, 'repos', 'test.git', 'hooks',

-                 'post-receive.pagure')))

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

                  'post-receive')))

              self.assertFalse(os.path.exists(os.path.join(

                  self.path, 'docs', 'test.git', 'hooks',
@@ -279,9 +273,6 @@ 

  

              self.assertTrue(os.path.exists(os.path.join(

                  self.path, 'repos', 'test.git', 'hooks',

-                 'post-receive.pagure')))

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

                  'post-receive')))

              self.assertFalse(os.path.exists(os.path.join(

                  self.path, 'docs', 'test.git', 'hooks',

@@ -137,10 +137,6 @@ 

                  '<input checked class="form-check-input mt-2" id="active" name="active" '

                  'type="checkbox" value="y">', output_text)

  

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

-                 'pre-receive.pagure_no_new_branches')))

- 

              # De-Activate hook

              data = {'csrf_token': self.csrf_token}

              output = self.app.post(

@@ -119,10 +119,6 @@ 

                  '<input checked class="form-check-input mt-2" id="active" name="active" '

                  'type="checkbox" value="y">', output_text)

  

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'requests', 'test.git', 'hooks',

-                 'post-receive.pagure-requests')))

- 

              # De-Activate hook

              data = {'csrf_token': csrf_token}

              output = self.app.post(
@@ -151,16 +147,6 @@ 

                  self.path, 'requests', 'test.git', 'hooks',

                  'post-receive.pagure-requests')))

  

-             # Try re-activate hook w/o the git repo

-             data = {

-                 'csrf_token': csrf_token,

-                 'active': 'y',

-             }

-             shutil.rmtree(os.path.join(self.path, 'repos', 'requests', 'test.git'))

- 

-             output = self.app.post('/test/settings/Pagure requests', data=data)

-             self.assertEqual(output.status_code, 404)

- 

  

  if __name__ == '__main__':

      unittest.main(verbosity=2)

@@ -120,10 +120,6 @@ 

                  '<input checked class="form-check-input mt-2" id="active" name="active" '

                  'type="checkbox" value="y">', output_text)

  

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'tickets', 'test.git', 'hooks',

-                 'post-receive.pagure-ticket')))

- 

              # De-Activate hook

              data = {'csrf_token': csrf_token}

              output = self.app.post(

@@ -107,10 +107,6 @@ 

                  'Hook activated',

                  output_text)

  

-             self.assertTrue(os.path.exists(os.path.join(

-                 self.path, 'repos', 'test.git', 'hooks',

-                 'pre-receive.pagureunsignedcommit')))

- 

              # De-Activate hook

              data = {'csrf_token': csrf_token}

              output = self.app.post(

2 new commits added

  • Adjust the unit-tests for the change in the plugin/git hook architecture
  • Drop import that is no longer needed
5 years ago

18 new commits added

  • Adjust the unit-tests for the change in the plugin/git hook architecture
  • Drop import that is no longer needed
  • Run black on the entire project
  • Bring back the existing git hook files but as symlink to a non-existing file
  • Fix permissions on the hookrunner
  • Port the pagure hook to the new runner architecture
  • Port the default hook to the new runner system
  • Port the requests git hook to the new runner architecture
  • Port the hook blocking force push to the new runner architecture
  • Port the pagure ticket hook to the new runner architecture
  • Port the pagure-ci plugin to the new runner architecture
  • Port the hook blocking new branch creation to the new runner architecture
  • Port the hook to block unsigned commits to the new runner architecture
  • Port the fedmsg plugin to the new runner architecture
  • Port the mirror hook to the new base runner architecture
  • Include the default pagure config if it exists or no other is specified
  • Add an option to add the default hook/plugin in the list of enabled plugins
  • Little doc string changes to hopefully make things clearer
5 years ago

1 new commit added

  • Clear out code that is no longer needed since we don't install hooks anymore
5 years ago

Uncaught failures?

14:17:16 FAILED test: py-test_pagure_flask_ui_plugins_pagure_hook
14:17:16 FAILED test: py-test_pagure_flask_internal
14:17:16 FAILED test: py-test_pagure_flask_ui_fork
14:17:16 FAILED test: py-test_pagure_flask_api_pr_flag
14:17:16 
14:17:16 
14:17:16 Ran 92 tests in 804.121196 seconds, of which 4 failed

1 new commit added

  • Attempt to make test fail on jenkins as well
5 years ago

Pretty please pagure-ci rebuild

20 new commits added

  • Attempt to make test fail on jenkins as well
  • Clear out code that is no longer needed since we don't install hooks anymore
  • Adjust the unit-tests for the change in the plugin/git hook architecture
  • Drop import that is no longer needed
  • Run black on the entire project
  • Bring back the existing git hook files but as symlink to a non-existing file
  • Fix permissions on the hookrunner
  • Port the pagure-ci plugin to the new runner architecture
  • Port the mirror hook to the new base runner architecture
  • Include the default pagure config if it exists or no other is specified
  • Port the fedmsg plugin to the new runner architecture
  • Port the default hook to the new runner system
  • Port the pagure hook to the new runner architecture
  • Port the requests git hook to the new runner architecture
  • Port the hook blocking force push to the new runner architecture
  • Port the pagure ticket hook to the new runner architecture
  • Port the hook blocking new branch creation to the new runner architecture
  • Port the hook to block unsigned commits to the new runner architecture
  • Add an option to add the default hook/plugin in the list of enabled plugins
  • Little doc string changes to hopefully make things clearer
5 years ago

20 new commits added

  • Ensure test fail on jenkins as well by propagating the return code
  • Clear out code that is no longer needed since we don't install hooks anymore
  • Adjust the unit-tests for the change in the plugin/git hook architecture
  • Drop import that is no longer needed
  • Run black on the entire project
  • Bring back the existing git hook files but as symlink to a non-existing file
  • Fix permissions on the hookrunner
  • Port the pagure-ci plugin to the new runner architecture
  • Port the mirror hook to the new base runner architecture
  • Include the default pagure config if it exists or no other is specified
  • Port the fedmsg plugin to the new runner architecture
  • Port the default hook to the new runner system
  • Port the pagure hook to the new runner architecture
  • Port the requests git hook to the new runner architecture
  • Port the hook blocking force push to the new runner architecture
  • Port the pagure ticket hook to the new runner architecture
  • Port the hook blocking new branch creation to the new runner architecture
  • Port the hook to block unsigned commits to the new runner architecture
  • Add an option to add the default hook/plugin in the list of enabled plugins
  • Little doc string changes to hopefully make things clearer
5 years ago

1 new commit added

  • Fix the unit-tests
5 years ago

1 new commit added

  • Let's use all procs on jenkins
5 years ago

22 new commits added

  • Let's use all procs on jenkins
  • Fix the unit-tests
  • Ensure test fail on jenkins as well by propagating the return code
  • Clear out code that is no longer needed since we don't install hooks anymore
  • Adjust the unit-tests for the change in the plugin/git hook architecture
  • Drop import that is no longer needed
  • Run black on the entire project
  • Bring back the existing git hook files but as symlink to a non-existing file
  • Fix permissions on the hookrunner
  • Port the hook blocking new branch creation to the new runner architecture
  • Port the hook to block unsigned commits to the new runner architecture
  • Port the mirror hook to the new base runner architecture
  • Include the default pagure config if it exists or no other is specified
  • Port the pagure-ci plugin to the new runner architecture
  • Port the default hook to the new runner system
  • Port the fedmsg plugin to the new runner architecture
  • Port the pagure hook to the new runner architecture
  • Port the requests git hook to the new runner architecture
  • Port the hook blocking force push to the new runner architecture
  • Port the pagure ticket hook to the new runner architecture
  • Add an option to add the default hook/plugin in the list of enabled plugins
  • Little doc string changes to hopefully make things clearer
5 years ago

Pretty please pagure-ci rebuild

Code looks okay, and confirmed that it fixes my local pagure instance based on git master! :thumbsup:

Commit 2d4374c fixes this pull-request

Pull-Request has been merged by pingou

5 years ago

Pull-Request has been merged by pingou

5 years ago
Metadata
Changes Summary 45
+3 -2
file changed
pagure/hooks/__init__.py
+263 -27
file changed
pagure/hooks/default.py
+10 -1
file changed
pagure/hooks/fedmsg.py
-294
file removed
pagure/hooks/files/default_hook.py
+1
file added
pagure/hooks/files/default_hook.py
+5 -0
file changed
pagure/hooks/files/hookrunner
-63
file removed
pagure/hooks/files/mirror.py
+1
file added
pagure/hooks/files/mirror.py
-76
file removed
pagure/hooks/files/pagure_block_unsigned.py
+1
file added
pagure/hooks/files/pagure_block_unsigned.py
-94
file removed
pagure/hooks/files/pagure_force_commit_hook.py
+1
file added
pagure/hooks/files/pagure_force_commit_hook.py
-239
file removed
pagure/hooks/files/pagure_hook.py
+1
file added
pagure/hooks/files/pagure_hook.py
-98
file removed
pagure/hooks/files/pagure_hook_requests.py
+1
file added
pagure/hooks/files/pagure_hook_requests.py
-79
file removed
pagure/hooks/files/pagure_hook_tickets.py
+1
file added
pagure/hooks/files/pagure_hook_tickets.py
-12
file removed
pagure/hooks/files/pagure_no_new_branches
+1
file added
pagure/hooks/files/pagure_no_new_branches
+30 -1
file changed
pagure/hooks/mirror_hook.py
+11 -1
file changed
pagure/hooks/pagure_ci.py
+42 -33
file changed
pagure/hooks/pagure_force_commit.py
+204 -41
file changed
pagure/hooks/pagure_hook.py
+24 -31
file changed
pagure/hooks/pagure_no_new_branches.py
+47 -59
file changed
pagure/hooks/pagure_request_hook.py
+42 -1
file changed
pagure/hooks/pagure_ticket_hook.py
+48 -31
file changed
pagure/hooks/pagure_unsigned_commits.py
+7 -15
file changed
pagure/internal/__init__.py
+1 -1
file changed
pagure/lib/model.py
+6 -1
file changed
pagure/lib/plugins.py
+2 -1
file changed
pagure/lib/tasks_services.py
+3 -2
file changed
pagure/utils.py
+4 -2
file changed
runtests.py
+4 -4
file changed
tests/test_pagure_flask_api_pr_flag.py
+43 -40
file changed
tests/test_pagure_flask_internal.py
+2 -12
file changed
tests/test_pagure_flask_ui_fork.py
+0 -2
file changed
tests/test_pagure_flask_ui_plugins_default_hook.py
+0 -3
file changed
tests/test_pagure_flask_ui_plugins_mirror.py
+0 -4
file changed
tests/test_pagure_flask_ui_plugins_noff.py
+0 -9
file changed
tests/test_pagure_flask_ui_plugins_pagure_hook.py
+0 -4
file changed
tests/test_pagure_flask_ui_plugins_pagure_no_new_branch.py
+0 -14
file changed
tests/test_pagure_flask_ui_plugins_pagure_request_hook.py
+0 -4
file changed
tests/test_pagure_flask_ui_plugins_pagure_ticket_hook.py
+0 -4
file changed
tests/test_pagure_flask_ui_plugins_unsigned.py