Skip to content

Commit

Permalink
PoC for labelling merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jan 6, 2023
1 parent b12ebac commit aaa724e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
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
80 changes: 80 additions & 0 deletions bedevere/conflict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from gidgethub.abc import GitHubAPI
import gidgethub.routing


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

prs_to_add_conflict_label = []
prs_to_remove_conflict_label = []

after = None
while True:
query = """
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)
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)
else:
if has_conflict_label:
prs_to_remove_conflict_label.append(pr)

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

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

0 comments on commit aaa724e

Please sign in to comment.