Skip to content

Commit 372de67

Browse files
committed
refactor: streamline Dependabot workflow run cleanup process (filter by username)
1 parent c7c40a8 commit 372de67

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

.github/workflows/clean-workflows.yml

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -158,49 +158,41 @@ jobs:
158158
script: |
159159
const pages = 5;
160160
let runs_to_delete = [];
161-
// Get all workflows
162-
const { data: workflowsData } = await github.rest.actions.listRepoWorkflows({
163-
owner: context.repo.owner,
164-
repo: context.repo.repo
165-
});
166-
// Find workflow by name
167-
const workflowName = "Dependabot Updates";
168-
const workflow = workflowsData.workflows.find(w => w.name === workflowName);
169-
if (!workflow) {
170-
console.log(`❌ No workflow named ${workflowName} found.`);
171-
return;
172-
}
173-
const workflowId = workflow.id;
174-
console.log(`✅ Found workflow ID: ${workflowId}`);
175-
// Fetch workflow runs
176-
for (let page = 0; page < pages; page += 1) {
177-
const response = await github.rest.actions.listWorkflowRuns({
161+
console.log(`🔍 Scanning for workflow runs triggered by User 'dependabot[bot]'...`);
162+
// 1. List ALL workflow runs in the repo, filtered by actor
163+
for (let page = 1; page <= pages; page++) {
164+
const response = await github.rest.actions.listWorkflowRunsForRepo({
178165
owner: context.repo.owner,
179166
repo: context.repo.repo,
180-
workflow_id: workflowId, // Use dynamically fetched ID
181167
page: page,
182168
per_page: 50,
169+
actor: 'dependabot[bot]'
183170
});
184-
if (response.data.workflow_runs.length === 0) break;
185-
if (response.data.workflow_runs.length > 0) {
186-
for (const run of response.data.workflow_runs) {
187-
if (run.triggering_actor?.login === 'dependabot[bot]') {
188-
runs_to_delete.push([run.id, run.name]);
189-
}
190-
}
171+
if (!response.data.workflow_runs || response.data.workflow_runs.length === 0) {
172+
break;
173+
}
174+
for (const run of response.data.workflow_runs) {
175+
runs_to_delete.push([run.id, run.name, run.workflow_id]);
191176
}
177+
// If we got fewer than 50, we are on the last page
192178
if (response.data.workflow_runs.length < 50) break;
193179
}
194-
// Delete workflow runs
195-
for (const run of runs_to_delete) {
196-
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}'.`);
180+
if (runs_to_delete.length === 0) {
181+
console.log(`✅ No Dependabot workflow runs found.`);
182+
return;
183+
}
184+
console.log(`🗑️ Found ${runs_to_delete.length} runs to delete.`);
185+
// 2. Delete workflow runs
186+
for (const [runId, runName, workflowId] of runs_to_delete) {
187+
console.log(`[Deleting] Run #${runId} (${runName})`);
197188
try {
198189
await github.rest.actions.deleteWorkflowRun({
199190
owner: context.repo.owner,
200191
repo: context.repo.repo,
201-
run_id: run[0]
192+
run_id: runId
202193
});
203194
} catch (error) {
204-
// ignore errors
195+
console.warn(`⚠️ Failed to delete run ${runId}: ${error.message}`);
205196
}
206-
}
197+
}
198+
console.log(`✅ Cleanup complete.`);

0 commit comments

Comments
 (0)