Skip to content

Commit

Permalink
🤖 Format code using black (#106)
Browse files Browse the repository at this point in the history
Closes #105
(I'm a bot 🤖)
  • Loading branch information
miss-islington authored and Mariatta committed Jun 16, 2018
1 parent 92ae45c commit 9ee5393
Show file tree
Hide file tree
Showing 10 changed files with 824 additions and 943 deletions.
12 changes: 5 additions & 7 deletions miss_islington/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
from . import delete_branch
from . import status_change

router = routing.Router(backport_pr.router,
delete_branch.router,
status_change.router)
router = routing.Router(backport_pr.router, delete_branch.router, status_change.router)

cache = cachetools.LRUCache(maxsize=500)

Expand All @@ -29,14 +27,14 @@ async def main(request):

secret = os.environ.get("GH_SECRET")
event = sansio.Event.from_http(request.headers, body, secret=secret)
print('GH delivery ID', event.delivery_id, file=sys.stderr)
print("GH delivery ID", event.delivery_id, file=sys.stderr)
if event.event == "ping":
return web.Response(status=200)
oauth_token = os.environ.get("GH_AUTH")
async with aiohttp.ClientSession() as session:
gh = gh_aiohttp.GitHubAPI(session, "python/cpython",
oauth_token=oauth_token,
cache=cache)
gh = gh_aiohttp.GitHubAPI(
session, "python/cpython", oauth_token=oauth_token, cache=cache
)
# Give GitHub some time to reach internal consistency.
await asyncio.sleep(1)
await router.dispatch(event, gh)
Expand Down
48 changes: 26 additions & 22 deletions miss_islington/backport_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
async def backport_pr(event, gh, *args, **kwargs):
if event.data["pull_request"]["merged"]:

issue_number = event.data['pull_request']['number']
merged_by = event.data['pull_request']['merged_by']['login']
created_by = event.data['pull_request']['user']['login']
issue_number = event.data["pull_request"]["number"]
merged_by = event.data["pull_request"]["merged_by"]["login"]
created_by = event.data["pull_request"]["user"]["login"]

commit_hash = event.data['pull_request']['merge_commit_sha']
commit_hash = event.data["pull_request"]["merge_commit_sha"]

pr_labels = []
if event.data['action'] == 'labeled':
if event.data["action"] == "labeled":
pr_labels = [event.data["label"]]
else:
gh_issue = await gh.getitem(event.data['repository']['issues_url'],
{'number': f"{event.data['pull_request']['number']}"})
pr_labels = await gh.getitem(gh_issue['labels_url'])
gh_issue = await gh.getitem(
event.data["repository"]["issues_url"],
{"number": f"{event.data['pull_request']['number']}"},
)
pr_labels = await gh.getitem(gh_issue["labels_url"])

branches = [label['name'].split()[-1]
for label in pr_labels
if label['name'].startswith("needs backport to")]
branches = [
label["name"].split()[-1]
for label in pr_labels
if label["name"].startswith("needs backport to")
]

if branches:
easter_egg = ""
Expand All @@ -42,19 +46,19 @@ async def backport_pr(event, gh, *args, **kwargs):
thanks_to = f"Thanks @{created_by} for the PR 🌮🎉."
else:
thanks_to = f"Thanks @{created_by} for the PR, and @{merged_by} for merging it 🌮🎉."
message = f"{thanks_to}. I'm working now to backport this PR to: {', '.join(branches)}."\
f"\n🐍🍒⛏🤖 {easter_egg}"
message = f"{thanks_to}. I'm working now to backport this PR to: {', '.join(branches)}." f"\n🐍🍒⛏🤖 {easter_egg}"

await util.leave_comment(gh, issue_number, message)

sorted_branches = sorted(branches,
reverse=True,
key=lambda v: tuple(map(int, v.split('.'))))
sorted_branches = sorted(
branches, reverse=True, key=lambda v: tuple(map(int, v.split(".")))
)

for branch in sorted_branches:
tasks.backport_task.delay(commit_hash,
branch,
issue_number=issue_number,
created_by=created_by,
merged_by=merged_by)

tasks.backport_task.delay(
commit_hash,
branch,
issue_number=issue_number,
created_by=created_by,
merged_by=merged_by,
)
3 changes: 1 addition & 2 deletions miss_islington/delete_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ async def delete_branch(event, gh, *args, **kwargs):
Delete the branch once miss-islington's PR is closed.
"""
if event.data["pull_request"]["user"]["login"] == "miss-islington":
branch_name = event.data['pull_request']['head']['ref']
branch_name = event.data["pull_request"]["head"]["ref"]
url = f"/repos/miss-islington/cpython/git/refs/heads/{branch_name}"
await gh.delete(url)

71 changes: 40 additions & 31 deletions miss_islington/status_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@

router = routing.Router()

TITLE_RE = re.compile(r'\[(?P<branch>\d+\.\d+)\].+?(?P<pr>\d+)\)')
TITLE_RE = re.compile(r"\[(?P<branch>\d+\.\d+)\].+?(?P<pr>\d+)\)")


@router.register("status")
async def check_status(event, gh, *args, **kwargs):
"""
Check the state change
"""
if event.data["commit"].get("committer") \
and event.data["commit"]["committer"]["login"] == "miss-islington":
if (
event.data["commit"].get("committer")
and event.data["commit"]["committer"]["login"] == "miss-islington"
):
sha = event.data["sha"]
await check_ci_status_and_approval(gh, sha, leave_comment=True)

Expand All @@ -32,45 +34,53 @@ async def pr_reviewed(event, gh, *args, **kwargs):

async def check_ci_status_and_approval(gh, sha, leave_comment=False):

result = await gh.getitem(f'/repos/python/cpython/commits/{sha}/status')
result = await gh.getitem(f"/repos/python/cpython/commits/{sha}/status")
all_ci_status = [status["state"] for status in result["statuses"]]
all_ci_context = [status["context"] for status in result["statuses"]]

if "pending" not in all_ci_status \
and "continuous-integration/travis-ci/pr" in all_ci_context:
async for ref in gh.getiter('/repos/miss-islington/cpython/git/refs/heads/'):
if (
"pending" not in all_ci_status
and "continuous-integration/travis-ci/pr" in all_ci_context
):
async for ref in gh.getiter("/repos/miss-islington/cpython/git/refs/heads/"):
if "backport-" in ref["ref"] and ref["object"]["sha"] == sha:
backport_branch_name = ref["ref"].split("/")[-1]
async for pr_response in gh.getiter(f'/repos/python/cpython/pulls?state=open&head=miss-islington:{backport_branch_name}'):
async for pr_response in gh.getiter(
f"/repos/python/cpython/pulls?state=open&head=miss-islington:{backport_branch_name}"
):
pr_number = pr_response["number"]
normalized_pr_title = util.normalize_title(
pr_response["title"],
pr_response["body"])
pr_response["title"], pr_response["body"]
)

title_match = TITLE_RE.match(normalized_pr_title)
if title_match:

if leave_comment:
original_pr_number = title_match.group('pr')
original_pr_url = f"/repos/python/cpython/pulls/{original_pr_number}"
original_pr_number = title_match.group("pr")
original_pr_url = (
f"/repos/python/cpython/pulls/{original_pr_number}"
)
original_pr_result = await gh.getitem(original_pr_url)
pr_author = original_pr_result["user"]["login"]
committer = original_pr_result["merged_by"]["login"]

participants = util.get_participants(
pr_author, committer)
emoji = "✅" if result['state'] == "success" else "❌"
participants = util.get_participants(pr_author, committer)
emoji = "✅" if result["state"] == "success" else "❌"

await util.leave_comment(gh,
await util.leave_comment(
gh,
pr_number=pr_number,
message=f"{participants}: Backport status check is done, and it's a {result['state']} {emoji} .")
message=f"{participants}: Backport status check is done, and it's a {result['state']} {emoji} .",
)

if result['state'] == "success":
async for review in gh.getiter(f"/repos/python/cpython/pulls/{pr_number}/reviews"):
if result["state"] == "success":
async for review in gh.getiter(
f"/repos/python/cpython/pulls/{pr_number}/reviews"
):
reviewer = review["user"]["login"]
approved = review["state"].lower() == "approved"
if approved \
and await util.is_core_dev(gh, reviewer):
if approved and await util.is_core_dev(gh, reviewer):
await merge_pr(gh, pr_number, sha)
break

Expand All @@ -81,14 +91,13 @@ async def merge_pr(gh, pr_number, sha):
pr_commit_msg = commit["commit"]["message"].split("\n")

cleaned_up_title = f"{pr_commit_msg[0]}"
await gh.put(f"/repos/python/cpython/pulls/{pr_number}/merge",
data={"commit_title": cleaned_up_title,
"commit_message": "\n".join(pr_commit_msg[1:]),
"sha": sha,
"merge_method": "squash"
}
)
await gh.put(
f"/repos/python/cpython/pulls/{pr_number}/merge",
data={
"commit_title": cleaned_up_title,
"commit_message": "\n".join(pr_commit_msg[1:]),
"sha": sha,
"merge_method": "squash",
},
)
break



61 changes: 37 additions & 24 deletions miss_islington/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@

from . import util

app = celery.Celery('backport_cpython')
app = celery.Celery("backport_cpython")

app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])
app.conf.update(
BROKER_URL=os.environ["REDIS_URL"], CELERY_RESULT_BACKEND=os.environ["REDIS_URL"]
)


@app.task(rate_limit="1/m")
def setup_cpython_repo():
print("Setting up CPython repository")
if "cpython" not in os.listdir('.'):
if "cpython" not in os.listdir("."):
subprocess.check_output(
f"git clone https://{os.environ.get('GH_AUTH')}:x-oauth-basic@github.com/miss-islington/cpython.git".split())
subprocess.check_output("git config --global user.email 'mariatta.wijaya+miss-islington@gmail.com'".split())
subprocess.check_output(["git", "config", "--global", "user.name", "'Miss Islington (bot)'"])
os.chdir('./cpython')
f"git clone https://{os.environ.get('GH_AUTH')}:x-oauth-basic@github.com/miss-islington/cpython.git".split()
)
subprocess.check_output(
f"git remote add upstream https://{os.environ.get('GH_AUTH')}:x-oauth-basic@github.com/python/cpython.git".split())
"git config --global user.email 'mariatta.wijaya+miss-islington@gmail.com'".split()
)
subprocess.check_output(
["git", "config", "--global", "user.name", "'Miss Islington (bot)'"]
)
os.chdir("./cpython")
subprocess.check_output(
f"git remote add upstream https://{os.environ.get('GH_AUTH')}:x-oauth-basic@github.com/python/cpython.git".split()
)
print("Finished setting up CPython Repo")
else:
print("cpython directory already exists")
Expand All @@ -35,49 +42,55 @@ def backport_task(commit_hash, branch, *, issue_number, created_by, merged_by):
"""Backport a commit into a branch."""
if not util.is_cpython_repo():
# cd to cpython if we're not already in it
if "cpython" in os.listdir('.'):
os.chdir('./cpython')
if "cpython" in os.listdir("."):
os.chdir("./cpython")
else:
print(f"pwd: {os.getcwd()}, listdir: {os.listdir('.')}")
util.comment_on_pr(issue_number,
f"""{util.get_participants(created_by, merged_by)}, Something is wrong... I can't backport for now.
util.comment_on_pr(
issue_number,
f"""{util.get_participants(created_by, merged_by)}, Something is wrong... I can't backport for now.
Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""")
""",
)
util.assign_pr_to_core_dev(issue_number, merged_by)
cp = cherry_picker.CherryPicker('origin', commit_hash, [branch],
prefix_commit=False)
cp = cherry_picker.CherryPicker(
"origin", commit_hash, [branch], prefix_commit=False
)
try:
cp.backport()
except cherry_picker.BranchCheckoutException:
util.comment_on_pr(issue_number,
f"""Sorry {util.get_participants(created_by, merged_by)}, I had trouble checking out the `{branch}` backport branch.
util.comment_on_pr(
issue_number,
f"""Sorry {util.get_participants(created_by, merged_by)}, I had trouble checking out the `{branch}` backport branch.
Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""")
""",
)
util.assign_pr_to_core_dev(issue_number, merged_by)
cp.abort_cherry_pick()
except cherry_picker.CherryPickException:
util.comment_on_pr(issue_number,
f"""Sorry, {util.get_participants(created_by, merged_by)}, I could not cleanly backport this to `{branch}` due to a conflict.
util.comment_on_pr(
issue_number,
f"""Sorry, {util.get_participants(created_by, merged_by)}, I could not cleanly backport this to `{branch}` due to a conflict.
Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""")
""",
)
util.assign_pr_to_core_dev(issue_number, merged_by)
cp.abort_cherry_pick()


class InitRepoStep(bootsteps.StartStopStep):

def start(self, c):
print("Initialize the repository.")
setup_cpython_repo()


app.steps['worker'].add(InitRepoStep)
app.steps["worker"].add(InitRepoStep)
Loading

0 comments on commit 9ee5393

Please sign in to comment.