Use PR branch name to link to corresponding redmine issue (#73088)
gitea-wip/gitea-redmine/pipeline/pr-main This commit looks good Details
gitea/gitea-redmine/pipeline/head This commit looks good Details

This commit is contained in:
Agate 2023-01-19 10:10:53 +01:00
parent d9c2b55180
commit 3e6855b459
2 changed files with 30 additions and 13 deletions

View File

@ -56,7 +56,11 @@ def incoming_webhook(token):
return {'status': 'success', 'detail': 'Skipped, unhandled webhook'}
haystack = f'{payload["pull_request"]["body"]} {payload["pull_request"]["title"]}'
issues_ids = get_issues_ids(haystack)
try:
pr_branch = payload['pull_request']['head']['ref']
except KeyError:
pr_branch = ''
issues_ids = get_issues_ids(haystack, pr_branch)
issues = []
for id in issues_ids:
@ -105,11 +109,15 @@ def get_handler(payload):
ISSUE_REGEX = re.compile(r'#(\d+)')
PR_BRANCH_REGEX = re.compile(r'wip/(\d+).*')
def get_issues_ids(text):
"""Extract issues number from their #1234 notation"""
return [int(i) for i in sorted(set(ISSUE_REGEX.findall(text)))]
def get_issues_ids(text, branch_name):
"""Extract issues number from their #1234 notation or the wip/xxx branch name"""
ids = [int(i) for i in ISSUE_REGEX.findall(text)]
ids += [int(i) for i in PR_BRANCH_REGEX.findall(branch_name or '')]
return list(sorted(set(ids)))
def noop(payload):

View File

@ -54,16 +54,20 @@ def test_get_handler(payload, expected_handler, expected_event):
@pytest.mark.parametrize(
'body, expected',
'body, branch_name, expected',
[
('None', []),
('#1, #2', [1, 2]),
('#none, #2', [2]),
('# Header\n, #2', [2]),
('None', '', []),
('#1, #2', '', [1, 2]),
('#none, #2', '', [2]),
('# Header\n, #2', '', [2]),
('# Header', 'wip/2-something', [2]),
('# Header', 'wip/2', [2]),
('# Header', 'wip/noop-2', []),
('#2 #3', 'wip/noop-2', [2, 3]),
],
)
def test_get_issues_ids(body, expected):
assert gitea_redmine.get_issues_ids(body) == expected
def test_get_issues_ids(body, branch_name, expected):
assert gitea_redmine.get_issues_ids(body, branch_name) == expected
@pytest.mark.parametrize(
@ -404,7 +408,8 @@ def test_incoming_webhook_calls_proper_handler(client, mocker):
project.parent = None
issue1 = mocker.Mock(journals=[], project=project)
issue2 = mocker.Mock(journals=[], project=project)
get_redmine_issue = mocker.patch.object(gitea_redmine, 'get_redmine_issue', side_effect=[issue1, issue2])
issue3 = mocker.Mock(journals=[], project=project)
get_redmine_issue = mocker.patch.object(gitea_redmine, 'get_redmine_issue', side_effect=[issue1, issue2, issue3])
get_redmine_project = mocker.patch.object(gitea_redmine, 'get_redmine_project', return_value=project)
get_handler = mocker.patch.object(gitea_redmine, 'get_handler', return_value=[mocker.Mock(), 'foo'])
@ -414,6 +419,9 @@ def test_incoming_webhook_calls_proper_handler(client, mocker):
"pull_request": {
"title": "Fix #1234",
"body": "And #5678",
"head": {
"ref": "wip/9100-something"
}
},
}
@ -425,10 +433,11 @@ def test_incoming_webhook_calls_proper_handler(client, mocker):
get_handler.assert_called_once_with(payload)
handler = get_handler.return_value[0]
assert handler.call_count == 2
assert handler.call_count == 3
get_redmine_issue.assert_any_call(1234)
get_redmine_issue.assert_any_call(5678)
get_redmine_issue.assert_any_call(9100)
handler.assert_any_call(issue1, payload, project)
handler.assert_any_call(issue2, payload, project)