Skip to content
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

use upload-artifact v4 #20

Merged
merged 1 commit into from
Oct 13, 2024
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
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.

Loading