Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #234: GitHub action api verification #246

Merged
merged 20 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/ci-pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
name: ci-pull-request
on:
pull_request:
types: [opened, reopened, synchronize, closed]
branches:
- main
- master
push:
branches:
- main
- master

jobs:
ci-pull-request:
if: ${{ github.event.action != 'closed' || github.event.pull_request.merged == true }}
robnagler marked this conversation as resolved.
Show resolved Hide resolved
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
37 changes: 37 additions & 0 deletions pykern/pkcli/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,43 @@ def restore(git_txz):
_shell(["git", "checkout"])


def verify_head_passed_ci(repo):
import requests

branch = "master" # TODO(rorour) find a way to set master/main
owner = "radiasoft" # TODO(rorour) remove var

def _run_on_repo(*cmd):
return (
subprocess.check_output(
["git", "-C", os.environ["HOME"] + f"/src/{owner}/{repo}", *cmd],
stderr=subprocess.STDOUT,
)
.decode("utf-8")
.rstrip("\n")
)

r = requests.get(
f"{_GITHUB_API}/repos/{owner}/{repo}/actions/runs?per_page=1&page=1&branch={branch}",
headers={
"Accept": "application/vnd.github+json",
"Authorization": "token " + os.environ["GITHUB_PAT"],
},
)
assert r.json().get("workflow_runs"), f"no workflow runs for branch {branch}"
rorour marked this conversation as resolved.
Show resolved Hide resolved
api_head_sha = r.json().get("workflow_runs")[0].get("head_sha")
_run_on_repo("checkout", branch)
rorour marked this conversation as resolved.
Show resolved Hide resolved
_run_on_repo("pull")
origin_head_sha = _run_on_repo("rev-parse", f"origin/{branch}")
assert (
api_head_sha == origin_head_sha
), f"most recent run on {branch}={api_head_sha} does not match {origin_head_sha}"
c = r.json().get("workflow_runs")[0].get("conclusion")
e = "success"
assert c == e, f"conclusion={c} does not match expected={e}"
pkdlog(f"HEAD from {branch} passed ci (sha {api_head_sha})")


class _GitHub(object):
def __init__(self):
self._github = None
Expand Down