-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh_check_artifacts
More file actions
executable file
·89 lines (70 loc) · 2.32 KB
/
gh_check_artifacts
File metadata and controls
executable file
·89 lines (70 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env python3
import typer
import subprocess
import json
from typing import Optional
from pathlib import Path
from rich import print as echo
app = typer.Typer(pretty_exceptions_show_locals=False)
def get_artifacts(owner: str, repo: str) -> dict:
cmd = [
"gh",
"api",
"-H",
"Accept: application/vnd.github+json",
"-H",
"X-GitHub-Api-Version: 2022-11-28",
f"/repos/{owner}/{repo}/actions/artifacts",
]
result = subprocess.run(cmd, capture_output=True, text=True)
result.check_returncode()
return json.loads(result.stdout)
def delete_artifact(owner: str, repo: str, artifact_id: int) -> None:
cmd = [
"gh",
"api",
"--method",
"DELETE",
"-H",
"Accept: application/vnd.github+json",
"-H",
"X-GitHub-Api-Version: 2022-11-28",
f"/repos/{owner}/{repo}/actions/artifacts/{artifact_id}",
]
result = subprocess.run(cmd, capture_output=True, text=True)
result.check_returncode()
def check_size(result: dict) -> float:
"""Return total size of artifacts in Bytes"""
n_bytes_total = 0
for element in result["artifacts"]:
n_bytes_total += element["size_in_bytes"]
return n_bytes_total
@app.command()
def main(
owner: str = typer.Argument(..., help="Repository owner"),
repo: str = typer.Argument(..., help="Repository name"),
output: Optional[Path] = typer.Option(None, help="Output file (default: stdout)"),
delete: bool = False,
verbose: bool = False,
):
"""Fetch GitHub Actions artifacts using gh CLI"""
try:
result = get_artifacts(owner, repo)
except subprocess.CalledProcessError as e:
typer.secho(f"Error: {e.stderr}", fg=typer.colors.RED, err=True)
raise typer.Exit(1)
echo(f"... number of artifacts: ", result["total_count"])
echo(f"... total size = {check_size(result=result)/1024**3:.3f} Gb")
if verbose:
for element in result["artifacts"]:
_id = element["id"]
echo(f"... Artifact '{_id}':")
echo(element)
if delete:
echo("*** DELETE ARTIFACTS")
for element in result["artifacts"]:
_id = element["id"]
echo(f"--> DELETE {_id}")
delete_artifact(owner, repo, _id)
if __name__ == "__main__":
app()