From e86156f6e700d782aefbb48bf081847beaacdfa4 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Tue, 1 Aug 2023 22:05:02 +0300 Subject: [PATCH] Refactor locking logic to check the last comment date (#100) - 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 Co-authored-by: silverwind --- README.md | 4 ++-- src/github.ts | 11 +++++++++++ src/github_test.ts | 25 ++++++++++++++++++++++++- src/lock.ts | 17 ++++++++++++++--- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0e0a091..a609d64 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/github.ts b/src/github.ts index 339ad9b..1eab989 100644 --- a/src/github.ts +++ b/src/github.ts @@ -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]; +}; diff --git a/src/github_test.ts b/src/github_test.ts index a325d4e..bfe2dee 100644 --- a/src/github_test.ts +++ b/src/github_test.ts @@ -1,6 +1,10 @@ -import { assertEquals } from "https://deno.land/std@0.189.0/testing/asserts.ts"; +import { + assertEquals, + assertFalse, +} from "https://deno.land/std@0.189.0/testing/asserts.ts"; import { fetchBranch, + fetchLastComment, fetchPr, fetchPrFileNames, getPrReviewers, @@ -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); + }, + ), + ); +}); diff --git a/src/lock.ts b/src/lock.ts index 599076c..3387831 100644 --- a/src/lock.ts +++ b/src/lock.ts @@ -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; @@ -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 ${