diff --git a/src/gardenlinux/github/release/__init__.py b/src/gardenlinux/github/release/__init__.py index 838d4e39..721153c7 100644 --- a/src/gardenlinux/github/release/__init__.py +++ b/src/gardenlinux/github/release/__init__.py @@ -1,3 +1,4 @@ +import json import os import requests @@ -9,6 +10,43 @@ REQUESTS_TIMEOUTS = (5, 30) # connect, read +def create_github_release(owner, repo, tag, commitish, latest, body): + token = os.environ.get("GITHUB_TOKEN") + if not token: + raise ValueError("GITHUB_TOKEN environment variable not set") + + headers = { + "Authorization": f"token {token}", + "Accept": "application/vnd.github.v3+json", + } + + data = { + "tag_name": tag, + "target_commitish": commitish, + "name": tag, + "body": body, + "draft": False, + "prerelease": False, + "make_latest": latest + } + + response = requests.post( + f"https://api.github.com/repos/{owner}/{repo}/releases", + headers=headers, + data=json.dumps(data), + timeout=REQUESTS_TIMEOUTS + ) + + if response.status_code == 201: + LOGGER.info("Release created successfully") + response_json = response.json() + return response_json.get("id") + else: + LOGGER.error("Failed to create release") + LOGGER.debug(response.json()) + response.raise_for_status() + + def upload_to_github_release_page( github_owner, github_repo, gardenlinux_release_id, file_to_upload, dry_run ): diff --git a/tests/constants.py b/tests/constants.py index d3883d8c..77a525b4 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -25,5 +25,6 @@ TEST_VERSION_STABLE = "1000" TEST_GARDENLINUX_RELEASE = "1877.3" +TEST_GARDENLINUX_COMMIT = "75df9f401a842914563f312899ec3ce34b24515c" S3_DOWNLOADS_DIR = Path(os.path.dirname(__file__)) / ".." / "s3_downloads" diff --git a/tests/github/test_create_github_release.py b/tests/github/test_create_github_release.py new file mode 100644 index 00000000..a81270b6 --- /dev/null +++ b/tests/github/test_create_github_release.py @@ -0,0 +1,56 @@ +import pytest +import requests +import requests_mock + +from gardenlinux.github.release import create_github_release + +from ..constants import TEST_GARDENLINUX_COMMIT, TEST_GARDENLINUX_RELEASE + + +def test_create_github_release_needs_github_token(): + with requests_mock.Mocker(): + with pytest.raises(ValueError) as exn: + create_github_release( + "gardenlinux", + "gardenlinux", + TEST_GARDENLINUX_RELEASE, + TEST_GARDENLINUX_COMMIT, + False, + "") + assert str(exn.value) == "GITHUB_TOKEN environment variable not set", \ + "Expected an exception to be raised on missing GITHUB_TOKEN environment variable" + + +def test_create_github_release_raise_on_failure(caplog, github_token): + with requests_mock.Mocker() as m: + with pytest.raises(requests.exceptions.HTTPError): + m.post( + "https://api.github.com/repos/gardenlinux/gardenlinux/releases", + text="{}", + status_code=503 + ) + create_github_release( + "gardenlinux", + "gardenlinux", + TEST_GARDENLINUX_RELEASE, + TEST_GARDENLINUX_COMMIT, + False, + "") + assert any("Failed to create release" in record.message for record in caplog.records), "Expected a failure log record" + + +def test_create_github_release(caplog, github_token): + with requests_mock.Mocker() as m: + m.post( + "https://api.github.com/repos/gardenlinux/gardenlinux/releases", + text='{"id": 101}', + status_code=201 + ) + assert create_github_release( + "gardenlinux", + "gardenlinux", + TEST_GARDENLINUX_RELEASE, + TEST_GARDENLINUX_COMMIT, + False, + "") == 101 + assert any("Release created successfully" in record.message for record in caplog.records), "Expected a success log record"