Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: fix stale closer action review sort bug #2201

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions scripts/close-stale-theme-prs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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...");
Expand Down
1 change: 0 additions & 1 deletion scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import { getInput } from "@actions/core";

// Script variables.
const OWNER = "anuraghazra";
const REPO = "github-readme-stats";

Expand Down
1 change: 0 additions & 1 deletion scripts/preview-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down