From a885ee3b3eced90b28ab731841be880fe1e5bb7d Mon Sep 17 00:00:00 2001 From: Olivier Fournier <66017789+tgdfool2@users.noreply.github.com> Date: Tue, 26 Oct 2021 20:57:26 +0200 Subject: [PATCH] Add new LOOKBACK_DAYS variable (#15) --- README.md | 1 + github.js | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4495763..0023f67 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ The dashboard has all of it's parameters passed via environment variables. - GITHUB_APP_CLIENTSECRET - The client secret from the GitHub App general settings page. - GITHUB_APP_INSTALLATIONID - Installation id that can be retrieved using steps in the next section. - GITHUB_APP_WEBHOOK_SECRET - Optional. If you don't supply the dashboard will not setup webhooks and only update every 15 minutes. +- LOOKBACK_DAYS - Optional, defaults to 7. Number of days to look in the past for workflow runs. - DEBUG=action-dashboard:\* - Optional setting to help in debugging ### Installation Id diff --git a/github.js b/github.js index da5032b..a81a425 100644 --- a/github.js +++ b/github.js @@ -22,6 +22,7 @@ const _privateKey = Buffer.from(process.env.GITHUB_APP_PRIVATEKEY || "", "base64 const _clientId = process.env.GITHUB_APP_CLIENTID; const _clientSecret = process.env.GITHUB_APP_CLIENTSECRET; const _installationId = process.env.GITHUB_APP_INSTALLATIONID; +const _lookbackDays = process.env.LOOKBACK_DAYS || 7 // Cache all workflows to speed up refresh let _runs = []; @@ -98,13 +99,13 @@ GitHub.getUsage = async function getUsage(repoOwner, repoName, workflowId, run_i GitHub.getMostRecentRuns = async function getMostRecentRuns(repoOwner, repoName, workflowId) { try { - const sevenDaysAgo = dayjs().subtract(7, 'day'); + const daysAgo = dayjs().subtract(_lookbackDays, 'day'); const runs = await octokit.paginate(octokit.actions.listWorkflowRuns, { repo: repoName, owner: repoOwner, workflow_id: workflowId }); if (runs.length > 0) { const groupedRuns = _.groupBy(runs, 'head_branch') const rows = _.reduce(groupedRuns, (result, runs, branch) => { debug(`branch`, branch); - if (sevenDaysAgo.isBefore(dayjs(runs[0].created_at))) { + if (daysAgo.isBefore(dayjs(runs[0].created_at))) { debug(`adding run.id: ${runs[0].id}`); result.push({ runId: runs[0].id, @@ -231,4 +232,4 @@ if (!process.env.DOCKER_BUILD) { setInterval(GitHub.refreshRuns, 1000 * 60 * 15); } -module.exports = GitHub; \ No newline at end of file +module.exports = GitHub;