From 3d0729de5e4867889ac4e7a059f3448b652c6aee Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:32:55 +0000 Subject: [PATCH 01/11] ENH: Add check for doc or workflow label Checking if the doc or workflow label is added to the PR. If it is then the CI should pass --- .github/workflows/self_test.yaml | 3 ++- action.yml | 3 +++ src/main.py | 24 +++++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/.github/workflows/self_test.yaml b/.github/workflows/self_test.yaml index cfc1bfc..7219e92 100644 --- a/.github/workflows/self_test.yaml +++ b/.github/workflows/self_test.yaml @@ -17,10 +17,11 @@ jobs: - name: Self test if: ${{ github.ref != 'refs/heads/main' }} id: selftest - uses: khalford/check-version-action@main + uses: stfc/check-version-action@pass_with_doc with: app_version_path: "version.txt" docker_compose_path: "docker-compose.yml" + labels: ${{ toJSON(github.event.pull_request.labels.*.name) }} - name: Log Success if: ${{ env.app_updated == 'true' }} diff --git a/action.yml b/action.yml index e65ec22..d6c458b 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,9 @@ inputs: docker_compose_path: description: 'Path to compose file.' required: false + labels: + description: 'Labels added to pull requests.' + required: True outputs: app_updated: description: 'If the app version was updated or not.' diff --git a/src/main.py b/src/main.py index 8286bb5..c4a784b 100644 --- a/src/main.py +++ b/src/main.py @@ -18,18 +18,20 @@ def main(): with open(branch_path / app_path, "r", encoding="utf-8") as release_file: release_version = release_file.read().strip("\n") - CompareAppVersion().run(main_path / app_path, branch_path / app_path) - if compose_path: - compose_path = Path(compose_path) - CompareComposeVersion().run(branch_path / app_path, branch_path / compose_path) - - github_env = os.getenv("GITHUB_ENV") - with open(github_env, "a", encoding="utf-8") as env: - # We can assume either/both of these values returned true otherwise they would have errored - env.write("app_updated=true\n") + labels = os.environ.get("INPUT_LABELS") + if not any(label in labels for label in ["documentation", "workflow"]): + CompareAppVersion().run(main_path / app_path, branch_path / app_path) if compose_path: - env.write("compose_updated=true") - env.write(f"release_tag={release_version}") + compose_path = Path(compose_path) + CompareComposeVersion().run(branch_path / app_path, branch_path / compose_path) + + github_env = os.getenv("GITHUB_ENV") + with open(github_env, "a", encoding="utf-8") as env: + # We can assume either/both of these values returned true otherwise they would have errored + env.write("app_updated=true\n") + if compose_path: + env.write("compose_updated=true") + env.write(f"release_tag={release_version}") if __name__ == "__main__": From f051ad329220aeb3d568992af7c7bf1fb83d3c00 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:36:58 +0000 Subject: [PATCH 02/11] STY: Black --- src/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index c4a784b..b8fca78 100644 --- a/src/main.py +++ b/src/main.py @@ -23,7 +23,9 @@ def main(): CompareAppVersion().run(main_path / app_path, branch_path / app_path) if compose_path: compose_path = Path(compose_path) - CompareComposeVersion().run(branch_path / app_path, branch_path / compose_path) + CompareComposeVersion().run( + branch_path / app_path, branch_path / compose_path + ) github_env = os.getenv("GITHUB_ENV") with open(github_env, "a", encoding="utf-8") as env: From ca691ba9b08a19436178626ca2ec806309ca70e9 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:41:59 +0000 Subject: [PATCH 03/11] TEST: Fix tests --- tests/test_main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_main.py b/tests/test_main.py index 7aec16b..3f10115 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -10,9 +10,11 @@ @patch("main.os") def test_main(mock_os, mock_compare_app, mock_compare_compose): """Test the main method runs correctly.""" - mock_os.environ.get.side_effect = [Path("app"), Path("compose"), Path("workspace")] + mock_os.environ.get.side_effect = [Path("app"), Path("compose"), Path("workspace"), []] + with patch("builtins.open", mock_open(read_data="1.0.0")): main() + mock_os.environ.get.assert_any_call("INPUT_LABELS") mock_os.environ.get.assert_any_call("INPUT_APP_VERSION_PATH") mock_os.environ.get.assert_any_call("INPUT_DOCKER_COMPOSE_PATH") mock_os.environ.get.assert_any_call("GITHUB_WORKSPACE") From 5b18672e4c661b8a600f5ef6345533bb48720510 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:42:59 +0000 Subject: [PATCH 04/11] STY: Black --- tests/test_main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_main.py b/tests/test_main.py index 3f10115..c8317ce 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -10,7 +10,12 @@ @patch("main.os") def test_main(mock_os, mock_compare_app, mock_compare_compose): """Test the main method runs correctly.""" - mock_os.environ.get.side_effect = [Path("app"), Path("compose"), Path("workspace"), []] + mock_os.environ.get.side_effect = [ + Path("app"), + Path("compose"), + Path("workspace"), + [], + ] with patch("builtins.open", mock_open(read_data="1.0.0")): main() From 91f34cda55e003e6c1e2142b84c99305e2752342 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:49:47 +0000 Subject: [PATCH 05/11] MAINT: Revert to main --- .github/workflows/self_test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/self_test.yaml b/.github/workflows/self_test.yaml index 7219e92..66a95dc 100644 --- a/.github/workflows/self_test.yaml +++ b/.github/workflows/self_test.yaml @@ -17,7 +17,7 @@ jobs: - name: Self test if: ${{ github.ref != 'refs/heads/main' }} id: selftest - uses: stfc/check-version-action@pass_with_doc + uses: stfc/check-version-action@main with: app_version_path: "version.txt" docker_compose_path: "docker-compose.yml" From 8df3901975503cde5bdb053fa05b22238a54d0a7 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:50:17 +0000 Subject: [PATCH 06/11] MAINT: Change to not required This is optional for now as using it or not doesn't affect the major outcome --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index d6c458b..de10723 100644 --- a/action.yml +++ b/action.yml @@ -10,7 +10,7 @@ inputs: required: false labels: description: 'Labels added to pull requests.' - required: True + required: False outputs: app_updated: description: 'If the app version was updated or not.' From 98f07f21648152425e430fd70e9a4f35cec1b4ce Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:52:09 +0000 Subject: [PATCH 07/11] MAINT: Skip checks if uses certain labels --- src/main.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main.py b/src/main.py index b8fca78..122773f 100644 --- a/src/main.py +++ b/src/main.py @@ -20,20 +20,22 @@ def main(): labels = os.environ.get("INPUT_LABELS") if not any(label in labels for label in ["documentation", "workflow"]): - CompareAppVersion().run(main_path / app_path, branch_path / app_path) - if compose_path: - compose_path = Path(compose_path) - CompareComposeVersion().run( - branch_path / app_path, branch_path / compose_path - ) + return + + CompareAppVersion().run(main_path / app_path, branch_path / app_path) + if compose_path: + compose_path = Path(compose_path) + CompareComposeVersion().run( + branch_path / app_path, branch_path / compose_path + ) - github_env = os.getenv("GITHUB_ENV") - with open(github_env, "a", encoding="utf-8") as env: - # We can assume either/both of these values returned true otherwise they would have errored - env.write("app_updated=true\n") - if compose_path: - env.write("compose_updated=true") - env.write(f"release_tag={release_version}") + github_env = os.getenv("GITHUB_ENV") + with open(github_env, "a", encoding="utf-8") as env: + # We can assume either/both of these values returned true otherwise they would have errored + env.write("app_updated=true\n") + if compose_path: + env.write("compose_updated=true") + env.write(f"release_tag={release_version}") if __name__ == "__main__": From ae66935aa6197ca88fdc0135e163bfca619ab1bc Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:52:15 +0000 Subject: [PATCH 08/11] MAINT: Bump versions --- docker-compose.yml | 2 +- version.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9ede7c4..b8fb975 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,3 +1,3 @@ services: self-test: - image: some/test:1.0.0 + image: some/test:1.1.0 diff --git a/version.txt b/version.txt index 3eefcb9..9084fa2 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 +1.1.0 From b7b155f6f32815bf58f6d8090e0b37ffabed4637 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 11:54:37 +0000 Subject: [PATCH 09/11] BUG: Should skip if labels are present --- src/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 122773f..d105f7b 100644 --- a/src/main.py +++ b/src/main.py @@ -19,7 +19,7 @@ def main(): release_version = release_file.read().strip("\n") labels = os.environ.get("INPUT_LABELS") - if not any(label in labels for label in ["documentation", "workflow"]): + if any(label in labels for label in ["documentation", "workflow"]): return CompareAppVersion().run(main_path / app_path, branch_path / app_path) From 18e919d5e71844cdec12daaa585954428d50c10a Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 12:06:34 +0000 Subject: [PATCH 10/11] TEST: Add tests for skipped case --- src/main.py | 11 +++++------ tests/test_main.py | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main.py b/src/main.py index d105f7b..34cf5f4 100644 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,7 @@ from comparison import CompareAppVersion, CompareComposeVersion -def main(): +def main() -> bool: """ The entry point function for the action. Here we get environment variables then set environment variables when finished. @@ -20,14 +20,12 @@ def main(): labels = os.environ.get("INPUT_LABELS") if any(label in labels for label in ["documentation", "workflow"]): - return - + return False + CompareAppVersion().run(main_path / app_path, branch_path / app_path) if compose_path: compose_path = Path(compose_path) - CompareComposeVersion().run( - branch_path / app_path, branch_path / compose_path - ) + CompareComposeVersion().run(branch_path / app_path, branch_path / compose_path) github_env = os.getenv("GITHUB_ENV") with open(github_env, "a", encoding="utf-8") as env: @@ -36,6 +34,7 @@ def main(): if compose_path: env.write("compose_updated=true") env.write(f"release_tag={release_version}") + return True if __name__ == "__main__": diff --git a/tests/test_main.py b/tests/test_main.py index c8317ce..810f25a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -18,7 +18,7 @@ def test_main(mock_os, mock_compare_app, mock_compare_compose): ] with patch("builtins.open", mock_open(read_data="1.0.0")): - main() + res = main() mock_os.environ.get.assert_any_call("INPUT_LABELS") mock_os.environ.get.assert_any_call("INPUT_APP_VERSION_PATH") mock_os.environ.get.assert_any_call("INPUT_DOCKER_COMPOSE_PATH") @@ -31,3 +31,18 @@ def test_main(mock_os, mock_compare_app, mock_compare_compose): mock_compare_compose.return_value.run.assert_called_once_with( mock_branch_path / "app", mock_branch_path / "compose" ) + assert res + + +@patch("main.os") +def test_main_skip(mock_os): + """Test the main method skips the comparison methods.""" + mock_os.environ.get.side_effect = [ + Path("app"), + Path("compose"), + Path("workspace"), + ["workflow", "documentation"], + ] + with patch("builtins.open", mock_open(read_data="1.0.0")): + res = main() + assert not res From 8565e223a927f5bb91951915ec1c322ed9e61d97 Mon Sep 17 00:00:00 2001 From: Kalibh Halford Date: Thu, 14 Nov 2024 12:10:29 +0000 Subject: [PATCH 11/11] DOC: README updates --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a57c54..3dc6db7 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ Read here for details: [Link to GitHub docs](https://docs.github.com/en/actions/ The release tag is extracted and stored in `$GITHUB_ENV`, you can access this in your workflow with `$ {{ env.release_tag }}` +If you are making a change which should not affect the version such as README or CI changes. You can label the pull request with `documentation` or `workflow` and the version checks will be skipped. + ```yaml - name: Checkout main @@ -43,12 +45,13 @@ you can access this in your workflow with `$ {{ env.release_tag }}` # Don't run on main otherwise it will compare main with main if: ${{ github.ref != 'refs/heads/main' }} id: version_comparison - uses: khalford/check-version-action@main + uses: stfc/check-version-action@main with: # Path to version file from project root app_version_path: "version.txt" # Optional: To check if compose image version matches application version docker_compose_path: "docker-compose.yaml" + labels: ${{ toJSON(github.event.pull_request.labels.*.name) }} - name: Log App Success if: ${{ env.app_updated == 'true' }}