-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b12ebac
commit aaa724e
Showing
2 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}", | ||
) |