Skip to content

Commit aaa724e

Browse files
committed
PoC for labelling merge conflicts
1 parent b12ebac commit aaa724e

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

bedevere/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
from gidgethub import routing
1212
from gidgethub import sansio
1313

14-
from . import backport, gh_issue, close_pr, filepaths, news, stage
14+
from . import backport, conflict, gh_issue, close_pr, filepaths, news, stage
1515

1616
import sentry_sdk
1717

1818
router = routing.Router(backport.router, gh_issue.router, close_pr.router,
1919
filepaths.router, news.router,
20-
stage.router)
20+
stage.router, conflict.router)
2121
cache = cachetools.LRUCache(maxsize=500)
2222

2323
sentry_sdk.init(os.environ.get("SENTRY_DSN"))

bedevere/conflict.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from gidgethub.abc import GitHubAPI
2+
import gidgethub.routing
3+
4+
5+
router = gidgethub.routing.Router()
6+
7+
MERGE_CONFLICT_LABEL = "merge-conflict"
8+
9+
10+
@router.register("push")
11+
async def new_commit_pushed_to_main(event, gh: GitHubAPI, *arg, **kwargs) -> None:
12+
"""If there is a new commit pushed to main, update all open PRs merge status."""
13+
if event.data["ref"] != "refs/heads/main":
14+
return
15+
16+
prs_to_add_conflict_label = []
17+
prs_to_remove_conflict_label = []
18+
19+
after = None
20+
while True:
21+
query = """
22+
query ($after: String) {
23+
repository(name: "cpython", owner: "python") {
24+
id
25+
pullRequests(first: 10, after: $after, states: OPEN) {
26+
nodes {
27+
id
28+
mergeable
29+
number
30+
# title
31+
# url
32+
# state
33+
labels(first: 100) {
34+
edges {
35+
node {
36+
name
37+
}
38+
}
39+
}
40+
}
41+
pageInfo {
42+
endCursor
43+
hasNextPage
44+
}
45+
}
46+
}
47+
rateLimit {
48+
remaining
49+
}
50+
}
51+
"""
52+
data = await gh.graphql(query, after=after)
53+
for pr in data["repository"]["pullRequests"]["nodes"]:
54+
has_conflict_label = MERGE_CONFLICT_LABEL in (
55+
edge["node"]["name"] for edge in pr["labels"]["edges"]
56+
)
57+
if pr["mergeable"] == "CONFLICTING":
58+
if not has_conflict_label:
59+
prs_to_add_conflict_label.append(pr)
60+
else:
61+
if has_conflict_label:
62+
prs_to_remove_conflict_label.append(pr)
63+
64+
if data["rateLimit"]["remaining"] < 1000:
65+
break
66+
after = data["repository"]["pullRequests"]["pageInfo"]["endCursor"]
67+
if not data["repository"]["pullRequests"]["pageInfo"]["hasNextPage"]:
68+
break
69+
70+
for pr in prs_to_add_conflict_label:
71+
number = pr["number"]
72+
await gh.post(
73+
f"https://api.github.com/repos/python/cpython/issues/{number}/labels",
74+
data={"labels": [MERGE_CONFLICT_LABEL]},
75+
)
76+
for pr in prs_to_remove_conflict_label:
77+
number = pr["number"]
78+
await gh.delete(
79+
f"https://api.github.com/repos/python/cpython/issues/{number}/labels/{MERGE_CONFLICT_LABEL}",
80+
)

0 commit comments

Comments
 (0)