Skip to content

Commit

Permalink
Refactor locking logic to check the last comment date (#100)
Browse files Browse the repository at this point in the history
- Added `fetchLastComment` function to retrieve the last comment of an issue
- Updated `run` function in `lock.ts` to use `fetchLastComment` to check the date of the last comment before adding a new comment
- Improved logic to determine if there is an active discussion before adding a new comment

Signed-off-by: Yarden Shoham <[email protected]>
Co-authored-by: silverwind <[email protected]>
  • Loading branch information
yardenshoham and silverwind authored Aug 1, 2023
1 parent 91b1ec2 commit e86156f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ translation files changed, directing the user to the crowdin project.
### Locks

The script will also lock issues and pull requests that have been closed for 3
months. If the issue was updated in the last two weeks, a comment will be posted
suggesting opening a new issue to continue the discussion.
months. If the issue was commented on in the last two weeks, a comment will be
posted suggesting opening a new issue to continue the discussion.

## Usage

Expand Down
11 changes: 11 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,14 @@ export const fetchClosedOldIssuesAndPRs = async (before: Date) => {
const json = await response.json();
return json;
};

// returns the last comment of the given issue
export const fetchLastComment = async (issueNumber: number) => {
const response = await fetch(
`${GITHUB_API}/repos/go-gitea/gitea/issues/${issueNumber}/comments?per_page=1&sort=created&direction=desc`,
{ headers: HEADERS },
);
const json = await response.json();
if (!json.length) return null;
return json[0];
};
25 changes: 24 additions & 1 deletion src/github_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import {
assertEquals,
assertFalse,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import {
fetchBranch,
fetchLastComment,
fetchPr,
fetchPrFileNames,
getPrReviewers,
Expand Down Expand Up @@ -117,3 +121,22 @@ Deno.test("fetchPrFileNames() can handle big PRs", async () => {
const aPrWith669Files = await fetchPrFileNames(24147);
assertEquals(aPrWith669Files.size, 669);
});

Deno.test("fetchLastComment() returns the appropriate comment", async () => {
const prToLastComment = {
10: "Closing as fixed by #199 ",
29: null,
1000: "LGTM",
10000:
"It is a feature of SQL databases. The repo id is stored in the database and uses auto increment:\r\nhttps://www.w3schools.com/sql/sql_autoincrement.asp",
};
await Promise.all(
Object.entries(prToLastComment).map(
async ([issueNumber, comment]) => {
const result = await fetchLastComment(Number(issueNumber));
if (!comment) return assertFalse(result);
assertEquals(result.body, comment);
},
),
);
});
17 changes: 14 additions & 3 deletions src/lock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { addComment, fetchClosedOldIssuesAndPRs, lockIssue } from "./github.ts";
import {
addComment,
fetchClosedOldIssuesAndPRs,
fetchLastComment,
lockIssue,
} from "./github.ts";

const MILLISECONDS_IN_A_DAY = 1000 * 60 * 60 * 24;

Expand All @@ -18,8 +23,14 @@ export const run = async () => {
) => {
const lockedSuccessfully = await lockIssue(issue.number, "resolved");

// if the issue was updated in the two weeks, we add a comment
if (lockedSuccessfully && new Date(issue.updated_at) > twoWeeksAgo) {
const lastComment = await fetchLastComment(issue.number);
let activeDiscussion = false;
if (lastComment) {
activeDiscussion = new Date(lastComment.created_at) > twoWeeksAgo;
}

// if the issue was commented on in the two weeks, we add a comment
if (lockedSuccessfully && activeDiscussion) {
await addComment(
issue.number,
`We lock ${
Expand Down

0 comments on commit e86156f

Please sign in to comment.