-
Notifications
You must be signed in to change notification settings - Fork 23
Support Depends-On #55
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c48c1ed
Support Depends-On
abikouo 8c20b09
adding README.md and unit tests
abikouo 88c65fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 70a4bd0
linters fixes
abikouo f42630d
install pytest for unit tests
abikouo ee15322
update related jobs
abikouo abf16a0
minor code review updates
abikouo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
adding README.md and unit tests
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Checkout_repository | ||
|
|
||
| This action checks-out your repository under the specified destination directory using the action actions/checkout. Use the `depends-On: repository/pull/xx` to override the reference to checkout. | ||
|
|
||
| # Usage | ||
|
|
||
| <!-- start usage --> | ||
|
|
||
| ```yaml | ||
| - uses: ansible-network/github_actions/.github/actions/checkout_repository@main | ||
| with: | ||
| # Repository name with owner. For example, ansible-collections/kubernetes.core | ||
| repository: "" | ||
|
|
||
| # The branch, tag, or SHA to checkout when the pull request body does not | ||
| # contain any override for this repository. | ||
| ref: "" | ||
|
|
||
| # Relative path under $GITHUB_WORKSPACE to place the repository | ||
| path: "" | ||
| ``` | ||
|
|
||
| <!-- end usage --> | ||
|
|
||
| # Depending on others PRs | ||
|
|
||
| The pull request body should contain the following sequence: | ||
|
|
||
| ``` | ||
| Depends-On: repository/pull/xx | ||
| ``` | ||
|
|
||
| # Scenarios | ||
|
|
||
| - [checkout pull request 12345 from repository my_org/my_collection](#Checkout-depending-pull-request) | ||
|
|
||
| ## Checkout depending pull request | ||
|
|
||
| Github action step: | ||
|
|
||
| ```yaml | ||
| - uses: ansible-network/github_actions/.github/actions/checkout_repository@main | ||
| with: | ||
| repository: my_org/my_collection | ||
| ref: main | ||
| path: /path/to/checkout/repository | ||
| ``` | ||
|
|
||
| Pull request body: | ||
|
|
||
| ```text | ||
| Depends-On: https://github.com/my_org/my_collection/pull/12345 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.github/actions/checkout_repository/test_resolve_dependency.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import pytest | ||
| import os | ||
| import string | ||
| from random import choice | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from resolve_dependency import ( | ||
| get_pr_merge_commit_sha, | ||
| resolve_ref, | ||
| main | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "pr_body,match", | ||
| [ | ||
| ("Depends-On: https://github.com/my_org/my_collection/pull/12345", True), | ||
| ( | ||
| "Depends-On: https://github.com/my_org/my_collection/pull/12345\n"\ | ||
| "Depends-On: https://github.com/my_org/my_collection/pull/67890", | ||
| True, | ||
| ), | ||
| ( | ||
| "Depends-On: https://github.com/another_org/my_collection/pull/4000\n"\ | ||
| "Depends-On: https://github.com/my_org/my_collection/pull/12345", | ||
| True, | ||
| ), | ||
| ( | ||
| "Depends-On: https://github.com/my_org/my_collection/pull/12345\n"\ | ||
| "Depends-On: https://github.com/my_org/my_collection/pull/67890", | ||
| True, | ||
| ), | ||
| ("Depends-On: https://github.com/another_org/my_collection/pull/12345", False), | ||
| ("Depends-On: https://github.com/my_org/my_collection2/pull/12345", False), | ||
| ("Depends-On: https://github.com/my_org/my_collection/pull", False), | ||
| ] | ||
| ) | ||
| def test_resolve_ref(pr_body, match): | ||
|
|
||
| expected = 12345 if match else 0 | ||
| assert resolve_ref(pr_body, "my_org/my_collection") == expected | ||
|
|
||
|
|
||
| class FakePullRequest(object): | ||
|
|
||
| def __init__(self, mergeable): | ||
| self.mergeable = mergeable | ||
| self.merge_commit_sha = self.generate_commit_sha() | ||
|
|
||
| @staticmethod | ||
| def generate_commit_sha(length=16): | ||
| data = string.ascii_letters + string.digits | ||
| return "".join([choice(data) for _ in range(length)]) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mergeable", [True, False]) | ||
| @patch("resolve_dependency.Github") | ||
| def test_get_pr_merge_commit_sha(m_Github, mergeable): | ||
|
|
||
| m_github_obj = MagicMock() | ||
| m_Github.return_value = m_github_obj | ||
|
|
||
| os.environ["GITHUB_TOKEN"] = "unittest_github_token" | ||
|
|
||
| m_github_repo = MagicMock() | ||
| m_github_obj.get_repo = MagicMock() | ||
| m_github_obj.get_repo.return_value = m_github_repo | ||
|
|
||
| local_pr = FakePullRequest(mergeable=mergeable) | ||
| m_github_repo.get_pull = MagicMock() | ||
| m_github_repo.get_pull.return_value = local_pr | ||
|
|
||
| repository = "my_testing_repository" | ||
| pr_number = 12345 | ||
|
|
||
| if mergeable: | ||
| assert get_pr_merge_commit_sha(repository, pr_number) == local_pr.merge_commit_sha | ||
| else: | ||
| with pytest.raises(ValueError): | ||
| get_pr_merge_commit_sha(repository, pr_number) | ||
|
|
||
| m_Github.assert_called_once_with("unittest_github_token") | ||
| m_github_obj.get_repo.assert_called_once_with(repository) | ||
| m_github_repo.get_pull.assert_called_once_with(pr_number) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("repository", [True, False]) | ||
| @pytest.mark.parametrize("resolve_ref_pr", [0, 1]) | ||
| @patch("resolve_dependency.get_pr_merge_commit_sha") | ||
| @patch("resolve_dependency.resolve_ref") | ||
| def test_main(m_resolve_ref, m_get_pr_merge_commit_sha, repository, resolve_ref_pr, tmp_path): | ||
|
|
||
| pr_body = "My pull request body - this is a sample for unit tests" | ||
| repository_name = "my_test_repository" | ||
| os.environ["RESOLVE_REF_PR_BODY"] = pr_body | ||
|
|
||
| gh_output_file = tmp_path / "github_output.txt" | ||
| env_update = {"GITHUB_OUTPUT": str(gh_output_file)} | ||
| if repository: | ||
| env_update.update({"RESOLVE_REF_REPOSITORY": repository_name}) | ||
|
|
||
| m_resolve_ref.return_value = resolve_ref_pr | ||
| merge_commit_sha = FakePullRequest.generate_commit_sha() | ||
| m_get_pr_merge_commit_sha.return_value = merge_commit_sha | ||
|
|
||
| with patch.dict(os.environ, env_update): | ||
| main() | ||
|
|
||
| if not repository: | ||
| m_resolve_ref.assert_not_called() | ||
| m_get_pr_merge_commit_sha.assert_not_called() | ||
| assert not gh_output_file.exists() | ||
| elif not resolve_ref_pr: | ||
| m_resolve_ref.assert_called_once_with(pr_body, repository_name) | ||
| m_get_pr_merge_commit_sha.assert_not_called() | ||
| assert not gh_output_file.exists() | ||
| else: | ||
| m_resolve_ref.assert_called_once_with(pr_body, repository_name) | ||
| m_get_pr_merge_commit_sha.assert_called_once_with(repository_name, resolve_ref_pr) | ||
| assert gh_output_file.exists() | ||
| # gh_output_file.read_text() == f"merge_commit_sha={merge_commit_sha}\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.