Skip to content

Commit

Permalink
fix for #4
Browse files Browse the repository at this point in the history
  • Loading branch information
cisaacstern committed Aug 1, 2023
1 parent 29cd273 commit 6eab39f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
27 changes: 16 additions & 11 deletions action/deploy_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def main():
# injected by github actions
repository = os.environ["GITHUB_REPOSITORY"] # will this fail for external prs?
api_url = os.environ["GITHUB_API_URL"]
ref = os.environ["GITHUB_HEAD_REF"]
head_ref = os.environ["GITHUB_HEAD_REF"]
sha = os.environ["GITHUB_SHA"]
repository_id = os.environ["GITHUB_REPOSITORY_ID"]
run_id = os.environ["GITHUB_RUN_ID"]
run_attempt = os.environ["GITHUB_RUN_ATTEMPT"]
Expand All @@ -44,24 +45,28 @@ def main():

# log variables to stdout
print(f"{conda_env = }")
print(f"{ref = }")
print(f"{head_ref = }")
print(f"{sha = }")
print(f"{config = }")

labels = []
if select_recipe_by_label:
# iterate through PRs on the repo. if the PR's head ref is the same
# as our ``ref`` here, get the labels from that PR, and stop iteration.
# FIXME: what if this is a push event, and not a pull_request event?
pulls_url = "/".join([api_url, "repos", repository, "pulls"])
print(f"Fetching pulls from {pulls_url}")
# `head_ref` is available for `pull_request` events. on a `push` event, the `head_ref`
# environment variable will be empty, so in that case we use `sha` to find the PR instead.
commit_sha = head_ref if head_ref else sha
# querying the following endpoint should give us the PR containing the desired labels,
# regardless of whether this is a `pull_request` or `push` event. see official docs here:
# https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#list-pull-requests-associated-with-a-commit
pulls_url = "/".join([api_url, "repos", repository, "commits", commit_sha, "pulls"])
headers = {"X-GitHub-Api-Version": "2022-11-28", "Accept": "application/vnd.github+json"}

pulls_response = requests.get(pulls_url)
print(f"Fetching pulls from {pulls_url}")
pulls_response = requests.get(pulls_url, headers=headers)
pulls_response.raise_for_status()
pulls = pulls_response.json()
assert len(pulls) == 1 # pretty sure this is always true, but just making sure
labels: list[str] = [label["name"] for label in pulls[0]["labels"]]

for p in pulls:
if p["head"]["ref"] == ref:
labels: list[str] = [label["name"] for label in p["labels"]]
recipe_ids = [l.replace("run:", "") for l in labels if l.startswith("run:")]

# dynamically install extra deps if requested.
Expand Down
43 changes: 29 additions & 14 deletions action/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@
from deploy_recipe import main


@pytest.fixture(
params=[
dict(INPUT_SELECT_RECIPE_BY_LABEL=""),
dict(INPUT_SELECT_RECIPE_BY_LABEL="true"),
]
)
def env(request):
@pytest.fixture(params=["", "abcdefg"])
def head_ref(request):
return request.param


@pytest.fixture(params=["", "true"])
def select_recipe_by_label(request):
return request.param


@pytest.fixture
def env(select_recipe_by_label, head_ref):
return {
"CONDA_ENV": "notebook",
"GITHUB_REPOSITORY": "my/repo",
"GITHUB_API_URL": "https://api.github.com",
"GITHUB_HEAD_REF": "abcdefg",
# fixturing of `head_ref` reflects that on `push`
# events, `GITHUB_HEAD_REF` env var is empty
"GITHUB_HEAD_REF": head_ref,
"GITHUB_SHA": "gfedcba",
"GITHUB_REPOSITORY_ID": "1234567890",
"GITHUB_RUN_ID": "0987654321",
"GITHUB_RUN_ATTEMPT": "1",
# TODO: parametrize runner config with `BaseCommand.feedstock-subdir`
"INPUT_PANGEO_FORGE_RUNNER_CONFIG": '{"a": "b"}',
"INPUT_SELECT_RECIPE_BY_LABEL": request.param["INPUT_SELECT_RECIPE_BY_LABEL"],
"INPUT_SELECT_RECIPE_BY_LABEL": select_recipe_by_label,
}


@pytest.fixture
def requests_get_returns_json():
return [
{"labels": [{"name": "run:my-recipe"}], "head":{"ref": "abcdefg"}}
{"labels": [{"name": "run:my-recipe"}]}
]


Expand Down Expand Up @@ -109,11 +117,18 @@ def test_main(
assert "pip" not in call

listdir.assert_called_once()

if env["INPUT_SELECT_RECIPE_BY_LABEL"]:
# requests.get is always called if INPUT_SELECT_RECIPE_BY_LABEL=true
# (to check github api for PR `run:...` labels)
requests_get.assert_called()
# requests.get is always called if INPUT_SELECT_RECIPE_BY_LABEL=true (to check github
# api for PR `run:...` labels). if this is a `pull_request` event, the called gh api
# url should contain the `GITHUB_HEAD_REF` for that PR. if this is a `push` event
# (reflected in the env by the fact that `GITHUB_HEAD_REF` is empty), then we expect to
# be calling an api url for the `GITHUB_SHA` associated with the `push`.
called_gh_api_url = requests_get.call_args[0][0]
if env["GITHUB_HEAD_REF"]:
assert env["GITHUB_HEAD_REF"] in called_gh_api_url
else:
assert env["GITHUB_SHA"] in called_gh_api_url

run_labels = [
label["name"].split("run:")[-1]
Expand Down

0 comments on commit 6eab39f

Please sign in to comment.