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
10 changes: 8 additions & 2 deletions cicd/python/attach_build_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ def sha256_file(path: str) -> str:


def gh(*args: str) -> str:
completed = subprocess.run(['gh', *args], capture_output=True, text=True)
# gh writes UTF-8 whatever the console code page. Decoding with the Windows
# default (cp1252) crashes the reader thread on any byte cp1252 cannot map,
# which leaves stdout as None.
completed = subprocess.run(['gh', *args], capture_output=True, text=True,
encoding='utf-8', errors='replace')
if completed.returncode != 0:
detail = completed.stderr.strip() or completed.stdout.strip()
raise ReleaseError(f"gh {' '.join(args)} failed: {detail}")
Expand Down Expand Up @@ -141,7 +145,9 @@ def get_run(repo: str, run_id: int) -> dict:


def tag_commit(repo: str, tag: str) -> str:
return gh_json('api', f'repos/{repo}/commits/{tag}')['sha']
# Only the sha is needed; unfiltered, the commit endpoint also returns the
# whole diff of the tagged commit.
return gh('api', f'repos/{repo}/commits/{tag}', '--jq', '.sha').strip()


def run_artifacts(repo: str, run_id: int) -> Dict[str, dict]:
Expand Down
18 changes: 18 additions & 0 deletions cicd/python/test_attach_build_artifacts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import unittest
from unittest import mock

import attach_build_artifacts as attach


class GhTests(unittest.TestCase):
def test_gh_decodes_output_as_utf8(self):
# Regression: on Windows the default cp1252 decoding of gh output crashed
# the stdout reader thread on the first non-cp1252 byte.
completed = mock.Mock(returncode=0, stdout='{"sha": "f7b4116"}', stderr='')
with mock.patch.object(attach.subprocess, 'run', return_value=completed) as run:
self.assertEqual(attach.gh('api', 'repos/stackql/stackql/commits/v1.0.888'),
'{"sha": "f7b4116"}')
self.assertEqual(run.call_args.kwargs.get('encoding'), 'utf-8')
self.assertEqual(run.call_args.kwargs.get('errors'), 'replace')

def test_tag_commit_requests_only_the_sha(self):
with mock.patch.object(attach, 'gh', return_value='f7b41161\n') as gh:
self.assertEqual(attach.tag_commit('stackql/stackql', 'v1.0.888'), 'f7b41161')
gh.assert_called_once_with('api', 'repos/stackql/stackql/commits/v1.0.888', '--jq', '.sha')


class VersionTests(unittest.TestCase):
def test_accepts_with_and_without_v_prefix(self):
self.assertEqual(attach.parse_version('0.12.778'), '0.12.778')
Expand Down
Loading