Skip to content

Commit caf95f0

Browse files
committed
Helper github function for integration tests
These helper functions allows integration tests to open a pull request and manipulate with it. The github API exposes a full PR object that can be used across all tests. JIRA: ISV-5278 Signed-off-by: Ales Raszka <araszka@redhat.com>
1 parent a519fb5 commit caf95f0

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

operator-pipeline-images/operatorcert/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import yaml
1414
from dateutil.parser import isoparse
15-
from operatorcert import github, pyxis
15+
from operatorcert import pyxis
1616
from operatorcert.utils import find_file
1717

1818
# Bundle annotations

operator-pipeline-images/operatorcert/github.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,67 @@ def add_or_remove_labels( # pylint: disable=too-many-locals
272272

273273
add_labels_to_pull_request(pull_request, labels_to_add)
274274
remove_labels_from_pull_request(pull_request, labels_to_remove)
275+
276+
277+
def open_pull_request( # pylint: disable=too-many-arguments
278+
github_client: Github,
279+
repository_name: str,
280+
title: str,
281+
body: str,
282+
head: str,
283+
base: str,
284+
) -> PullRequest.PullRequest:
285+
"""Open a new Github pull request in the given repository.
286+
287+
Args:
288+
github_client (Github): A Github API client
289+
repository_name (str): A repository name in the format "organization/repository"
290+
title (str): A title for the pull request
291+
body (str): A body text for the pull request
292+
head (str): A git reference for the PR head (e.g. a branch name)
293+
base (str): A git reference for the PR base (e.g. a branch name)
294+
295+
Returns:
296+
PullRequest.PullRequest: A Github pull request object
297+
"""
298+
repository = github_client.get_repo(repository_name)
299+
pull_request = repository.create_pull(title=title, body=body, head=head, base=base)
300+
return pull_request
301+
302+
303+
def get_pull_request_by_number(
304+
github_client: Github,
305+
repository_name: str,
306+
pr_number: int,
307+
) -> PullRequest.PullRequest:
308+
"""
309+
Get a Github pull request by number and repository name.
310+
311+
Args:
312+
github_client (Github): A Github API client
313+
repository_name (str): A repository name in the format "organization/repository"
314+
pr_number (int): A pull request number
315+
316+
Returns:
317+
PullRequest.PullRequest: A Github pull request object
318+
"""
319+
repository = github_client.get_repo(repository_name)
320+
pull_request = repository.get_pull(pr_number)
321+
return pull_request
322+
323+
324+
def close_pull_request(
325+
pull_request: PullRequest.PullRequest,
326+
) -> PullRequest.PullRequest:
327+
"""
328+
Close a Github pull request.
329+
330+
Args:
331+
pull_request (PullRequest.PullRequest): A Github pull request object
332+
that should be closed
333+
334+
Returns:
335+
PullRequest: A Github pull request object after closing
336+
"""
337+
pull_request.edit(state="closed")
338+
return pull_request

operator-pipeline-images/tests/test_github.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,49 @@ def test_remove_labels_from_pull_request() -> None:
183183
mock_pull_request.remove_from_labels.assert_has_calls(
184184
[call("label1"), call("label2")]
185185
)
186+
187+
188+
def test_open_pull_request() -> None:
189+
mock_client = MagicMock()
190+
mock_repo = MagicMock()
191+
mock_pull_request = MagicMock()
192+
193+
mock_client.get_repo.return_value = mock_repo
194+
mock_repo.create_pull.return_value = mock_pull_request
195+
196+
resp = github.open_pull_request(
197+
mock_client, "repo_name", "title", "body", "branch", "base"
198+
)
199+
200+
mock_client.get_repo.assert_called_once_with("repo_name")
201+
mock_repo.create_pull.assert_called_once_with(
202+
title="title", body="body", head="branch", base="base"
203+
)
204+
205+
assert resp == mock_pull_request
206+
207+
208+
def test_get_pull_request_by_number() -> None:
209+
mock_client = MagicMock()
210+
mock_repo = MagicMock()
211+
mock_pull_request = MagicMock()
212+
213+
mock_client.get_repo.return_value = mock_repo
214+
mock_repo.get_pull.return_value = mock_pull_request
215+
216+
resp = github.get_pull_request_by_number(mock_client, "repo_name", 1)
217+
218+
mock_client.get_repo.assert_called_once_with("repo_name")
219+
mock_repo.get_pull.assert_called_once_with(1)
220+
221+
assert resp == mock_pull_request
222+
223+
224+
def test_close_pull_request() -> None:
225+
mock_pull_request = MagicMock()
226+
227+
resp = github.close_pull_request(mock_pull_request)
228+
229+
mock_pull_request.edit.assert_called_once_with(state="closed")
230+
231+
assert resp == mock_pull_request

0 commit comments

Comments
 (0)