Skip to content

Commit 1aef041

Browse files
Fix dry-run pagination
1 parent f7cd1fd commit 1aef041

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

.github/workflows/cleanup-workflows.yml

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ jobs:
109109
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
110110
MODE: ${{ inputs.mode }}
111111
DELETED_WORKFLOWS: ${{ steps.deleted.outputs.workflows }}
112+
OWNER: ${{ github.repository_owner }}
113+
REPO: ${{ github.repository }}
112114
run: |
113115
set -euo pipefail
114116
@@ -135,18 +137,18 @@ jobs:
135137
# Total table width
136138
TOTAL_WIDTH=$(( INDEX_WIDTH + 3 + MAX_WF_LENGTH + 3 + WORKFLOW_COUNT_WIDTH + 3 + GLOBAL_TOTAL_WIDTH ))
137139
138-
# Function to print dynamic table headers padded with '=' and spaces
140+
# Function to print dynamic table headers padded with '=' and spaces around the name
139141
print_header() {
140142
local name="$1"
141143
local name_len=${#name}
142-
# Add 2 for the spaces around the name
143144
local pad=$(( (TOTAL_WIDTH - name_len - 2) / 2 ))
144145
local left_pad=$pad
145146
local right_pad=$(( TOTAL_WIDTH - name_len - 2 - left_pad ))
146147
147148
left_str=$(printf '=%0.s' $(seq 1 $left_pad))
148149
right_str=$(printf '=%0.s' $(seq 1 $right_pad))
149150
151+
printf " \n"
150152
printf "%s %s %s\n" "$left_str" "$name" "$right_str"
151153
}
152154
@@ -171,18 +173,28 @@ jobs:
171173
CURRENT_INDEX=$((i + 1))
172174
WORKFLOW_COUNT=0
173175
176+
# Resolve workflow ID using gh workflow list (legible)
177+
WORKFLOW_ID=$(gh workflow list --all --json id,path \
178+
--jq ".[] | select(.path==\"$workflow\") | .id" 2>/dev/null || true)
179+
180+
if [ -z "$WORKFLOW_ID" ]; then
181+
echo " ⚠️ Workflow not found: $workflow, skipping..."
182+
SUMMARY+=("$workflow|0")
183+
continue
184+
fi
185+
186+
# GraphQL pagination variables
187+
AFTER_CURSOR=null
188+
PER_PAGE=100
189+
174190
while true; do
175-
# List workflow runs.
176-
#
177-
# Note that github will return 404 once all runs are deleted.
178-
# We use `|| true` to ignore this error since it is expected when all runs are deleted.
179-
RUN_IDS=$(gh run list \
180-
--workflow "$workflow" \
181-
--limit 100 \
182-
--json databaseId \
183-
--jq '.[].databaseId' 2>/dev/null || true)
184-
185-
[ -z "$RUN_IDS" ] && break # stop when no runs remain
191+
# GraphQL query to fetch workflow runs
192+
QUERY=$(printf 'query { repository(owner: "%s", name: "%s") { workflow(id: "%s") { runs(first: 100, after: %s) { pageInfo { hasNextPage endCursor } nodes { databaseId } } } } }' \
193+
"$OWNER" "$REPO" "$WORKFLOW_ID" "$AFTER_CURSOR")
194+
RESPONSE=$(gh api graphql -f query="$QUERY")
195+
196+
RUN_IDS=$(echo "$RESPONSE" | jq -r '.data.repository.workflow.runs.nodes[].databaseId')
197+
[ -z "$RUN_IDS" ] && break
186198
187199
BATCH_COUNT=$(echo "$RUN_IDS" | wc -l | tr -d ' ')
188200
WORKFLOW_COUNT=$((WORKFLOW_COUNT + BATCH_COUNT))
@@ -201,22 +213,24 @@ jobs:
201213
done
202214
fi
203215
204-
# TEMPORARY BREAK for testing
205-
if [ "$WORKFLOW_COUNT" -gt 20 ]; then
206-
echo " ⚠️ Temporary break: workflow count exceeded 20, stopping early for safety."
216+
# Prepare next page
217+
AFTER_CURSOR=$(echo "$RESPONSE" | jq -r '.data.repository.workflow.runs.pageInfo.endCursor')
218+
HAS_NEXT=$(echo "$RESPONSE" | jq -r '.data.repository.workflow.runs.pageInfo.hasNextPage')
219+
[ "$HAS_NEXT" != "true" ] && break
220+
221+
# TEMPORARY break for testing
222+
if [ "$WORKFLOW_COUNT" -gt 200 ]; then
223+
echo " ⚠️ Temporary break: workflow count exceeded 200, stopping early for safety."
207224
break
208225
fi
209226
done
210227
211-
# Save summary
212228
SUMMARY+=("$workflow|$WORKFLOW_COUNT")
213229
done
214230
215231
# === Summary Header ===
216-
printf " \n"
217232
print_header "Workflow Cleanup Summary"
218233
219-
# Summary table uses the same total width as progress table
220234
printf "%*s | %-*s | %-*s | %-*s\n" \
221235
"$INDEX_WIDTH" "" \
222236
"$MAX_WF_LENGTH" "Workflow" \
@@ -229,7 +243,7 @@ jobs:
229243
"$WORKFLOW_COUNT_WIDTH" "$(printf '%.0s-' $(seq 1 $WORKFLOW_COUNT_WIDTH))" \
230244
"$GLOBAL_TOTAL_WIDTH" "$(printf '%.0s-' $(seq 1 $GLOBAL_TOTAL_WIDTH))"
231245
232-
# Print final summary table
246+
# Print summary rows
233247
for entry in "${SUMMARY[@]}"; do
234248
wf="${entry%%|*}"
235249
count="${entry##*|}"
@@ -254,8 +268,6 @@ jobs:
254268
"$WORKFLOW_COUNT_WIDTH" "$TOTAL_AFFECTED" \
255269
"$GLOBAL_TOTAL_WIDTH" ""
256270
257-
# Final mode message
258-
printf " \n"
259271
if [ "$MODE" = "dry run" ]; then
260272
echo "Dry run complete. No runs were deleted."
261273
else

0 commit comments

Comments
 (0)