Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Re-request core review when there is a new commit to the approved PR #220

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 1 commit into from
May 15, 2020
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
62 changes: 47 additions & 15 deletions bedevere/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@

LABEL_PREFIX = "awaiting"


@enum.unique
class Blocker(enum.Enum):
"""What is blocking a pull request from being committed."""

review = f"{LABEL_PREFIX} review"
core_review = f"{LABEL_PREFIX} core review"
changes = f"{LABEL_PREFIX} changes"
Expand Down Expand Up @@ -147,6 +149,26 @@ async def opened_pr(event, gh, *arg, **kwargs):
await stage(gh, issue, Blocker.review)


@router.register("push")
async def new_commit_pushed(event, gh, *arg, **kwargs):
"""If there is a new commit pushed to the PR branch that is in `awaiting merge` state,
move it back to `awaiting core review` stage.
"""
commits = event.data["commits"]
if len(commits) > 0:
# get the latest commit hash
commit_hash = commits[-1]["id"]
pr = await util.get_pr_for_commit(gh, commit_hash)
for label in util.labels(pr):
if label == "awaiting merge":
issue = await util.issue_for_PR(gh, pr)
greeting = "There's a new commit after the PR has been approved."
await request_core_review(
gh, issue, blocker=Blocker.core_review, greeting=greeting
)
break


async def core_dev_reviewers(gh, pull_request_url):
"""Find the reviewers who are core developers."""
# Unfortunately the reviews URL is not contained in a pull request's data.
Expand Down Expand Up @@ -176,8 +198,9 @@ async def new_review(event, gh, *args, **kwargs):
return
else:
# Waiting for a core developer to leave a review.
await stage(gh, await util.issue_for_PR(gh, pull_request),
Blocker.core_review)
await stage(
gh, await util.issue_for_PR(gh, pull_request), Blocker.core_review
)
else:
if state == "approved":
await stage(gh, await util.issue_for_PR(gh, pull_request), Blocker.merge)
Expand All @@ -193,10 +216,11 @@ async def new_review(event, gh, *args, **kwargs):
pr_author = util.user_login(pull_request)
if await util.is_core_dev(gh, pr_author):
comment = CORE_DEV_CHANGES_REQUESTED_MESSAGE.format(
easter_egg=easter_egg)
easter_egg=easter_egg
)
await stage(gh, issue, Blocker.changes)
await gh.post(pull_request["comments_url"], data={"body": comment})
else: # pragma: no cover
else: # pragma: no cover
raise ValueError(f"unexpected review state: {state!r}")


Expand All @@ -212,21 +236,29 @@ async def new_comment(event, gh, *args, **kwargs):
# PR creator didn't request another review.
return
else:
await stage(gh, issue, Blocker.change_review)
pr_url = issue["pull_request"]["url"]
# Using a set comprehension to remove duplicates.
core_devs = ", ".join({"@" + core_dev
async for core_dev in core_dev_reviewers(gh, pr_url)})
if FUN_TRIGGER_PHRASE.lower() in comment_body:
thanks = FUN_THANKS
else:
thanks = BORING_THANKS
comment = ACK.format(greeting=thanks, core_devs=core_devs)
await gh.post(issue["comments_url"], data={"body": comment})
# Re-request reviews from core developers based on the new state of the PR.
reviewers_url = f'{pr_url}/requested_reviewers'
reviewers = [core_dev async for core_dev in core_dev_reviewers(gh, pr_url)]
await gh.post(reviewers_url, data={"reviewers": reviewers})
await request_core_review(
gh, issue, blocker=Blocker.change_review, greeting=thanks
)


async def request_core_review(gh, issue, *, blocker, greeting):
await stage(gh, issue, blocker)
pr_url = issue["pull_request"]["url"]
# Using a set comprehension to remove duplicates.
core_devs = ", ".join(
{"@" + core_dev async for core_dev in core_dev_reviewers(gh, pr_url)}
)

comment = ACK.format(greeting=greeting, core_devs=core_devs)
await gh.post(issue["comments_url"], data={"body": comment})
# Re-request reviews from core developers based on the new state of the PR.
reviewers_url = f"{pr_url}/requested_reviewers"
reviewers = [core_dev async for core_dev in core_dev_reviewers(gh, pr_url)]
await gh.post(reviewers_url, data={"reviewers": reviewers})


@router.register("pull_request", action="closed")
Expand Down
45 changes: 29 additions & 16 deletions bedevere/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

NEWS_NEXT_DIR = "Misc/NEWS.d/next/"


@enum.unique
class StatusState(enum.Enum):
SUCCESS = 'success'
ERROR = 'error'
FAILURE = 'failure'
SUCCESS = "success"
ERROR = "error"
FAILURE = "failure"


def create_status(context, state, *, description=None, target_url=None):
Expand All @@ -19,13 +20,13 @@ def create_status(context, state, *, description=None, target_url=None):
context to avoid repeatedly specifying it throughout a module.
"""
status = {
'context': context,
'state': state.value,
"context": context,
"state": state.value,
}
if description is not None:
status['description'] = description
status["description"] = description
if target_url is not None:
status['target_url'] = target_url
status["target_url"] = target_url

return status

Expand Down Expand Up @@ -65,12 +66,12 @@ async def files_for_PR(gh, pull_request):
files_url = f'{pull_request["url"]}/files'
data = []
async for filedata in gh.getiter(files_url):
data.append({
'file_name': filedata['filename'],
'patch': filedata.get('patch', ''),
})
data.append(
{"file_name": filedata["filename"], "patch": filedata.get("patch", ""),}
)
return data


async def issue_for_PR(gh, pull_request):
"""Get the issue data for a pull request."""
return await gh.getitem(pull_request["issue_url"])
Expand Down Expand Up @@ -106,18 +107,30 @@ def is_news_dir(filename):

def normalize_title(title, body):
"""Normalize the title if it spills over into the PR's body."""
if not (title.endswith('…') and body.startswith('…')):
if not (title.endswith("…") and body.startswith("…")):
return title
else:
# Being paranoid in case \r\n is used.
return title[:-1] + body[1:].partition('\r\n')[0]
return title[:-1] + body[1:].partition("\r\n")[0]


def no_labels(event_data):
if "label" not in event_data:
print("no 'label' key in payload; "
"'unlabeled' event triggered by label deletion?",
file=sys.stderr)
print(
"no 'label' key in payload; "
"'unlabeled' event triggered by label deletion?",
file=sys.stderr,
)
return True
else:
return False


async def get_pr_for_commit(gh, sha):
"""Find the PR containing the specific commit hash."""
prs_for_commit = await gh.getitem(
f"/search/issues?q=type:pr+repo:python/cpython+sha:{sha}"
)
if prs_for_commit["total_count"] > 0: # there should only be one
return prs_for_commit["items"][0]
return None
Loading