Skip to content

Commit

Permalink
fix: improves input parameter parser
Browse files Browse the repository at this point in the history
  • Loading branch information
igorjs committed Jul 1, 2022
1 parent abc1233 commit 1d5e599
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 56 deletions.
27 changes: 20 additions & 7 deletions .github/workflows/check.yml
Expand Up @@ -11,41 +11,54 @@ on:
- main
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'

jobs:
check-dist:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
if: github.event_name == 'pull_request'
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

- uses: actions/checkout@v2
if: github.event_name == 'push'
with:
fetch-depth: 0

- name: Set Node.js 17.x
- name: Set Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 17.x
node-version: 16.x

- name: Install dependencies and Rebuild the dist/ directory
run: |
npm ci
npm run build --if-present
- name: Compare the expected and actual dist/ directories
continue-on-error: true
id: diff
run: |
if [ "$(git diff --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
git diff
git diff --name-only
exit 1
fi
id: diff
# If index.js was different than expected, upload the expected version as an artifact
- name: Commit files # commit the new dist folder
if: ${{ failure() && steps.diff.conclusion == 'failure' }}
if: ${{ steps.diff.outcome == 'failure' }}
run: |
git config --local user.email "ci@github.com"
git config --local user.email "igorjs@users.noreply.github.com"
git config --local user.name "GitHub Action CI"
git add ./dist
git commit -m "chore(CI): update distribution"
git commit -m "chore(ci): update dist folder"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 1 addition & 6 deletions README.md
Expand Up @@ -7,11 +7,7 @@ Clean workflow run logs based on configuration
### Parameters
- token: The token to use to access the GitHub API (required)

- owner: The owner of the repository (required)

- repo: The name of the repository (required)

- days_old: The amount of days old to delete (default: '7')
- days_old: The amount of days old to delete (default: 7)

### Outputs

Expand Down Expand Up @@ -46,4 +42,3 @@ jobs:

1. The routine only deletes 100 workflows per run. There's an ongoing work to address [this issue](https://github.com/igorjs/gh-actions-clean-workflow/issues/22).

2. The parameter `days_old` isn't working as expected on the [v1](https://github.com/igorjs/gh-actions-clean-workflow/releases/tag/v1). This issue has been addressed on issue [#34](https://github.com/igorjs/gh-actions-clean-workflow/issues/34)
28 changes: 11 additions & 17 deletions action.yml
@@ -1,32 +1,26 @@
name: 'Clean Workflow Action'
name: "Clean Workflow Action"

description: 'Clean workflow run logs based on configuration'
description: "Clean workflow run logs based on configuration"

author: igor.js
author: igorjs

branding:
icon: 'chevron-right'
color: 'yellow'
icon: "chevron-right"
color: "yellow"

inputs:
token:
description: 'The token to use to access the GitHub API'
required: true
owner:
description: 'The owner of the repository'
required: true
repo:
description: 'The name of the repository'
description: "The token to use to access the GitHub API"
required: true
days_old:
description: 'The amount of days old to delete'
description: "The amount of days old to delete"
required: false
default: '7'
default: "7"

outputs:
result:
description: 'The number of workflows deleted'
description: "The number of workflows deleted"

runs:
using: 'node12'
main: 'dist/index.js'
using: "node16"
main: "dist/index.js"
35 changes: 35 additions & 0 deletions src/helpers/params.js
@@ -0,0 +1,35 @@
import { getInput } from "@actions/core";

function throwError(message) {
throw new Error(message);
}

export function getToken() {
const value = getInput("token", { required: true, trimWhitespace: true });

return value || throwError("[Invalid Parameter] A token must be provided");
}

export function getOwner() {
const value = getInput("owner", { required: false, trimWhitespace: true });

return value || process.env["GITHUB_REPOSITORY_OWNER"];
}

export function getRepo() {
const value = getInput("repo", { required: false, trimWhitespace: true });
const currentRepository = process.env["GITHUB_REPOSITORY"];

return value || currentRepository.slice(currentRepository.indexOf("/") + 1);
}

export function getDaysOld() {
const value = getInput("days_old", { required: false, trimWhitespace: true });
const numberValue = Number(value);

if (Number.isSafeInteger(numberValue) && !Number.isNaN(numberValue)) {
return numberValue;
}

return 7; // Default value
}
41 changes: 17 additions & 24 deletions src/index.js
@@ -1,18 +1,14 @@
import { getInput, info, setFailed, setOutput } from "@actions/core";
import { setFailed } from "@actions/core";
import { getOctokit } from "@actions/github";
import { dateDiff, calcTimeUnits } from "./dateutils.js";
import { dateDiff, calcTimeUnits } from "./utils/date.js";
import { getToken, getOwner, getRepo, getDaysOld } from "./helpers/params";

async function run() {
try {
const token = getInput("token", { required: true, trimWhitespace: true });
const owner = getInput("owner", { required: true, trimWhitespace: true });
const repo = getInput("repo", { required: true, trimWhitespace: true });
const days_old = getInput("days_old", {
required: false,
trimWhitespace: true,
});

const numDaysOldToBeDeleted = Number(days_old || 7);
const repo = getRepo("repo");
const owner = getOwner("owner");
const token = getToken("token");
const numDaysOldToBeDeleted = getDaysOld("days_old");

/**
* https://octokit.github.io/rest.js/v18
Expand All @@ -23,10 +19,6 @@ async function run() {
* We need to fetch the list of workflow runs for a particular repo.
* We use octokit.paginate() to automatically loop over all the pages of the results.
*/
// const workflows = await octokit.paginate(
// "GET /repos/:owner/:repo/actions/workflows",
// defaults
// );
const { data } = await octokit.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
Expand All @@ -41,30 +33,31 @@ async function run() {

const workflowRunsToDelete = data.workflow_runs.filter(hasRunBeforeDate);

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

if (workflowRunsToDelete.length > 0) {
/**
* Loop over all the WorkflowRuns and delete them.
**/
const deleteRunAction = ({ id }) => {
info(`Deleting workflow run #${id}`);
console.info(`Deleting workflow run #${id}`);

return octokit.rest.actions
.deleteWorkflowRun({ owner, repo, run_id: id })
.catch((err) => `An error occurrend: ${err.message}`);
};

const requests = await Promise.all(
const results = await Promise.all(
workflowRunsToDelete.map(deleteRunAction)
);

info(`${requests.length} workflow runs sucessfully deleted`);

setOutput(
"result",
`${requests.length} workflow runs sucessfully deleted`
);
if (results.length > 0) {
console.info(`${results.length} workflow runs sucessfully deleted`);
} else {
throw new Error(
`The action could not delete any workflows. Please review your parameters.`
);
}
}
} catch (error) {
setFailed(error.message);
Expand Down
4 changes: 2 additions & 2 deletions src/dateutils.js → src/utils/date.js
Expand Up @@ -4,7 +4,7 @@
* @return {Object|null} Reallocated time units. NULL on failure.
*/
export function allocateTimeUnits(ms) {
if (!Number.isInteger(ms)) {
if (!Number.isSafeInteger(ms)) {
return null;
}

Expand Down Expand Up @@ -39,7 +39,7 @@ export function allocateTimeUnits(ms) {
* @return {Object|null} Reallocated time units. NULL on failure.
*/
export function calcTimeUnits(ms) {
if (!Number.isInteger(ms)) {
if (!Number.isSafeInteger(ms)) {
return null;
}

Expand Down

0 comments on commit 1d5e599

Please sign in to comment.