Skip to content

Commit

Permalink
fix(bitrise): normalize refs in bitrise payload builder
Browse files Browse the repository at this point in the history
Bitrise assumes that the short version of the ref gets passed in. It
looks like many of our repos pass the long form in as parameters. Make
mozilla-taskgraph handle this for them.
  • Loading branch information
ahal committed Apr 2, 2024
1 parent a4d6b3d commit c86ac64
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/mozilla_taskgraph/worker_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,32 @@ def build_bitrise_payload(config, task, task_def):
build_params.setdefault("commit_hash", config.params["head_rev"])
build_params.setdefault("branch_repo_owner", config.params["head_repository"])

if config.params["head_ref"]:
build_params.setdefault("branch", config.params["head_ref"])
def normref(ref, type="heads"):
if ref:
prefix = f"refs/{type}/"
if ref.startswith(prefix):
return ref[len(prefix) :]
# The ref is a different type than the requested one, return None
# to indicate this.
elif ref.startswith("refs/"):
return None
return ref

if config.params["head_tag"]:
build_params.setdefault("tag", config.params["head_tag"])
head_ref = normref(config.params["head_ref"])
head_tag = normref(config.params["head_tag"], type="tags")
base_ref = normref(config.params["base_ref"])

if head_ref:
build_params.setdefault("branch", head_ref)

if head_tag:
build_params.setdefault("tag", head_tag)

if config.params["tasks_for"] == "github-pull-request":
build_params.setdefault("pull_request_author", config.params["owner"])

if config.params["base_ref"]:
build_params.setdefault("branch_dest", config.params["base_ref"])
if base_ref:
build_params.setdefault("branch_dest", base_ref)

if config.params["base_repository"]:
build_params.setdefault(
Expand Down
47 changes: 47 additions & 0 deletions test/test_worker_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,53 @@
},
id="opposite params", # this test helps hit other half of if statements
),
pytest.param(
{"bitrise": {"app": "some-app", "workflows": ["bar"]}},
{
"base_ref": "refs/heads/foo",
"base_repository": "",
"head_ref": "refs/heads/bar",
"head_tag": "refs/tags/some-tag",
"tasks_for": "github-pull-request",
},
{
"payload": {
"build_params": {
"branch": "bar",
"branch_dest": "foo",
"branch_repo_owner": "http://example.com/head/repo",
"commit_hash": "abcdef",
"pull_request_author": "some-owner",
"tag": "some-tag",
}
},
"scopes": ["foo:bitrise:app:some-app", "foo:bitrise:workflow:bar"],
"tags": {"worker-implementation": "scriptworker"},
},
id="normalize refs",
),
pytest.param(
{"bitrise": {"app": "some-app", "workflows": ["bar"]}},
{
"base_ref": "refs/tags/foo",
"base_repository": "",
"head_ref": "refs/tags/bar",
"head_tag": "refs/heads/some-tag",
"tasks_for": "github-pull-request",
},
{
"payload": {
"build_params": {
"branch_repo_owner": "http://example.com/head/repo",
"commit_hash": "abcdef",
"pull_request_author": "some-owner",
}
},
"scopes": ["foo:bitrise:app:some-app", "foo:bitrise:workflow:bar"],
"tags": {"worker-implementation": "scriptworker"},
},
id="normalize refs wrong type",
),
),
)
def test_build_bitrise_payload(
Expand Down

0 comments on commit c86ac64

Please sign in to comment.