From cf02691ce67529b56224d4c5afece8e3c31f660f Mon Sep 17 00:00:00 2001 From: rickstaa Date: Sun, 16 Oct 2022 12:15:53 +0200 Subject: [PATCH] ci: fix stale closer action review sort bug This commit makes sure the PR staleness is checked by the latest review that is done. --- scripts/close-stale-theme-prs.js | 26 +++++++++++++++++++------- scripts/helpers.js | 1 - scripts/preview-theme.js | 1 - 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/scripts/close-stale-theme-prs.js b/scripts/close-stale-theme-prs.js index 8c14c1e3ce471..fedcc3a14b667 100644 --- a/scripts/close-stale-theme-prs.js +++ b/scripts/close-stale-theme-prs.js @@ -9,19 +9,28 @@ import github from "@actions/github"; import { RequestError } from "@octokit/request-error"; import { getGithubToken, getRepoInfo } from "./helpers.js"; -// Script parameters const CLOSING_COMMENT = ` \rThis PR has been automatically closed due to inactivity. Please feel free to reopen it if you need to continue working on it.\ \rThank you for your contributions. `; +const REVIEWER = "github-actions[bot]"; + +/** + * Retrieve the review user. + * @returns {string} review user. + */ +const getReviewer = () => { + return process.env.REVIEWER ? process.env.REVIEWER : REVIEWER; +}; /** * Fetch open PRs from a given repository. * @param user The user name of the repository owner. * @param repo The name of the repository. + * @param reviewer The reviewer to filter by. * @returns The open PRs. */ -export const fetchOpenPRs = async (octokit, user, repo) => { +export const fetchOpenPRs = async (octokit, user, repo, reviewer) => { const openPRs = []; let hasNextPage = true; let endCursor; @@ -49,9 +58,9 @@ export const fetchOpenPRs = async (octokit, user, repo) => { name } } - reviews(first: 1, states: CHANGES_REQUESTED, author: "github-actions[bot]") { + reviews(first: 100, states: CHANGES_REQUESTED, author: "${reviewer}") { nodes { - updatedAt + submittedAt } } } @@ -99,11 +108,13 @@ const isStale = (pullRequest, staleDays) => { pullRequest.commits.nodes[0].commit.pushedDate, ); if (pullRequest.reviews.nodes[0]) { - const lastReviewDate = new Date(pullRequest.reviews.nodes[0].updatedAt); + const lastReviewDate = new Date( + pullRequest.reviews.nodes.sort((a, b) => (a < b ? 1 : -1))[0].submittedAt, + ); const lastUpdateDate = lastCommitDate >= lastReviewDate ? lastCommitDate : lastReviewDate; const now = new Date(); - return now - lastUpdateDate > 1000 * 60 * 60 * 24 * staleDays; + return (now - lastUpdateDate) / (1000 * 60 * 60 * 24) >= staleDays; } else { return false; } @@ -120,10 +131,11 @@ const run = async () => { debug("Creating octokit client..."); const octokit = github.getOctokit(getGithubToken()); const { owner, repo } = getRepoInfo(github.context); + const reviewer = getReviewer(); // Retrieve all theme pull requests. debug("Retrieving all theme pull requests..."); - const prs = await fetchOpenPRs(octokit, owner, repo); + const prs = await fetchOpenPRs(octokit, owner, repo, reviewer); const themePRs = pullsWithLabel(prs, "themes"); const invalidThemePRs = pullsWithLabel(themePRs, "invalid"); debug("Retrieving stale theme PRs..."); diff --git a/scripts/helpers.js b/scripts/helpers.js index 3fc5fc7d00eee..bc9621e47b72e 100644 --- a/scripts/helpers.js +++ b/scripts/helpers.js @@ -4,7 +4,6 @@ import { getInput } from "@actions/core"; -// Script variables. const OWNER = "anuraghazra"; const REPO = "github-readme-stats"; diff --git a/scripts/preview-theme.js b/scripts/preview-theme.js index 0dc3da253d22a..9fbe07c41b8c8 100644 --- a/scripts/preview-theme.js +++ b/scripts/preview-theme.js @@ -16,7 +16,6 @@ import { isValidHexColor } from "../src/common/utils.js"; import { themes } from "../themes/index.js"; import { getGithubToken, getRepoInfo } from "./helpers.js"; -// Script variables. const COMMENTER = "github-actions[bot]"; const COMMENT_TITLE = "Automated Theme Preview";