Skip to content

Commit da81f24

Browse files
committed
create_github_release routine
1 parent d2eca70 commit da81f24

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/gardenlinux/github/release/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23

34
import requests
@@ -9,6 +10,43 @@
910
REQUESTS_TIMEOUTS = (5, 30) # connect, read
1011

1112

13+
def create_github_release(owner, repo, tag, commitish, latest, body):
14+
token = os.environ.get("GITHUB_TOKEN")
15+
if not token:
16+
raise ValueError("GITHUB_TOKEN environment variable not set")
17+
18+
headers = {
19+
"Authorization": f"token {token}",
20+
"Accept": "application/vnd.github.v3+json",
21+
}
22+
23+
data = {
24+
"tag_name": tag,
25+
"target_commitish": commitish,
26+
"name": tag,
27+
"body": body,
28+
"draft": False,
29+
"prerelease": False,
30+
"make_latest": latest
31+
}
32+
33+
response = requests.post(
34+
f"https://api.github.com/repos/{owner}/{repo}/releases",
35+
headers=headers,
36+
data=json.dumps(data),
37+
timeout=REQUESTS_TIMEOUTS
38+
)
39+
40+
if response.status_code == 201:
41+
LOGGER.info("Release created successfully")
42+
response_json = response.json()
43+
return response_json.get("id")
44+
else:
45+
LOGGER.error("Failed to create release")
46+
LOGGER.debug(response.json())
47+
response.raise_for_status()
48+
49+
1250
def upload_to_github_release_page(
1351
github_owner, github_repo, gardenlinux_release_id, file_to_upload, dry_run
1452
):

tests/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
TEST_VERSION_STABLE = "1000"
2626

2727
TEST_GARDENLINUX_RELEASE = "1877.3"
28+
TEST_GARDENLINUX_COMMIT = "75df9f401a842914563f312899ec3ce34b24515c"
2829

2930
S3_DOWNLOADS_DIR = Path(os.path.dirname(__file__)) / ".." / "s3_downloads"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
import requests
3+
import requests_mock
4+
5+
from gardenlinux.github.release import create_github_release
6+
7+
from ..constants import TEST_GARDENLINUX_COMMIT, TEST_GARDENLINUX_RELEASE
8+
9+
10+
def test_create_github_release_needs_github_token():
11+
with requests_mock.Mocker():
12+
with pytest.raises(ValueError) as exn:
13+
create_github_release(
14+
"gardenlinux",
15+
"gardenlinux",
16+
TEST_GARDENLINUX_RELEASE,
17+
TEST_GARDENLINUX_COMMIT,
18+
False,
19+
"")
20+
assert str(exn.value) == "GITHUB_TOKEN environment variable not set", \
21+
"Expected an exception to be raised on missing GITHUB_TOKEN environment variable"
22+
23+
24+
def test_create_github_release_raise_on_failure(caplog, github_token):
25+
with requests_mock.Mocker() as m:
26+
with pytest.raises(requests.exceptions.HTTPError):
27+
m.post(
28+
"https://api.github.com/repos/gardenlinux/gardenlinux/releases",
29+
text="{}",
30+
status_code=503
31+
)
32+
create_github_release(
33+
"gardenlinux",
34+
"gardenlinux",
35+
TEST_GARDENLINUX_RELEASE,
36+
TEST_GARDENLINUX_COMMIT,
37+
False,
38+
"")
39+
assert any("Failed to create release" in record.message for record in caplog.records), "Expected a failure log record"
40+
41+
42+
def test_create_github_release(caplog, github_token):
43+
with requests_mock.Mocker() as m:
44+
m.post(
45+
"https://api.github.com/repos/gardenlinux/gardenlinux/releases",
46+
text='{"id": 101}',
47+
status_code=201
48+
)
49+
assert create_github_release(
50+
"gardenlinux",
51+
"gardenlinux",
52+
TEST_GARDENLINUX_RELEASE,
53+
TEST_GARDENLINUX_COMMIT,
54+
False,
55+
"") == 101
56+
assert any("Release created successfully" in record.message for record in caplog.records), "Expected a success log record"

0 commit comments

Comments
 (0)