-
Notifications
You must be signed in to change notification settings - Fork 2
upload to github release page #191
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 all commits
Commits
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
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import argparse | ||
|
||
from .release import upload_to_github_release_page | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="GitHub Release Script") | ||
subparsers = parser.add_subparsers(dest="command") | ||
|
||
upload_parser = subparsers.add_parser("upload") | ||
upload_parser.add_argument("--owner", default="gardenlinux") | ||
upload_parser.add_argument("--repo", default="gardenlinux") | ||
upload_parser.add_argument("--release_id", required=True) | ||
upload_parser.add_argument("--file_path", required=True) | ||
upload_parser.add_argument("--dry-run", action="store_true", default=False) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == "upload": | ||
upload_to_github_release_page( | ||
args.owner, args.repo, args.release_id, args.file_path, args.dry_run | ||
) | ||
else: | ||
parser.print_help() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,46 @@ | ||
import os | ||
|
||
import requests | ||
|
||
from gardenlinux.logger import LoggerSetup | ||
|
||
LOGGER = LoggerSetup.get_logger("gardenlinux.github", "INFO") | ||
|
||
REQUESTS_TIMEOUTS = (5, 30) # connect, read | ||
|
||
|
||
def upload_to_github_release_page( | ||
github_owner, github_repo, gardenlinux_release_id, file_to_upload, dry_run | ||
): | ||
if dry_run: | ||
LOGGER.info( | ||
f"Dry run: would upload {file_to_upload} to release {gardenlinux_release_id} in repo {github_owner}/{github_repo}" | ||
) | ||
return | ||
|
||
token = os.environ.get("GITHUB_TOKEN") | ||
if not token: | ||
raise ValueError("GITHUB_TOKEN environment variable not set") | ||
|
||
headers = { | ||
"Authorization": f"token {token}", | ||
"Content-Type": "application/octet-stream", | ||
} | ||
|
||
upload_url = f"https://uploads.github.com/repos/{github_owner}/{github_repo}/releases/{gardenlinux_release_id}/assets?name={os.path.basename(file_to_upload)}" | ||
|
||
try: | ||
with open(file_to_upload, "rb") as f: | ||
file_contents = f.read() | ||
except IOError as e: | ||
LOGGER.error(f"Error reading file {file_to_upload}: {e}") | ||
return | ||
|
||
response = requests.post(upload_url, headers=headers, data=file_contents, timeout=REQUESTS_TIMEOUTS) | ||
if response.status_code == 201: | ||
LOGGER.info("Upload successful") | ||
else: | ||
LOGGER.error( | ||
f"Upload failed with status code {response.status_code}: {response.text}" | ||
) | ||
response.raise_for_status() |
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
Empty file.
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,28 @@ | ||
import os | ||
import shutil | ||
|
||
import pytest | ||
|
||
from ..constants import S3_DOWNLOADS_DIR | ||
|
||
|
||
@pytest.fixture | ||
def downloads_dir(): | ||
os.makedirs(S3_DOWNLOADS_DIR, exist_ok=True) | ||
yield | ||
shutil.rmtree(S3_DOWNLOADS_DIR) | ||
|
||
|
||
@pytest.fixture | ||
def github_token(): | ||
os.environ["GITHUB_TOKEN"] = "foobarbazquux" | ||
yield | ||
del os.environ["GITHUB_TOKEN"] | ||
|
||
|
||
@pytest.fixture | ||
def artifact_for_upload(downloads_dir): | ||
artifact = S3_DOWNLOADS_DIR / "artifact.log" | ||
artifact.touch() | ||
yield artifact | ||
artifact.unlink() |
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,117 @@ | ||
import sys | ||
|
||
import pytest | ||
import requests | ||
import requests_mock | ||
|
||
import gardenlinux.github.__main__ as gh | ||
from gardenlinux.github.release import upload_to_github_release_page | ||
|
||
from ..constants import TEST_GARDENLINUX_RELEASE | ||
|
||
|
||
def test_upload_to_github_release_page_dryrun(caplog, artifact_for_upload): | ||
with requests_mock.Mocker(): | ||
assert upload_to_github_release_page( | ||
"gardenlinux", | ||
"gardenlinux", | ||
TEST_GARDENLINUX_RELEASE, | ||
artifact_for_upload, | ||
dry_run=True) is None | ||
assert any("Dry run: would upload" in record.message for record in caplog.records), "Expected a dry‑run log entry" | ||
|
||
|
||
def test_upload_to_github_release_page_needs_github_token(downloads_dir, artifact_for_upload): | ||
with requests_mock.Mocker(): | ||
with pytest.raises(ValueError) as exn: | ||
upload_to_github_release_page( | ||
"gardenlinux", | ||
"gardenlinux", | ||
TEST_GARDENLINUX_RELEASE, | ||
artifact_for_upload, | ||
dry_run=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_upload_to_github_release_page(downloads_dir, caplog, github_token, artifact_for_upload): | ||
with requests_mock.Mocker(real_http=True) as m: | ||
m.post( | ||
f"https://uploads.github.com/repos/gardenlinux/gardenlinux/releases/{TEST_GARDENLINUX_RELEASE}/assets?name=artifact.log", | ||
text="{}", | ||
status_code=201 | ||
) | ||
|
||
upload_to_github_release_page( | ||
"gardenlinux", | ||
"gardenlinux", | ||
TEST_GARDENLINUX_RELEASE, | ||
artifact_for_upload, | ||
dry_run=False) | ||
assert any("Upload successful" in record.message for record in caplog.records), \ | ||
"Expected an upload confirmation log entry" | ||
|
||
|
||
def test_upload_to_github_release_page_unreadable_artifact(downloads_dir, caplog, github_token, artifact_for_upload): | ||
artifact_for_upload.chmod(0) | ||
|
||
upload_to_github_release_page( | ||
"gardenlinux", | ||
"gardenlinux", | ||
TEST_GARDENLINUX_RELEASE, | ||
artifact_for_upload, | ||
dry_run=False) | ||
assert any("Error reading file" in record.message for record in caplog.records), \ | ||
"Expected an error message log entry" | ||
|
||
|
||
def test_upload_to_github_release_page_failed(downloads_dir, caplog, github_token, artifact_for_upload): | ||
with requests_mock.Mocker(real_http=True) as m: | ||
m.post( | ||
f"https://uploads.github.com/repos/gardenlinux/gardenlinux/releases/{TEST_GARDENLINUX_RELEASE}/assets?name=artifact.log", | ||
text="{}", | ||
status_code=503 | ||
) | ||
|
||
with pytest.raises(requests.exceptions.HTTPError): | ||
upload_to_github_release_page( | ||
"gardenlinux", | ||
"gardenlinux", | ||
TEST_GARDENLINUX_RELEASE, | ||
artifact_for_upload, | ||
dry_run=False) | ||
assert any("Upload failed with status code 503:" in record.message for record in caplog.records), \ | ||
"Expected an error HTTP status code to be logged" | ||
|
||
|
||
def test_script_parse_args_wrong_command(monkeypatch, capfd): | ||
monkeypatch.setattr(sys, "argv", ["gh", "rejoice"]) | ||
|
||
with pytest.raises(SystemExit): | ||
gh.main() | ||
captured = capfd.readouterr() | ||
|
||
assert "argument command: invalid choice: 'rejoice'" in captured.err, "Expected help message printed" | ||
|
||
|
||
def test_script_parse_args_upload_command_required_args(monkeypatch, capfd): | ||
monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", "gardenlinux"]) | ||
|
||
with pytest.raises(SystemExit): | ||
gh.main() | ||
captured = capfd.readouterr() | ||
|
||
assert "the following arguments are required: --release_id, --file_path" in captured.err, \ | ||
"Expected help message on missing arguments for 'upload' command" | ||
|
||
|
||
def test_script_upload_dry_run(monkeypatch, capfd): | ||
monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", | ||
"gardenlinux", "--release_id", TEST_GARDENLINUX_RELEASE, "--file_path", "foo", "--dry-run"]) | ||
monkeypatch.setattr("gardenlinux.github.__main__.upload_to_github_release_page", | ||
lambda a1, a2, a3, a4, dry_run: print(f"dry-run: {dry_run}")) | ||
|
||
gh.main() | ||
captured = capfd.readouterr() | ||
|
||
assert captured.out == "dry-run: True\n" |
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.