Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/gardenlinux/github/release/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

import requests
Expand All @@ -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
):
Expand Down
1 change: 1 addition & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
56 changes: 56 additions & 0 deletions tests/github/test_create_github_release.py
Original file line number Diff line number Diff line change
@@ -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"
Loading