Skip to content

Commit 73149fd

Browse files
committed
update clean-workflows [skip ci]
1 parent d314ecf commit 73149fd

File tree

1 file changed

+81
-7
lines changed

1 file changed

+81
-7
lines changed

.github/workflows/clean-workflows.yml

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ jobs:
3030
script: |
3131
const cancelled = await github.rest.actions.listWorkflowRunsForRepo({
3232
owner: context.repo.owner,
33-
per_page: 100,
33+
per_page: 50,
3434
repo: context.repo.repo,
3535
status: 'cancelled',
3636
});
3737
const skipped = await github.rest.actions.listWorkflowRunsForRepo({
3838
owner: context.repo.owner,
39-
per_page: 100,
39+
per_page: 50,
4040
repo: context.repo.repo,
4141
status: 'skipped',
4242
});
@@ -70,17 +70,19 @@ jobs:
7070
let response = await github.rest.actions.listWorkflowRuns({
7171
owner: context.repo.owner,
7272
page: page,
73-
per_page: 100,
73+
per_page: 50,
7474
repo: context.repo.repo,
7575
workflow_id: workflow
7676
});
77+
if (response.data.workflow_runs.length === 0) break;
7778
if (response.data.workflow_runs.length > 0) {
7879
for (const run of response.data.workflow_runs) {
7980
if (now - Date.parse(run.created_at) > ms_in_day * days_to_expiration) {
8081
runs_to_delete.push([run.id, run.name]);
8182
}
8283
}
8384
}
85+
if (response.data.workflow_runs.length < 50) break;
8486
}
8587
}
8688
for (const run of runs_to_delete) {
@@ -104,24 +106,96 @@ jobs:
104106
script: |
105107
const pages = 5;
106108
let runs_to_delete = [];
109+
// Get all workflows
110+
const { data: workflowsData } = await github.rest.actions.listRepoWorkflows({
111+
owner: context.repo.owner,
112+
repo: context.repo.repo
113+
});
114+
// Find workflow by name
115+
const workflowName = "Clean Workflow Runs";
116+
const workflow = workflowsData.workflows.find(w => w.name === workflowName);
117+
if (!workflow) {
118+
console.log(`❌ No workflow named ${workflowName} found.`);
119+
return;
120+
}
121+
const workflowId = workflow.id;
122+
console.log(`✅ Found workflow ID: ${workflowId}`);
123+
// Fetch workflow runs
107124
for (let page = 0; page < pages; page += 1) {
108-
let response = await github.rest.actions.listWorkflowRuns({
125+
const response = await github.rest.actions.listWorkflowRuns({
109126
owner: context.repo.owner,
127+
repo: context.repo.repo,
128+
workflow_id: workflowId, // Use dynamically fetched ID
110129
page: page,
111-
per_page: 100,
130+
per_page: 50,
131+
});
132+
if (response.data.workflow_runs.length === 0) break;
133+
if (response.data.workflow_runs.length > 0) {
134+
for (const run of response.data.workflow_runs) {
135+
runs_to_delete.push([run.id, run.name]);
136+
}
137+
}
138+
if (response.data.workflow_runs.length < 50) break;
139+
}
140+
// Delete workflow runs
141+
for (const run of runs_to_delete) {
142+
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}'.`);
143+
try {
144+
await github.rest.actions.deleteWorkflowRun({
145+
owner: context.repo.owner,
146+
repo: context.repo.repo,
147+
run_id: run[0]
148+
});
149+
} catch (error) {
150+
// ignore errors
151+
}
152+
}
153+
154+
- name: ✂ Remove Dependabot workflow runs
155+
uses: actions/github-script@v7 # https://github.com/actions/github-script
156+
with:
157+
github-token: ${{ secrets.GITHUB_TOKEN }}
158+
script: |
159+
const pages = 5;
160+
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({
178+
owner: context.repo.owner,
112179
repo: context.repo.repo,
113-
workflow_id: 'clean-workflows.yml'
180+
workflow_id: workflowId, // Use dynamically fetched ID
181+
page: page,
182+
per_page: 50,
114183
});
184+
if (response.data.workflow_runs.length === 0) break;
115185
if (response.data.workflow_runs.length > 0) {
116186
for (const run of response.data.workflow_runs) {
187+
if (run.triggering_actor?.login === 'dependabot[bot]') {
117188
runs_to_delete.push([run.id, run.name]);
189+
}
118190
}
119191
}
192+
if (response.data.workflow_runs.length < 50) break;
120193
}
194+
// Delete workflow runs
121195
for (const run of runs_to_delete) {
122196
console.log(`[Deleting] Run id ${run[0]} of '${run[1]}'.`);
123197
try {
124-
await github.actions.deleteWorkflowRun({
198+
await github.rest.actions.deleteWorkflowRun({
125199
owner: context.repo.owner,
126200
repo: context.repo.repo,
127201
run_id: run[0]

0 commit comments

Comments
 (0)