From 2c1a956773d35f373000782dc7b8ed086113f221 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Dec 15 2015 11:14:20 +0000
Subject: [PATCH 1/3] Also allow PRs to be closed by post hooks


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure/hooks/files/pagure_hook.py b/pagure/hooks/files/pagure_hook.py
index d1c6234..eb8fc87 100755
--- a/pagure/hooks/files/pagure_hook.py
+++ b/pagure/hooks/files/pagure_hook.py
@@ -6,9 +6,7 @@ relates to an issue.
 """
 
 import os
-import re
 import sys
-import subprocess
 
 from sqlalchemy.exc import SQLAlchemyError
 
@@ -37,13 +35,15 @@ def generate_revision_change_log(new_commits_list):
         line = line.strip()
 
         print '*', line
-        for issue in pagure.lib.link.get_relation(
+        for relation in pagure.lib.link.get_relation(
                 pagure.SESSION,
                 pagure.lib.git.get_repo_name(abspath),
                 pagure.lib.git.get_username(abspath),
                 line,
-                'fixes'):
-            fixes_commit(commitid, issue, pagure.APP.config.get('APP_URL'))
+                'fixes',
+                include_prs=True):
+            fixes_relation(commitid, relation,
+                           pagure.APP.config.get('APP_URL'))
 
         for issue in pagure.lib.link.get_relation(
                 pagure.SESSION,
@@ -70,7 +70,7 @@ def relates_commit(commitid, issue, app_url=None):
         commitid[:8], url)
 
     try:
-        message = pagure.lib.add_issue_comment(
+        pagure.lib.add_issue_comment(
             pagure.SESSION,
             issue=issue,
             comment=comment,
@@ -85,30 +85,42 @@ def relates_commit(commitid, issue, app_url=None):
         pagure.APP.logger.exception(err)
 
 
-def fixes_commit(commitid, issue, app_url=None):
-    ''' Add a comment to an issue that this commit fixes it and update
+def fixes_relation(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 = '../%s' % commitid[:8]
     if app_url:
         if app_url.endswith('/'):
             app_url = app_url[:-1]
-        project = issue.project.path.split('.git')[0]
-        if issue.project.is_fork:
+        project = relation.project.path.split('.git')[0]
+        if relation.project.is_fork:
             project = 'fork/%s' % project
         url = '%s/%s/%s' % (app_url, project, commitid[:8])
 
-    comment = ''' Commit [%s](%s) fixes this ticket''' % (
-        commitid[:8], url)
+    comment = ''' Commit [%s](%s) fixes this %s''' % (
+        commitid[:8], url, relation.isa)
 
     try:
-        message = pagure.lib.add_issue_comment(
-            pagure.SESSION,
-            issue=issue,
-            comment=comment,
-            user=pagure.lib.git.get_pusher_email(commitid, abspath),
-            ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
-        )
+        if relation.isa == 'issue':
+            pagure.lib.add_issue_comment(
+                pagure.SESSION,
+                issue=relation,
+                comment=comment,
+                user=pagure.lib.git.get_pusher_email(commitid, abspath),
+                ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
+            )
+        elif relation.isa == 'pull-request':
+            pagure.lib.add_pull_request_comment(
+                pagure.SESSION,
+                request=relation,
+                commit=None,
+                filename=None,
+                row=None,
+                comment=comment,
+                user=pagure.lib.git.get_pusher_email(commitid, abspath),
+                requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
+            )
         pagure.SESSION.commit()
     except pagure.exceptions.PagureException as err:
         print err
@@ -124,12 +136,20 @@ def fixes_commit(commitid, issue, app_url=None):
 
     if 'master' in branches:
         try:
-            pagure.lib.edit_issue(
-                pagure.SESSION,
-                issue,
-                ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
-                user=pagure.lib.git.get_pusher_email(commitid, abspath),
-                status='Fixed')
+            if relation.isa == 'issue':
+                pagure.lib.edit_issue(
+                    pagure.SESSION,
+                    relation,
+                    ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
+                    user=pagure.lib.git.get_pusher_email(commitid, abspath),
+                    status='Fixed')
+            elif relation.isa == 'pull-request':
+                pagure.lib.close_pull_request(
+                    pagure.SESSION,
+                    relation,
+                    requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
+                    user=pagure.lib.git.get_pusher_email(commitid, abspath),
+                    merged=True)
             pagure.SESSION.commit()
         except pagure.exceptions.PagureException as err:
             print err
@@ -140,7 +160,6 @@ def fixes_commit(commitid, issue, app_url=None):
 
 def run_as_post_receive_hook():
 
-    changes = []
     for line in sys.stdin:
         if pagure.APP.config.get('HOOK_DEBUG', False):
             print line
diff --git a/pagure/lib/link.py b/pagure/lib/link.py
index 3ca01d3..311c978 100644
--- a/pagure/lib/link.py
+++ b/pagure/lib/link.py
@@ -30,11 +30,14 @@ RELATES = [
 ]
 
 
-def get_relation(session, reponame, username, text, reftype='relates'):
+def get_relation(session, reponame, username, text, reftype='relates',
+                 include_prs=False):
     ''' For a given text, searches using regex if the text contains
     reference to another issue in this project or another one.
 
     Returns the list of issues referenced (possibly empty).
+    If include_prs=True, it may also contain pull requests (may still
+    be empty).
 
     By default it searches for references of type: `relates`, for example:
     ``this commits relates to #2``.
@@ -52,24 +55,30 @@ def get_relation(session, reponame, username, text, reftype='relates'):
     if reftype == 'fixes':
         regex = FIXES
 
-    issues = []
+    relations = []
     for motif in regex:
-        issueid = None
+        relid = None
         project = None
         if motif.match(text):
             if len(motif.match(text).groups()) >= 2:
-                issueid = motif.match(text).group(2)
+                relid = motif.match(text).group(2)
                 project = motif.match(text).group(1)
             else:
-                issueid = motif.match(text).group(1)
+                relid = motif.match(text).group(1)
 
-        if issueid:
-            issue = pagure.lib.search_issues(
-                session, repo=repo, issueid=issueid)
-            if issue is None or issue.project.name not in [project, repo.name]:
+        if relid:
+            relation = pagure.lib.search_issues(
+                session, repo=repo, issueid=relid)
+
+            if relation is None and include_prs:
+                relation = pagure.lib.search_pull_requests(
+                    session, project_id=repo.id, requestid=relid)
+
+            if relation is None or relation.project.name not in [project,
+                                                                 repo.name]:
                 continue
 
-            if issue not in issues:
-                issues.append(issue)
+            if relation not in relations:
+                relations.append(relation)
 
-    return issues
+    return relations

From d04b5624e498476a03876f0b54c046ad46e5f81c Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Dec 15 2015 12:00:42 +0000
Subject: [PATCH 2/3] Use project.fullname instead of .path.split...


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure/hooks/files/pagure_hook.py b/pagure/hooks/files/pagure_hook.py
index eb8fc87..82ae2db 100755
--- a/pagure/hooks/files/pagure_hook.py
+++ b/pagure/hooks/files/pagure_hook.py
@@ -61,7 +61,7 @@ def relates_commit(commitid, issue, app_url=None):
     if app_url:
         if app_url.endswith('/'):
             app_url = app_url[:-1]
-        project = issue.project.path.split('.git')[0]
+        project = issue.project.fullname
         if issue.project.is_fork:
             project = 'fork/%s' % project
         url = '%s/%s/%s' % (app_url, project, commitid[:8])
@@ -93,7 +93,7 @@ def fixes_relation(commitid, relation, app_url=None):
     if app_url:
         if app_url.endswith('/'):
             app_url = app_url[:-1]
-        project = relation.project.path.split('.git')[0]
+        project = relation.project.fullname
         if relation.project.is_fork:
             project = 'fork/%s' % project
         url = '%s/%s/%s' % (app_url, project, commitid[:8])

From 6e1dc7dba5c8d4bf0c68d0af6d96fd6fcb176294 Mon Sep 17 00:00:00 2001
From: Patrick Uiterwijk <puiterwijk@redhat.com>
Date: Dec 15 2015 12:01:25 +0000
Subject: [PATCH 3/3] .path is just .fullname +.git


Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>

---

diff --git a/pagure/lib/model.py b/pagure/lib/model.py
index cff73c0..f95c302 100644
--- a/pagure/lib/model.py
+++ b/pagure/lib/model.py
@@ -311,11 +311,7 @@ class Project(BASE):
     @property
     def path(self):
         ''' Return the name of the git repo on the filesystem. '''
-        if self.parent_id:
-            path = '%s/%s.git' % (self.user.user, self.name)
-        else:
-            path = '%s.git' % (self.name)
-        return path
+        return '%s.git' % self.fullname
 
     @property
     def is_fork(self):

