Skip to content

Post a status check for backport PR title. #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion bedevere/backport.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Automatically remove a backport label."""
"""Automatically remove a backport label, and check backport PR validity."""
import functools
import re

import gidgethub.routing

from . import util

create_status = functools.partial(util.create_status, 'bedevere/backport-pr')


router = gidgethub.routing.Router()

Expand All @@ -15,6 +18,8 @@
'[{branch} branch](https://github.com/python/cpython/tree/{branch}).')


BACKPORT_TITLE_DEVGUIDE_URL = "https://devguide.python.org/committing/#backport-pr-title"

async def _copy_over_labels(gh, original_issue, backport_issue):
"""Copy over relevant labels from the original PR to the backport PR."""
label_prefixes = "skip", "type", "sprint"
Expand Down Expand Up @@ -58,3 +63,34 @@ async def manage_labels(event, gh, *args, **kwargs):

backport_issue = await util.issue_for_PR(gh, pull_request)
await _copy_over_labels(gh, original_issue, backport_issue)


@router.register("pull_request", action="opened")
@router.register("pull_request", action="edited")
@router.register("pull_request", action="synchronize")
async def validate_backport_pr(event, gh, *args, **kwargs):
"""Check the PR title for backport pull requests.

If the PR was made against maintenance branch, and the title does not
match the backport PR pattern, then post a failure status.
"""
if event.data["action"] == "edited" and "title" not in event.data["changes"]:
return
pull_request = event.data["pull_request"]
base_branch = pull_request["base"]["ref"]

if base_branch == "master":
return

title = util.normalize_title(pull_request["title"],
pull_request["body"])
title_match = TITLE_RE.match(title)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to worry about PRs that are only to fix a bug in a maintenance branch and has no commit against master? I'm thinking of the case when something was implicitly fixed by something and the need to fix a maintenance release comes up later. In that instance there wouldn't necessarily be a PR to refer to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that PR to the maintenance branch that is not a backport of another PR should be quite rare, and if happens we should be "suspicious" about it. That's why I'm checking for both [X.Y] and (GH-NNNN) here.
But I propose to make this status check optional, so we can still merge it even if the status check failed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. We can see if anyone complains about the failure being a big deal on this cases and adjust accordingly.


if title_match is None:
status = create_status(util.StatusState.FAILURE,
description="Not a valid backport PR title.",
target_url=BACKPORT_TITLE_DEVGUIDE_URL)
else:
status = create_status(util.StatusState.SUCCESS,
description="Valid backport PR title.")
await util.post_status(gh, event, status)
147 changes: 138 additions & 9 deletions tests/test_backport.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, *, getitem=None, delete=None, post=None):
self._post_return = post
self.getitem_url = None
self.delete_url = None
self.post_url = self.post_data = None
self.post_ = []

async def getitem(self, url, url_vars={}):
self.getitem_url = sansio.format_url(url, url_vars)
Expand All @@ -21,8 +21,8 @@ async def delete(self, url, url_vars):
self.delete_url = sansio.format_url(url, url_vars)

async def post(self, url, url_vars={}, *, data):
self.post_url = sansio.format_url(url, url_vars)
self.post_data = data
post_url = sansio.format_url(url, url_vars)
self.post_.append((post_url, data))


