Skip to content

Commit 3bad1be

Browse files
authored
Merge pull request #20 from pre-commit-ci/upload-artifact-v4
use upload-artifact v4
2 parents ea952db + 3282fa3 commit 3bad1be

File tree

2 files changed

+98
-43
lines changed

2 files changed

+98
-43
lines changed

bin/main

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import os.path
99
import shutil
1010
import subprocess
1111
import tempfile
12-
import urllib.request
1312
from collections.abc import Iterable
1413
from collections.abc import Sequence
1514
from typing import Any
@@ -23,6 +22,7 @@ _GIT = (
2322
2423
'-c', 'protocol.version=2',
2524
)
25+
_HERE = os.path.dirname(os.path.abspath(__file__))
2626

2727

2828
def _has_changes(*, src_repo: str) -> bool:
@@ -213,48 +213,23 @@ def _save_artifact(
213213
url: str,
214214
token: str,
215215
) -> None:
216-
contents = json.dumps(data, separators=(',', ':')).encode()
217-
218-
artifact_name = f'pre-commit-ci-lite-{run_id}'
219-
220-
headers = {
221-
'Accept': 'application/json;api-version=6.0-preview',
222-
'Authorization': f'Bearer {token}',
223-
}
224-
225-
base_url = f'{url}_apis/pipelines/workflows/{run_id}/artifacts?api-version=6.0-preview' # noqa: E501
226-
227-
req_create = urllib.request.Request(
228-
base_url,
229-
method='POST',
230-
headers={**headers, 'Content-Type': 'application/json'},
231-
data=json.dumps({
232-
'type': 'actions_storage',
233-
'name': artifact_name,
234-
'retentionDays': 1,
235-
}).encode(),
236-
)
237-
resp_create = json.load(urllib.request.urlopen(req_create))
238-
239-
req_upload = urllib.request.Request(
240-
f'{resp_create["fileContainerResourceUrl"]}?itemPath={artifact_name}/data.json', # noqa: E501
241-
method='PUT',
242-
headers={
243-
**headers,
244-
'Content-Type': 'application/octet-stream',
245-
'Content-Range': f'bytes 0-{len(contents) - 1}/{len(contents)}',
246-
},
247-
data=contents,
248-
)
249-
urllib.request.urlopen(req_upload)
250-
251-
req_finish = urllib.request.Request(
252-
f'{base_url}&artifactName={artifact_name}',
253-
method='PATCH',
254-
headers={**headers, 'Content-Type': 'application/json'},
255-
data=json.dumps({'size': len(contents)}).encode(),
256-
)
257-
urllib.request.urlopen(req_finish)
216+
with tempfile.TemporaryDirectory() as tmpdir:
217+
data_json = os.path.join(tmpdir, 'data.json')
218+
with open(data_json, 'w', encoding='UTF-8') as f:
219+
json.dump(data, f, separators=(',', ':'))
220+
221+
ret = subprocess.call(
222+
(
223+
os.path.join(_HERE, 'upload-artifact-cli'),
224+
f'pre-commit-ci-lite-{run_id}',
225+
'data.json',
226+
),
227+
cwd=tmpdir,
228+
stdout=subprocess.DEVNULL,
229+
stderr=subprocess.DEVNULL,
230+
)
231+
if ret:
232+
raise SystemExit(f'uploading artifact failed with code {ret}')
258233

259234

260235
def main(argv: Sequence[str] | None = None) -> int:

bin/upload-artifact-cli

Lines changed: 80 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)