Skip to content

Commit

Permalink
Add support for multiple branches in builds API calls (#87)
Browse files Browse the repository at this point in the history
* builds: add support for multiple `branch` values

BuildKite docs allow support for multiple branches when filtering. Works
the same way the `state` parameter does.

The `branch` value can be a scalar value, which we presume is a single
name of a branch, or it can be a list.

* deps: bump typing-extensions version to latest

Unable to build successfully with specifically 3.7.4.2 because `black`
specifically requires something newer than that, so it conflicts.

* tests: add tests for multi-branch values

* client: for `branch`, just add the raw value
  • Loading branch information
ltriant committed Apr 18, 2023
1 parent 24922c1 commit 30f2ce2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
19 changes: 16 additions & 3 deletions pybuildkite/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def list_all(
"created_to": self.__api_date_format(created_to),
"finished_from": self.__api_date_format(finished_from),
"state": self.__get_build_states_query_param(states),
"branch": branch,
"branch": self.__get_branches_query_param(branch),
"commit": commit,
"include_retried_jobs": True if include_retried_jobs is True else None,
"page": page,
Expand Down Expand Up @@ -163,7 +163,7 @@ def list_all_for_org(
"created_to": self.__api_date_format(created_to),
"finished_from": self.__api_date_format(finished_from),
"state": self.__get_build_states_query_param(states),
"branch": branch,
"branch": self.__get_branches_query_param(branch),
"commit": commit,
"include_retried_jobs": True if include_retried_jobs is True else None,
"page": page,
Expand Down Expand Up @@ -222,7 +222,7 @@ def list_all_for_pipeline(
"created_to": self.__api_date_format(created_to),
"finished_from": self.__api_date_format(finished_from),
"state": self.__get_build_states_query_param(states),
"branch": branch,
"branch": self.__get_branches_query_param(branch),
"commit": commit,
"include_retried_jobs": True if include_retried_jobs is True else None,
"page": page,
Expand Down Expand Up @@ -364,3 +364,16 @@ def __get_build_states_query_param(states):
for state in states:
param_string += "state[]={}&".format(state.value)
return param_string[:-1]

@staticmethod
def __get_branches_query_param(branches):
if not branches:
return None

if isinstance(branches, List):
param_string = ""
for branch in branches:
param_string += "branch[]={}&".format(branch)
return param_string[:-1]
else:
return "branch=" + branches
2 changes: 2 additions & 0 deletions pybuildkite/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ def _convert_query_params_to_string_for_bytes(query_params):
query_string += "&"
if key == "state":
query_string += value
elif key == "branch":
query_string += value
else:
query_string += key + "=" + str(value)
return query_string
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pytest-cov==2.8.1
requests==2.26.0
urllib3==1.26.5
black==22.6.0
typing-extensions==3.7.4.2
typing-extensions==4.5.0
mypy==0.770
mypy-extensions==0.4.3
28 changes: 28 additions & 0 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,31 @@ def test_rebuild_build(fake_client):
builds.path_for_build_number.format("org_slug", "pipeline_id", "build_number")
+ "/rebuild"
)


def test_multi_branch(fake_client):
builds = Builds(fake_client, "https://api.buildkite.com/v2/")
builds.rebuild_build("org_slug", "pipeline_id", "build_number")

builds.list_all_for_pipeline(
organization="org",
pipeline="pipe",
branch=["main", "master"]
)

args = fake_client.get.call_args[0][1]
assert args["branch"] == "branch[]=main&branch[]=master"


def test_single_branch(fake_client):
builds = Builds(fake_client, "https://api.buildkite.com/v2/")
builds.rebuild_build("org_slug", "pipeline_id", "build_number")

builds.list_all_for_pipeline(
organization="org",
pipeline="pipe",
branch="main"
)

args = fake_client.get.call_args[0][1]
assert args["branch"] == "branch=main"

0 comments on commit 30f2ce2

Please sign in to comment.