From c35f2438427555b3fd04d38e9cfc6940fc12a8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20W=C3=B3dkiewicz?= Date: Sat, 2 Apr 2022 01:06:56 +0200 Subject: [PATCH] feat: add include_drafts filter --- README.md | 6 ++++++ action.yml | 6 +++++- dist/filters.js | 6 +++++- dist/index.js | 8 ++++---- dist/input.js | 2 ++ src/filters.ts | 6 ++++++ src/index.ts | 21 +++++++++++++-------- src/input.ts | 4 ++++ 8 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index da4c464..f7c37e8 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ The stats will only include PRs between the date `days_back` days ago and now. D Default: 30 +### `include_drafts` + +If set to `true`, the stats will include draft PRs. + +Default: `false` + ## Example usage ``` diff --git a/action.yml b/action.yml index 158dcfc..9d30c2a 100644 --- a/action.yml +++ b/action.yml @@ -9,12 +9,16 @@ inputs: description: "Your workflow GITHUB_TOKEN secret" required: true days_back: - description: "How far back should the calculations go, considering PR creation date (default: 30)" + description: "Number of days defining the earliest PR creation date to be included in the calculations" required: false default: 30 labels_to_ignore: description: "PR with these labels will be ignored. Values delimited with commas" required: false + include_drafts: + desription: "If set to true, draft PRs will be included in the calculations" + required: false + default: false branding: icon: git-pull-request color: purple diff --git a/dist/filters.js b/dist/filters.js index 8b5d8ac..a715923 100644 --- a/dist/filters.js +++ b/dist/filters.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.labelFilter = exports.dateFilter = void 0; +exports.draftFilter = exports.labelFilter = exports.dateFilter = void 0; function dateFilter(daysBack) { const ms = daysBack * 24 * 60 * 60 * 1000; const thresholdDate = new Date(Date.now() - ms); @@ -15,3 +15,7 @@ function labelFilter(labelsToIgnore) { }; } exports.labelFilter = labelFilter; +function draftFilter(includeDrafts) { + return (p) => (includeDrafts ? true : !p.draft); +} +exports.draftFilter = draftFilter; diff --git a/dist/index.js b/dist/index.js index 4c33775..5181838 100644 --- a/dist/index.js +++ b/dist/index.js @@ -46,7 +46,7 @@ const input_1 = require("./input"); function main() { var e_1, _a; return __awaiter(this, void 0, void 0, function* () { - const { daysBack, labelsToIgnore, token, overrideOwner, overrideRepo } = (0, input_1.parseInput)(); + const { daysBack, includeDrafts, labelsToIgnore, overrideOwner, overrideRepo, token, } = (0, input_1.parseInput)(); const octokit = github.getOctokit(token); const pulls = []; try { @@ -60,8 +60,9 @@ function main() { const response = _c.value; const pullsBatch = response.data; const pullsAfterStartDate = pullsBatch.filter((0, filters_1.dateFilter)(daysBack)); - const pullsToConsider = pullsAfterStartDate.filter((0, filters_1.labelFilter)(labelsToIgnore)); - pulls.push(...pullsToConsider); + pulls.push(...pullsAfterStartDate + .filter((0, filters_1.labelFilter)(labelsToIgnore)) + .filter((0, filters_1.draftFilter)(includeDrafts))); if (pullsAfterStartDate.length < pullsBatch.length) { break; } @@ -74,7 +75,6 @@ function main() { } finally { if (e_1) throw e_1.error; } } - core.debug(`pulls: ${JSON.stringify(pulls, null, undefined)}`); const pullsWithDurations = pulls .map(attachPullDurationMs) .sort((p1, p2) => p1[1] - p2[1]); diff --git a/dist/input.js b/dist/input.js index 3a09cee..7e06415 100644 --- a/dist/input.js +++ b/dist/input.js @@ -44,12 +44,14 @@ function parseInput() { .split(",") .map((x) => x.trim()) .filter(Boolean); + const includeDrafts = core.getBooleanInput("include_drafts"); const overrideOwner = core.getInput("override_owner") || undefined; const overrideRepo = core.getInput("override_repo") || undefined; return { token, daysBack, labelsToIgnore, + includeDrafts, overrideRepo, overrideOwner, }; diff --git a/src/filters.ts b/src/filters.ts index 1395a2a..d3e59aa 100644 --- a/src/filters.ts +++ b/src/filters.ts @@ -20,3 +20,9 @@ export function labelFilter( ); }; } + +export function draftFilter( + includeDrafts: Input["includeDrafts"] +): (pull: Pull) => boolean { + return (p: Pull) => (includeDrafts ? true : !p.draft); +} diff --git a/src/index.ts b/src/index.ts index bd05630..bf59fc1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,18 @@ import * as core from "@actions/core"; import * as github from "@actions/github"; -import { dateFilter, labelFilter } from "./filters"; +import { dateFilter, draftFilter, labelFilter } from "./filters"; import { parseInput } from "./input"; import { Pull } from "./types"; async function main() { - const { daysBack, labelsToIgnore, token, overrideOwner, overrideRepo } = - parseInput(); + const { + daysBack, + includeDrafts, + labelsToIgnore, + overrideOwner, + overrideRepo, + token, + } = parseInput(); const octokit = github.getOctokit(token); @@ -24,17 +30,16 @@ async function main() { )) { const pullsBatch: Pull[] = response.data; const pullsAfterStartDate = pullsBatch.filter(dateFilter(daysBack)); - const pullsToConsider = pullsAfterStartDate.filter( - labelFilter(labelsToIgnore) + pulls.push( + ...pullsAfterStartDate + .filter(labelFilter(labelsToIgnore)) + .filter(draftFilter(includeDrafts)) ); - pulls.push(...pullsToConsider); if (pullsAfterStartDate.length < pullsBatch.length) { break; } } - core.debug(`pulls: ${JSON.stringify(pulls, null, undefined)}`); - const pullsWithDurations = pulls .map(attachPullDurationMs) .sort((p1, p2) => p1[1] - p2[1]); diff --git a/src/input.ts b/src/input.ts index 3f0ff94..209326f 100644 --- a/src/input.ts +++ b/src/input.ts @@ -22,6 +22,8 @@ export function parseInput(): Input { .map((x) => x.trim()) .filter(Boolean); + const includeDrafts = core.getBooleanInput("include_drafts"); + const overrideOwner = core.getInput("override_owner") || undefined; const overrideRepo = core.getInput("override_repo") || undefined; @@ -29,6 +31,7 @@ export function parseInput(): Input { token, daysBack, labelsToIgnore, + includeDrafts, overrideRepo, overrideOwner, }; @@ -38,6 +41,7 @@ export interface Input { token: string; daysBack: number; labelsToIgnore: string[]; + includeDrafts: boolean; overrideRepo?: string; overrideOwner?: string; }