async def test_edit_not_title():
Expand All @@ -41,7 +41,14 @@ async def test_edit_not_title():
async def test_missing_branch_in_title():
data = {
'action': 'opened',
'pull_request': {'title': 'Backport this (GH-1234)', 'body': ''},
'pull_request': {
'title': 'Backport this (GH-1234)',
'body': '',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
}
event = sansio.Event(data, event='pull_request',
delivery_id='1')
Expand All @@ -53,7 +60,14 @@ async def test_missing_branch_in_title():
async def test_missing_pr_in_title():
data = {
'action': 'opened',
'pull_request': {'title': '[3.6] Backport this', 'body': ''},
'pull_request': {
'title': '[3.6] Backport this',
'body': '',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
}
event = sansio.Event(data, event='pull_request',
delivery_id='1')
Expand All @@ -71,6 +85,10 @@ async def test_missing_backport_label():
'title': title,
'body': '',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {'issues_url': 'https://api.github.com/issue{/number}'}
}
Expand All @@ -94,6 +112,10 @@ async def test_backport_label_removal_success():
'title': '[3.6] Backport this …',
'body': '…(GH-1234)',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {
'issues_url': 'https://api.github.com/issue{/number}',
Expand All @@ -114,8 +136,9 @@ async def test_backport_label_removal_success():
issue_data = getitem_data['https://api.github.com/issue/1234']
assert gh.delete_url == sansio.format_url(issue_data['labels_url'],
{'name': 'needs backport to 3.6'})
assert gh.post_url == issue_data['comments_url']
message = gh.post_data['body']
post = gh.post_[0]
assert post[0] == issue_data['comments_url']
message = post[1]['body']
assert message == backport.MESSAGE_TEMPLATE.format(branch='3.6', pr='2248')


Expand All @@ -127,6 +150,10 @@ async def test_backport_label_removal_with_leading_space_in_title():
'title': ' [3.6] Backport this (GH-1234)',
'body': '…',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {
'issues_url': 'https://api.github.com/issue{/number}',
Expand Down Expand Up @@ -157,6 +184,10 @@ async def test_backport_label_removal_with_parentheses_in_title():
'title': '[3.6] Backport (0.9.6) this (more bpo-1234) (GH-1234)',
'body': '…',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {
'issues_url': 'https://api.github.com/issue{/number}',
Expand Down Expand Up @@ -187,6 +218,10 @@ async def test_label_copying():
'title': '[3.6] Backport this (GH-1234)',
'body': 'N/A',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {
'issues_url': 'https://api.github.com/issue{/number}',
Expand All @@ -207,5 +242,99 @@ async def test_label_copying():
}
gh = FakeGH(getitem=getitem_data)
await backport.router.dispatch(event, gh)
assert gh.post_url == 'https://api.github.com/issue/1234/labels'
assert {"skip news", "type-enhancement", "sprint"} == frozenset(gh.post_data)
post = gh.post_[0]
assert post[0] == 'https://api.github.com/issue/1234/labels'
assert {'skip news', 'type-enhancement', 'sprint'} == frozenset(post[1])


async def test_valid_backport_pr_title():
title = '[3.6] Backport this (GH-1234)'
data = {
'action': 'opened',
'number': 2248,
'pull_request': {
'title': title,
'body': '',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {'issues_url': 'https://api.github.com/issue{/number}'}
}
event = sansio.Event(data, event='pull_request',
delivery_id='1')
getitem = {
'https://api.github.com/issue/1234':
{'labels': [{'name': 'CLA signed'}]},
'https://api.github.com/issue/2248': {},
}
gh = FakeGH(getitem=getitem)
await backport.router.dispatch(event, gh)
post = gh.post_[0]
assert post[0] == 'https://api.github.com/repos/python/cpython/statuses/somehash'
assert post[1]['context'] == 'bedevere/backport-pr'
assert post[1]['description'] == 'Valid backport PR title.'
assert post[1]['state'] == 'success'


async def test_not_valid_backport_pr_title():
title = 'Fix some typo'
data = {
'action': 'opened',
'number': 2248,
'pull_request': {
'title': title,
'body': '',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': '3.6',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {'issues_url': 'https://api.github.com/issue{/number}'}
}
event = sansio.Event(data, event='pull_request',
delivery_id='1')
getitem = {
'https://api.github.com/issue/1234':
{'labels': [{'name': 'CLA signed'}]},
'https://api.github.com/issue/2248': {},
}
gh = FakeGH(getitem=getitem)
await backport.router.dispatch(event, gh)
post = gh.post_[0]
assert post[0] == 'https://api.github.com/repos/python/cpython/statuses/somehash'
assert post[1]['context'] == 'bedevere/backport-pr'
assert post[1]['description'] == 'Not a valid backport PR title.'
assert post[1]['state'] == 'failure'
assert post[1]['target_url'] == 'https://devguide.python.org/committing/#backport-pr-title'


async def test_backport_pr_status_not_posted_on_master():
title = 'Fix some typo'
data = {
'action': 'opened',
'number': 2248,
'pull_request': {
'title': title,
'body': '',
'issue_url': 'https://api.github.com/issue/2248',
'base': {
'ref': 'master',
},
'statuses_url': 'https://api.github.com/repos/python/cpython/statuses/somehash',
},
'repository': {'issues_url': 'https://api.github.com/issue{/number}'}
}
event = sansio.Event(data, event='pull_request',
delivery_id='1')
getitem = {
'https://api.github.com/issue/1234':
{'labels': [{'name': 'CLA signed'}]},
'https://api.github.com/issue/2248': {},
}
gh = FakeGH(getitem=getitem)
await backport.router.dispatch(event, gh)
assert len(gh.post_) == 0
12 changes: 6 additions & 6 deletions tests/test_filepaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def test_news_only():
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
},
}
event = sansio.Event(event_data, event='pull_request', delivery_id=1)
event = sansio.Event(event_data, event='pull_request', delivery_id='1')
await filepaths.router.dispatch(event, gh)
assert gh.getiter_url == 'https://api.github.com/repos/cpython/python/pulls/1234/files'
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
Expand All @@ -68,7 +68,7 @@ async def test_docs_only():
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
},
}
event = sansio.Event(event_data, event='pull_request', delivery_id=1)
event = sansio.Event(event_data, event='pull_request', delivery_id='1')
await filepaths.router.dispatch(event, gh)
assert gh.getiter_url == 'https://api.github.com/repos/cpython/python/pulls/1234/files'
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
Expand All @@ -93,7 +93,7 @@ async def test_tests_only():
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
},
}
event = sansio.Event(event_data, event='pull_request', delivery_id=1)
event = sansio.Event(event_data, event='pull_request', delivery_id='1')
await filepaths.router.dispatch(event, gh)
assert gh.getiter_url == 'https://api.github.com/repos/cpython/python/pulls/1234/files'
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
Expand All @@ -118,7 +118,7 @@ async def test_docs_and_tests():
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
},
}
event = sansio.Event(event_data, event='pull_request', delivery_id=1)
event = sansio.Event(event_data, event='pull_request', delivery_id='1')
await filepaths.router.dispatch(event, gh)
assert gh.getiter_url == 'https://api.github.com/repos/cpython/python/pulls/1234/files'
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
Expand All @@ -145,7 +145,7 @@ async def test_news_and_tests():
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
},
}
event = sansio.Event(event_data, event='pull_request', delivery_id=1)
event = sansio.Event(event_data, event='pull_request', delivery_id='1')
await filepaths.router.dispatch(event, gh)
assert gh.getiter_url == 'https://api.github.com/repos/cpython/python/pulls/1234/files'
assert gh.getitem_url == 'https://api.github.com/repos/cpython/python/issue/1234'
Expand All @@ -172,7 +172,7 @@ async def test_synchronize():
'issue_url': 'https://api.github.com/repos/cpython/python/issue/1234',
},
}
event = sansio.Event(event_data, event='pull_request', delivery_id=1)
event = sansio.Event(event_data, event='pull_request', delivery_id='1')
await filepaths.router.dispatch(event, gh)
assert gh.getiter_url == 'https://api.github.com/repos/cpython/python/pulls/1234/files'
assert gh.getitem_url is None
Expand Down