From e7d4db209554c2a64220a3daa6d42945c852fc8c Mon Sep 17 00:00:00 2001 From: Mike McLean Date: May 05 2016 15:33:28 +0000 Subject: [PATCH 1/4] clean up regexes a bit .*\s* is equivalent to .* which doesn't seem to be intended (?:.*\s+)? will match the empty string, or arbitrary space-separated prefixes --- diff --git a/pagure/lib/link.py b/pagure/lib/link.py index 481818d..ca8ae79 100644 --- a/pagure/lib/link.py +++ b/pagure/lib/link.py @@ -15,21 +15,17 @@ import pagure.exceptions FIXES = [ - re.compile(r'fixe?[sd]?:?\s*?#(\d+)', re.I), - re.compile(r'.*\s*fixe?[sd]?:?\s*?#(\d+)', re.I), - re.compile(r'fixe?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), - re.compile(r'.*\s*?fixe?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), - re.compile(r'merge?[sd]?:?\s*?#(\d+)', re.I), - re.compile(r'.*\s*merge?[sd]?:?\s*?#(\d+)', re.I), - re.compile(r'merge?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), - re.compile(r'.*\s*?merge?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), + re.compile(r'(?:.*\s+)?fixe?[sd]?:?\s*?#(\d+)', re.I), + re.compile(r'(?:.*\s+)?fixe?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), + re.compile(r'(?:.*\s+)?merge?[sd]?:?\s*?#(\d+)', re.I), + re.compile(r'(?:.*\s+)?merge?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), ] RELATES = [ - re.compile(r'.*\s*relate[sd]?:?\s*?(?:to)?\s*?#(\d+)', re.I), - re.compile(r'.*\s*relate[sd]?:?\s?#(\d+)', re.I), + re.compile(r'(?:.*\s+)?relate[sd]?:?\s*?(?:to)?\s*?#(\d+)', re.I), + re.compile(r'(?:.*\s+)?relate[sd]?:?\s?#(\d+)', re.I), re.compile( - r'.*\s*relate[sd]?:?\s*?(?:to)?\s*?https?://.*/(\w+)/issue/(\d+)', + r'(?:.*\s+)?relate[sd]?:?\s*?(?:to)?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), ] From 961e983b08a91523bcdc60299eafe208134583be Mon Sep 17 00:00:00 2001 From: Mike McLean Date: May 05 2016 15:33:28 +0000 Subject: [PATCH 2/4] also accept pull-request urls for fix/merge links --- diff --git a/pagure/lib/link.py b/pagure/lib/link.py index ca8ae79..d5ad312 100644 --- a/pagure/lib/link.py +++ b/pagure/lib/link.py @@ -16,9 +16,9 @@ import pagure.exceptions FIXES = [ re.compile(r'(?:.*\s+)?fixe?[sd]?:?\s*?#(\d+)', re.I), - re.compile(r'(?:.*\s+)?fixe?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), + re.compile(r'(?:.*\s+)?fixe?[sd]?:?\s*?https?://.*/(\w+)/(?:issue|pull-request)/(\d+)', re.I), re.compile(r'(?:.*\s+)?merge?[sd]?:?\s*?#(\d+)', re.I), - re.compile(r'(?:.*\s+)?merge?[sd]?:?\s*?https?://.*/(\w+)/issue/(\d+)', re.I), + re.compile(r'(?:.*\s+)?merge?[sd]?:?\s*?https?://.*/(\w+)/(?:issue|pull-request)/(\d+)', re.I), ] RELATES = [ From fa0f93e427fda24403c35d2967fb9ec457789829 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: May 05 2016 15:33:28 +0000 Subject: [PATCH 3/4] update+expand tests for fixes regex --- diff --git a/tests/test_pagure_lib_link.py b/tests/test_pagure_lib_link.py index f90575c..13082fd 100644 --- a/tests/test_pagure_lib_link.py +++ b/tests/test_pagure_lib_link.py @@ -191,34 +191,65 @@ class PagureLibLinktests(tests.Modeltests): def test_fixes_regex(self): ''' Test the fixes regex present in pagure.lib.link. ''' - text = 'fixes http://localhost/fork/pingou/test/issue/1' - for index, regex in enumerate(pagure.lib.link.FIXES): - if index in [2, 3]: - self.assertNotEqual(regex.match(text), None) - else: - self.assertEqual(regex.match(text), None) - - text = 'fix http://209.132.184.222/fork/pingou/test/issue/1' - for index, regex in enumerate(pagure.lib.link.FIXES): - if index in [2, 3]: - self.assertNotEqual(regex.match(text), None) - else: - self.assertEqual(regex.match(text), None) - text = 'This fixed #5' - for index, regex in enumerate(pagure.lib.link.FIXES): - if index == 1: - self.assertNotEqual(regex.match(text), None) - else: - self.assertEqual(regex.match(text), None) - - text = 'Could this be fixes '\ - ' https://fedorahosted.org/pagure/tests2/issue/6' - for index, regex in enumerate(pagure.lib.link.FIXES): - if index == 3: - self.assertNotEqual(regex.match(text), None) - else: - self.assertEqual(regex.match(text), None) + # project/issue matches + def project_match(text, groups): + match = None + for regex in pagure.lib.link.FIXES: + match = regex.match(text) + if match: + break + self.assertNotEqual(match, None) + self.assertEqual(len(match.groups()), 2) + self.assertEqual(match.groups(), groups) + + data = [ + # [string, groups] + ] + + project_match('fixes http://localhost/fork/pingou/test/issue/1', + ('test', '1')) + project_match('fix http://209.132.184.222/fork/pingou/test/issue/1', + ('test', '1')) + project_match('Could this be fixes ' + ' https://fedorahosted.org/pagure/tests2/issue/6', + ('tests2', '6')) + project_match('merged https://pagure.io/myproject/pull-request/70', + ('myproject', '70')) + project_match('Now we merge https://pagure.io/myproject/pull-request/99', + ('myproject', '99')) + + # issue matches + def issue_match(text, issue): + match = None + for regex in pagure.lib.link.FIXES: + match = regex.match(text) + if match: + break + self.assertNotEqual(match, None) + self.assertEqual(len(match.groups()), 1) + self.assertEqual(match.group(1), issue) + + issue_match('This fixed #5', '5') + issue_match('Merged #17', '17') + issue_match('Fixed: #23', '23') + issue_match('This commit fixes: #42', '42') + issue_match('Merge #137', '137') + + # no match + def no_match(text): + match = None + for regex in pagure.lib.link.FIXES: + match = regex.match(text) + if match: + break + self.assertEqual(match, None) + + no_match('nowhitespacemerge: #47') + no_match('This commit unmerges #45') + no_match('Fixed 45 typos') + no_match('Fixed 4 typos') + no_match("Merge branch 'work'") if __name__ == '__main__': From e7d05bcfa2fee586aa09cbe7be343507fba2e128 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: May 05 2016 15:33:28 +0000 Subject: [PATCH 4/4] Update pagure_hook description --- diff --git a/pagure/hooks/pagure_hook.py b/pagure/hooks/pagure_hook.py index f3ee149..c04de9b 100644 --- a/pagure/hooks/pagure_hook.py +++ b/pagure/hooks/pagure_hook.py @@ -57,23 +57,36 @@ class PagureForm(wtf.Form): DESCRIPTION = ''' -Pagure specific hook to add a comment to issues if the pushed commits fix them +Pagure specific hook to add a comment to issues or pull requests if the pushed +commits fix them or relate to them. This is determined based on the commit message. -To reference an issue you need to use one of recognized keywords followed by an -issue number. The number can optionally be preceded by `#` symbol. +To reference an issue/PR you need to use one of recognized keywords followed by +a reference to the issue or PR, separated by whitespace and and optional colon. +Such references can be either: + + * The issue/PR number preceded by the `#` symbol + * The full URL of the issue or PR + +If using the full URL, it is possible to reference issues in other projects. + +The recognized keywords are: + + * fix/fixed/fixes + * relate/related/relates + * merge/merges/merged + +Examples: + + * Fixes #21 + * related: https://pagure.io/myproject/issue/32 + * this commit merges #74 + * Merged: https://pagure.io/myproject/pull-request/74 + Capitalization does not matter; neither does the colon between keyword and number. - * fix - * fixed - * fixes - * relate - * related - * relates -Instead of an issue number, you can use full URL of the issue. This way it is -possible to reference issues in other projects. '''