Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
driazati committed Jan 21, 2022
1 parent 25c8f4c commit 8289e4f
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 1 deletion.
42 changes: 42 additions & 0 deletions .github/workflows/ready_for_merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# Label PRs that have passed CI and are approved

name: Merge

on:
pull_request_review:

concurrency:
group: Merge-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: "recursive"
- name: Check and mark if PR is ready for merge
env:
SHA: ${{ github.event.pull_request.head.sha || github.event.commit.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eux
python tests/scripts/github_check_pr_is_mergeable.py --sha "$SHA" || echo task failed
75 changes: 75 additions & 0 deletions tests/python/unittest/test_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,81 @@ def run(pr_body, expected_reviewers):
)


def test_pr_is_mergable():
is_mergable_script = REPO_ROOT / "tests" / "scripts" / "github_check_pr_is_mergeable.py"

def run(decision, statuses, mergeable):
# Mock out the response from GitHub's API
data = {
"reviewDecision": decision,
"commits": {
"nodes": [{"commit": {"statusCheckRollup": {"contexts": {"nodes": statuses}}}}]
},
}
proc = subprocess.run(
[str(is_mergable_script), "--pr-json", json.dumps(data)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
if proc.returncode != 0:
raise RuntimeError(f"Process failed:\nstdout:\n{proc.stdout}\n\nstderr:\n{proc.stderr}")

# Find the relevant string in the output
if mergeable:
assert "PR passed CI and is approved, labelling" in proc.stdout
else:
assert "PR is not ready for merge" in proc.stdout

# mergeable should be true iff all statuses are successful and PR is approved
run(decision="CHANGES_REQUESTED", statuses=[], mergeable=False)
run(decision="APPROVED", statuses=[], mergeable=True)
run(
decision="CHANGES_REQUESTED",
statuses=[
{
"context": "abc",
"state": "FAILED",
}
],
mergeable=False,
)
run(
decision="APPROVED",
statuses=[
{
"context": "abc",
"state": "FAILED",
}
],
mergeable=False,
)
run(
decision="APPROVED",
statuses=[
{
"context": "abc",
"state": "SUCCESS",
}
],
mergeable=True,
)
run(
decision="APPROVED",
statuses=[
{
"context": "abc",
"state": "SUCCESS",
},
{
"context": "abc2",
"state": "FAILURE",
},
],
mergeable=False,
)


def test_skip_ci():
skip_ci_script = REPO_ROOT / "tests" / "scripts" / "git_skip_ci.py"

Expand Down
11 changes: 10 additions & 1 deletion tests/scripts/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def headers(self):
}

def graphql(self, query: str) -> Dict[str, Any]:
return self._post("https://api.github.com/graphql", {"query": query})
res = self._post("https://api.github.com/graphql", {"query": compress_query(query)})
if "data" not in res:
raise RuntimeError(f"Error querying GraphQL: {res}")
return res

def _post(self, full_url: str, body: Dict[str, Any]) -> Dict[str, Any]:
print("Requesting POST to", full_url, "with", body)
Expand Down Expand Up @@ -70,6 +73,12 @@ def delete(self, url: str) -> Dict[str, Any]:
return response


def compress_query(query: str) -> str:
query = query.replace("\n", "")
query = re.sub("\s+", " ", query)
return query


def parse_remote(remote: str) -> Tuple[str, str]:
"""
Get a GitHub (user, repo) pair out of a git remote
Expand Down
165 changes: 165 additions & 0 deletions tests/scripts/github_check_pr_is_mergeable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
import json
import argparse
from typing import Any

from git_utils import git, GitHubRepo, parse_remote


_pr_query_fields = """
number
reviewDecision
commits(last:1) {
nodes {
commit {
statusCheckRollup {
contexts(last:100) {
nodes {
... on CheckRun {
conclusion
status
name
checkSuite {
workflowRun {
workflow {
name
}
}
}
}
... on StatusContext {
context
state
}
}
}
}
}
}
}
"""


def pr_query(repo: str, user: str, pr_number: str) -> str:
return f"""
{{
repository(name: "{repo}", owner: "{user}") {{
pullRequest(number: {pr_number}) {{
{_pr_query_fields}
}}
}}
}}"""


def commit_query(repo: str, user: str, sha: str) -> str:
"""
Build the GraphQL query to find a PR linked from a commit along with its
latest build status
"""
return f"""
{{
repository(name: "{repo}", owner: "{user}") {{
object(oid: "{sha}") {{
... on Commit {{
associatedPullRequests(last:1) {{
nodes {{
{_pr_query_fields}
}}
}}
}}
}}
}}
}}"""


def is_pr_ready(data: Any) -> bool:
"""
Returns true if a PR is approved and all of its statuses are SUCCESS
"""
approved = data["reviewDecision"] == "APPROVED"
print("Is approved?", approved)

statuses = data["commits"]["nodes"][0]["commit"]["statusCheckRollup"]["contexts"]["nodes"]
unified_statuses = []
for status in statuses:
if "context" in status:
# Parse non-GHA status
unified_statuses.append((status["context"], status["state"] == "SUCCESS"))
else:
# Parse GitHub Actions item
workflow = status["checkSuite"]["workflowRun"]["workflow"]["name"]
name = f"{workflow} / {status['name']}"
unified_statuses.append((name, status["conclusion"] == "SUCCESS"))

print("Got statuses:", json.dumps(unified_statuses, indent=2))
passed_ci = all(status for name, status in unified_statuses)
return approved and passed_ci


if __name__ == "__main__":
help = "Adds label to PRs that have passed CI and are approved"
parser = argparse.ArgumentParser(description=help)
parser.add_argument("--sha")
parser.add_argument("--pr")
parser.add_argument("--remote", default="origin", help="ssh remote to parse")
parser.add_argument("--label", default="ready-for-merge", help="label to add")
parser.add_argument(
"--pr-json", help="(testing) PR data to use instead of fetching from GitHub"
)
args = parser.parse_args()

remote = git(["config", "--get", f"remote.{args.remote}.url"])
user, repo = parse_remote(remote)

is_testing = args.pr_json is not None
if not is_testing and (args.sha is None and args.pr is None):
print("--sha or --pr must be used outside of testing")
exit(1)

github = GitHubRepo(token=os.environ["GITHUB_TOKEN"], user=user, repo=repo)

if args.pr_json:
pr = json.loads(args.pr_json)
elif args.pr:
pr_number = args.pr.strip()
if not pr_number.isdigit():
print(f"PR number was {pr_number}, cannot fetch from GitHub")
exit(0)
# --pr used (i.e. from Jenkins)
data = github.graphql(pr_query(repo, user, args.pr))
pr = data["data"]["repository"]["pullRequest"]
else:
# --sha used (i.e. from GitHub Actions)
data = github.graphql(commit_query(repo, user, args.sha))
pr = data["data"]["repository"]["object"]["associatedPullRequests"]["nodes"][0]

if is_pr_ready(pr):
print("PR passed CI and is approved, labelling...")
if not is_testing:
github.post(f"issues/{pr['number']}/labels", {"labels": [args.label]})
else:
print("PR is not ready for merge")
if not is_testing:
try:
github.delete(f"issues/{pr['number']}/labels/{args.label}")
except error.HTTPError as e:
print(e)
print("Failed to remove label (it may not have been there at all)")

0 comments on commit 8289e4f

Please sign in to comment.