Skip to content

Commit f2de064

Browse files
authored
feat: Add --help and --limit option (#4)
* feat: Add `--help` and `--limit` option - feat: Add workflow parameter handling * if workflow is empty, exit * fix: Better exit code * ci: Add `pull_request_target` event To leave more logs * test: Add more tests
1 parent 5bc2c02 commit f2de064

File tree

3 files changed

+91
-6
lines changed

3 files changed

+91
-6
lines changed

.github/workflows/setup.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: setup
22
on:
33
pull_request:
4+
pull_request_target:
45
push:
56
workflow_dispatch:
67
jobs:

gh-workflow-log-cleaner

+42-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,55 @@ set -e
44
# Disable pager
55
export GH_PAGER=
66

7-
if [ -z "$1" ]; then
7+
LIMIT="1000"
8+
WORKFLOW=""
9+
10+
help() {
811
echo "Usage:"
9-
echo "gh workflow-log-cleaner [<workflow-id> | <workflow-name> | <filename>]"
12+
echo "gh workflow-log-cleaner [<workflow-id> | <workflow-name> | <filename>] [options]"
13+
echo ""
14+
echo "Options:"
15+
echo " -h, --help Show this help message and exit"
16+
echo " --limit Limit the number of runs to delete (default: 1000)"
1017
echo ""
1118
echo "Available workflows:"
1219
gh workflow list --all
20+
}
21+
22+
if [ -z "$1" ]; then
23+
help
1324
exit
1425
fi
1526

16-
for id in $(gh run list --workflow "$1" --json databaseId --jq '.[].databaseId' --limit 10000); do
27+
while [ $# -gt 0 ]; do
28+
case "$1" in
29+
--limit)
30+
LIMIT="$2"
31+
shift
32+
;;
33+
-h|--help)
34+
help
35+
exit
36+
;;
37+
-*)
38+
echo "Unknown option: $1"
39+
help
40+
exit 1
41+
;;
42+
*)
43+
WORKFLOW="$1"
44+
;;
45+
esac
46+
shift
47+
done
48+
49+
if [ -z "$WORKFLOW" ]; then
50+
echo "Error: Specify a workflow to delete the log."
51+
help
52+
exit 1
53+
fi
54+
55+
for id in $(gh run list --workflow "$WORKFLOW" --json databaseId --jq '.[].databaseId' --limit $LIMIT); do
1756
echo "Deleting run ID: $id"
1857
gh run delete $id
1958
done

gh-workflow-log-cleaner.bats

+48-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,62 @@ assert_output_contains() {
1111
run ./gh-workflow-log-cleaner
1212
[ "$status" -eq 0 ]
1313
expected_output="Usage:
14-
gh workflow-log-cleaner [<workflow-id> | <workflow-name> | <filename>]
14+
gh workflow-log-cleaner [<workflow-id> | <workflow-name> | <filename>] [options]
15+
16+
Options:
17+
-h, --help Show this help message and exit
18+
--limit Limit the number of runs to delete (default: 1000)
19+
20+
Available workflows:
21+
setup active 114854128
22+
test active 114855097"
23+
[ "$output" = "$expected_output" ]
24+
}
25+
26+
@test "with help option" {
27+
run ./gh-workflow-log-cleaner --help
28+
[ "$status" -eq 0 ]
29+
expected_output="Usage:
30+
gh workflow-log-cleaner [<workflow-id> | <workflow-name> | <filename>] [options]
31+
32+
Options:
33+
-h, --help Show this help message and exit
34+
--limit Limit the number of runs to delete (default: 1000)
1535
1636
Available workflows:
1737
setup active 114854128
1838
test active 114855097"
1939
[ "$output" = "$expected_output" ]
2040
}
2141

22-
@test "with a workflow-name parameter" {
23-
run ./gh-workflow-log-cleaner setup
42+
@test "with a workflow-name and limit option" {
43+
run ./gh-workflow-log-cleaner setup --limit 1
2444
[ "$status" -eq 0 ]
2545
assert_output_contains "Deleting run ID:"
2646
assert_output_contains "✓ Request to delete workflow submitted."
2747
}
48+
49+
@test "with limit option and a workflow-name" {
50+
run ./gh-workflow-log-cleaner --limit 1 setup
51+
[ "$status" -eq 0 ]
52+
assert_output_contains "Deleting run ID:"
53+
assert_output_contains "✓ Request to delete workflow submitted."
54+
}
55+
56+
@test "with an invalid option" {
57+
run ./gh-workflow-log-cleaner setup --invalid
58+
[ "$status" -eq 1 ]
59+
assert_output_contains "Unknown option: --invalid"
60+
}
61+
62+
@test "without a workflow" {
63+
run ./gh-workflow-log-cleaner --limit 1
64+
[ "$status" -eq 1 ]
65+
assert_output_contains "Error: Specify a workflow to delete the log"
66+
}
67+
68+
@test "with an invalid workflow-name" {
69+
run ./gh-workflow-log-cleaner invalid
70+
[ "$status" -eq 0 ]
71+
assert_output_contains "could not find any workflows named invalid"
72+
}

0 commit comments

Comments
 (0)