Skip to content

Commit

Permalink
Merge pull request #20 from pre-commit-ci/upload-artifact-v4
Browse files Browse the repository at this point in the history
use upload-artifact v4
  • Loading branch information
asottile authored Oct 13, 2024
2 parents ea952db + 3282fa3 commit 3bad1be
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 43 deletions.
61 changes: 18 additions & 43 deletions bin/main
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import os.path
import shutil
import subprocess
import tempfile
import urllib.request
from collections.abc import Iterable
from collections.abc import Sequence
from typing import Any
Expand All @@ -23,6 +22,7 @@ _GIT = (
'-c', '[email protected]',
'-c', 'protocol.version=2',
)
_HERE = os.path.dirname(os.path.abspath(__file__))


def _has_changes(*, src_repo: str) -> bool:
Expand Down Expand Up @@ -213,48 +213,23 @@ def _save_artifact(
url: str,
token: str,
) -> None:
contents = json.dumps(data, separators=(',', ':')).encode()

artifact_name = f'pre-commit-ci-lite-{run_id}'

headers = {
'Accept': 'application/json;api-version=6.0-preview',
'Authorization': f'Bearer {token}',
}

base_url = f'{url}_apis/pipelines/workflows/{run_id}/artifacts?api-version=6.0-preview' # noqa: E501

req_create = urllib.request.Request(
base_url,
method='POST',
headers={**headers, 'Content-Type': 'application/json'},
data=json.dumps({
'type': 'actions_storage',
'name': artifact_name,
'retentionDays': 1,
}).encode(),
)
resp_create = json.load(urllib.request.urlopen(req_create))

req_upload = urllib.request.Request(
f'{resp_create["fileContainerResourceUrl"]}?itemPath={artifact_name}/data.json', # noqa: E501
method='PUT',
headers={
**headers,
'Content-Type': 'application/octet-stream',
'Content-Range': f'bytes 0-{len(contents) - 1}/{len(contents)}',
},
data=contents,
)
urllib.request.urlopen(req_upload)

req_finish = urllib.request.Request(
f'{base_url}&artifactName={artifact_name}',
method='PATCH',
headers={**headers, 'Content-Type': 'application/json'},
data=json.dumps({'size': len(contents)}).encode(),
)
urllib.request.urlopen(req_finish)
with tempfile.TemporaryDirectory() as tmpdir:
data_json = os.path.join(tmpdir, 'data.json')
with open(data_json, 'w', encoding='UTF-8') as f:
json.dump(data, f, separators=(',', ':'))

ret = subprocess.call(
(
os.path.join(_HERE, 'upload-artifact-cli'),
f'pre-commit-ci-lite-{run_id}',
'data.json',
),
cwd=tmpdir,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
if ret:
raise SystemExit(f'uploading artifact failed with code {ret}')


def main(argv: Sequence[str] | None = None) -> int:
Expand Down
80 changes: 80 additions & 0 deletions bin/upload-artifact-cli

Large diffs are not rendered by default.

0 comments on commit 3bad1be

Please sign in to comment.