Skip to content
Draft
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
4 changes: 2 additions & 2 deletions bedevere/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
from gidgethub import routing
from gidgethub import sansio

from . import backport, gh_issue, close_pr, filepaths, news, stage
from . import backport, conflict, gh_issue, close_pr, filepaths, news, stage

import sentry_sdk

router = routing.Router(backport.router, gh_issue.router, close_pr.router,
filepaths.router, news.router,
stage.router)
stage.router, conflict.router)
cache = cachetools.LRUCache(maxsize=500)

sentry_sdk.init(os.environ.get("SENTRY_DSN"))
Expand Down
84 changes: 84 additions & 0 deletions bedevere/conflict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import asyncio

import gidgethub.routing
from gidgethub.abc import GitHubAPI

router = gidgethub.routing.Router()

MERGE_CONFLICT_LABEL = "merge-conflict"


@router.register("push")
async def new_commit_pushed_to_main(event, gh: GitHubAPI, *arg, **kwargs) -> None:
"""If there is a new commit pushed to main, update all open PRs merge status."""
if event.data["ref"] != "refs/heads/main":
return

Check warning on line 15 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L15

Added line #L15 was not covered by tests

prs_to_add_conflict_label = []
prs_to_remove_conflict_label = []

Check warning on line 18 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L17-L18

Added lines #L17 - L18 were not covered by tests

after = None
while True:
query = """

Check warning on line 22 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L20-L22

Added lines #L20 - L22 were not covered by tests
query ($after: String) {
repository(name: "cpython", owner: "python") {
id
pullRequests(first: 10, after: $after, states: OPEN) {
nodes {
id
mergeable
number
# title
# url
# state
labels(first: 100) {
edges {
node {
name
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
rateLimit {
remaining
}
}
"""
data = await gh.graphql(query, after=after)

Check warning on line 53 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L53

Added line #L53 was not covered by tests
for pr in data["repository"]["pullRequests"]["nodes"]:
has_conflict_label = MERGE_CONFLICT_LABEL in (
edge["node"]["name"] for edge in pr["labels"]["edges"]
)
if pr["mergeable"] == "CONFLICTING":
if not has_conflict_label:
prs_to_add_conflict_label.append(pr)

Check warning on line 60 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L60

Added line #L60 was not covered by tests
else:
if has_conflict_label:
prs_to_remove_conflict_label.append(pr)

Check warning on line 63 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L63

Added line #L63 was not covered by tests

if data["rateLimit"]["remaining"] < 1000:
break
after = data["repository"]["pullRequests"]["pageInfo"]["endCursor"]

Check warning on line 67 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L66-L67

Added lines #L66 - L67 were not covered by tests
if not data["repository"]["pullRequests"]["pageInfo"]["hasNextPage"]:
break

Check warning on line 69 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L69

Added line #L69 was not covered by tests

futs = [
gh.post(
f"https://api.github.com/repos/python/cpython/issues/{pr['number']}/labels",
data={"labels": [MERGE_CONFLICT_LABEL]},
)
for pr in prs_to_add_conflict_label
] + [
gh.delete(
f"https://api.github.com/repos/python/cpython/issues/{pr['number']}/labels/{MERGE_CONFLICT_LABEL}",
)
for pr in prs_to_remove_conflict_label
]
for fut in asyncio.as_completed(futs):
await fut

Check warning on line 84 in bedevere/conflict.py

View check run for this annotation

Codecov / codecov/patch

bedevere/conflict.py#L84

Added line #L84 was not covered by tests