Skip to content

Commit

Permalink
fix: implements pagination over 100 workflow runs
Browse files Browse the repository at this point in the history
  • Loading branch information
igorjs committed Jul 2, 2022
1 parent 8c87a3f commit cf571c9
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 177 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/clean.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Clean Logs

on:
schedule:
# Runs "At 00:00 UTC every day." (see https://crontab.guru)
- cron: "0 0 * * *"

workflow_dispatch:
inputs:
days_old:
Expand All @@ -12,9 +16,10 @@ jobs:
clean-logs:
runs-on: ubuntu-latest
steps:
- uses: igorjs/github-clean-workflow-action@main
- uses: actions/checkout@v2
- uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
days_old: ${{ github.event.inputs.days_old }}
# owner: ${{ github.repository_owner }}
# repo: ${{ github.event.repository.name }}
days_old: 0
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name: Unit Tests

on:
pull_request:
push:
branches:
- main
- 'releases/*'
pull_request:

jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 17
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 17
node-version: 16
- name: Run Tests
run: |
npm ci
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sourcemap-register.js

Large diffs are not rendered by default.

242 changes: 120 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions src/api.js

This file was deleted.

41 changes: 41 additions & 0 deletions src/helpers/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getOctokit } from "@actions/github";

export const getApi = ({ token, owner, repo }) => {
/**
* https://octokit.github.io/rest.js/v18
**/
const octokit = new getOctokit(token);

const deleteRunById = async (id) => {
console.info("Deleting workflow run #%d", id);

return octokit.rest.actions
.deleteWorkflowRun({ owner, repo, run_id: id })
.catch(() => false);
};

const deleteRuns = async (runs) => {
return await Promise.all(
runs.map((run) => deleteRunById(run.id)).filter(Boolean)
);
};

const listWorkflowRuns = async (status = "completed") => {
const workflowRuns = [];

for await (const results of octokit.paginate.iterator(
octokit.rest.actions.listWorkflowRunsForRepo,
{ owner, repo, status, per_page: 100 }
)) {
workflowRuns.push(...results.data);
}

return workflowRuns;
};

return {
deleteRuns,
deleteRunById,
listWorkflowRuns,
};
};
20 changes: 9 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setFailed } from "@actions/core";
import { getApiActions } from "./api.js";
import { dateDiff, calcTimeUnits } from "./utils/date.js";
import { getToken, getOwner, getRepo, getDaysOld } from "./helpers/params";
import { getToken, getOwner, getRepo, getDaysOld } from "./helpers/params.js";
import { getApi } from "./helpers/api.js";

async function run() {
try {
Expand All @@ -10,26 +10,24 @@ async function run() {
const token = getToken("token");
const numDaysOldToBeDeleted = getDaysOld("days_old");

const actions = getApiActions({ token, owner, repo });

const { data } = await actions.listWorkflowRunsForRepo();
const api = getApi({ token, owner, repo });

const hasRunBeforeDate = (run) => {
const diff = dateDiff(run.updated_at, Date.now());
return calcTimeUnits(diff).days >= numDaysOldToBeDeleted;
};

const workflowRunsToDelete = data.workflow_runs.filter(hasRunBeforeDate);
const workflowRuns = await api.listWorkflowRuns();

const workflowRunsToDelete = workflowRuns.filter(hasRunBeforeDate);

console.info(`${workflowRunsToDelete.length} workflow runs to be deleted`);
console.info("%d workflow runs to be deleted", workflowRunsToDelete.length);

if (workflowRunsToDelete.length > 0) {
const results = await Promise.all(
workflowRunsToDelete.map(actions.deleteRunAction)
);
const results = await api.deleteRuns(workflowRunsToDelete);

if (results.length > 0) {
console.info(`${results.length} workflow runs sucessfully deleted`);
console.info("%d workflow runs sucessfully deleted", results.length);
} else {
throw new Error(
`The action could not delete any workflows. Please review your parameters.`
Expand Down

0 comments on commit cf571c9

Please sign in to comment.