Skip to content

Commit

Permalink
Ignored labels (#20)
Browse files Browse the repository at this point in the history
We're planning to use Stale PR Finder to help manage the backlogs for
the open source project https://github.com/prebid, but we needed to
ability to flag certain PRs as excluded from being considered stale.
Added the ignoredLabels feature for your consideration.

Thank you.

---------

Co-authored-by: Javier Bullrich <javier@bullrich.dev>
  • Loading branch information
bretg and Bullrich authored Dec 9, 2024
1 parent 145b790 commit d27fadf
Show file tree
Hide file tree
Showing 6 changed files with 2,080 additions and 772 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ You can find all the inputs in [the action file](./action.yml) but let's walk th
- **Important**: If set be sure to connect the names by comma.
- Example: `feature,bug,good first issue`
- It is **not** _case sensitive_.
- `ignoredLabels`: Collections of labels separated by commas that should be ignored when searching for a PR.
- Short for `Ignore PRs with any of the required labels`.
- **optional**
- **Important**: If set be sure to connect the names by comma.
- Example: `feature,bug,good first issue`
- It is **not** _case sensitive_.

#### Accessing other repositories

Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ inputs:
required: false
description: Collections of labels separated by commas that should be required when searching for a PR.
type: string
ignoredLabels:
required: false
description: Collections of labels separated by commas that should be ignored when searching for a PR.
type: string
outputs:
repo:
description: 'The name of the repo in owner/repo pattern'
Expand All @@ -45,4 +49,4 @@ outputs:

runs:
using: 'docker'
image: 'docker://ghcr.io/paritytech/stale-pr-finder/action:0.2.0'
image: 'docker://ghcr.io/paritytech/stale-pr-finder/action:0.3.0'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stale-pr-finder",
"version": "0.2.0",
"version": "0.3.0",
"description": "GitHub action that finds stale PRs and produce an output with them",
"main": "src/index.ts",
"scripts": {
Expand Down
12 changes: 10 additions & 2 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export const olderThanDays = (pr: PullRequest, daysStale: number): boolean =>

export const byNoReviews = (pr: PullRequest): boolean => !pr.reviews || pr.reviews.length === 0;

export const byLabels = (pr: PullRequest, labels: string[]): boolean =>
export const withLabels = (pr: PullRequest, labels: string[]): boolean =>
pr.labels &&
pr.labels.map((l) => l.name.toLowerCase()).some((l) => labels.map((label) => label.toLowerCase()).includes(l));
pr.labels
.map((prl) => prl.name.toLowerCase())
.some((prl) => labels.map((label) => label.toLowerCase()).includes(prl));

export const withoutLabels = (pr: PullRequest, labels: string[]): boolean =>
pr.labels &&
!pr.labels
.map((prl) => prl.name.toLowerCase())
.some((prl) => labels.map((label) => label.toLowerCase()).includes(prl));
26 changes: 20 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { github } from "@eng-automation/integrations";
import { writeFile } from "fs";
import moment from "moment";

import { byLabels, byNoReviews, olderThanDays } from "./filters";
import { byNoReviews, olderThanDays, withLabels, withoutLabels } from "./filters";
import { getPullRequestWithReviews } from "./githubApi";
import { PullRequest, Repo } from "./types";

type Filters = { daysStale: number; noReviews: boolean; ignoreDrafts: boolean; requiredLabels: string[] };
type Filters = {
daysStale: number;
noReviews: boolean;
ignoreDrafts: boolean;
requiredLabels: string[];
ignoredLabels: string[];
};

const daysSinceDate = (date: string): number => moment().diff(moment(date), "days");

Expand All @@ -33,12 +39,18 @@ const getFiltersFromInput = (): Filters => {
const ignoreDrafts = getInput("ignoreDrafts") ? getBooleanInput("ignoreDrafts") : true;

let requiredLabels: string[] = [];
let ignoredLabels: string[] = [];
const labels = getInput("requiredLabels");
if (labels) {
requiredLabels = labels.split(",");
}

return { daysStale, noReviews, ignoreDrafts, requiredLabels };
const ignoredLabelsInput = getInput("ignoredLabels");
if (ignoredLabelsInput) {
ignoredLabels = ignoredLabelsInput.split(",");
}

return { daysStale, noReviews, ignoreDrafts, requiredLabels, ignoredLabels };
};

const generateMarkdownMessage = (prs: PullRequest[], repo: { owner: string; repo: string }) => {
Expand All @@ -64,7 +76,10 @@ const filterPRs = (prs: PullRequest[] | undefined, filters: Filters) => {
filteredData = filteredData.filter((pr) => !pr.draft);
}
if (filters.requiredLabels.length > 0) {
filteredData = filteredData.filter((fd) => byLabels(fd, filters.requiredLabels));
filteredData = filteredData.filter((fd) => withLabels(fd, filters.requiredLabels));
}
if (filters.ignoredLabels.length > 0) {
filteredData = filteredData.filter((fd) => withoutLabels(fd, filters.ignoredLabels));
}

return filteredData;
Expand Down Expand Up @@ -93,9 +108,8 @@ const runAction = async (ctx: Context) => {

const inputDays = Number.parseInt(getInput("days-stale", { required: false }));
const daysStale = isNaN(inputDays) ? 5 : inputDays;
const stale = isNaN(daysStale);
const outputFile = getInput("fileOutput", { required: false });
console.log("daysStale", daysStale, stale);
console.log("daysStale", daysStale);

const octokit = await github.getInstance({ authType: "token", authToken: token });
const prs = await getPullRequestWithReviews(octokit, repo);
Expand Down
Loading

0 comments on commit d27fadf

Please sign in to comment.