Skip to content

Commit

Permalink
feat: add include_drafts filter
Browse files Browse the repository at this point in the history
  • Loading branch information
akwodkiewicz authored Apr 1, 2022
2 parents f04f9e8 + c35f243 commit b35e94e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion dist/filters.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -15,3 +15,7 @@ function labelFilter(labelsToIgnore) {
};
}
exports.labelFilter = labelFilter;
function draftFilter(includeDrafts) {
return (p) => (includeDrafts ? true : !p.draft);
}
exports.draftFilter = draftFilter;
8 changes: 4 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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]);
Expand Down
2 changes: 2 additions & 0 deletions dist/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
6 changes: 6 additions & 0 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export function labelFilter(
);
};
}

export function draftFilter(
includeDrafts: Input["includeDrafts"]
): (pull: Pull) => boolean {
return (p: Pull) => (includeDrafts ? true : !p.draft);
}
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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]);
Expand Down
4 changes: 4 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ 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;

return {
token,
daysBack,
labelsToIgnore,
includeDrafts,
overrideRepo,
overrideOwner,
};
Expand All @@ -38,6 +41,7 @@ export interface Input {
token: string;
daysBack: number;
labelsToIgnore: string[];
includeDrafts: boolean;
overrideRepo?: string;
overrideOwner?: string;
}

0 comments on commit b35e94e

Please sign in to comment.