From 756a97fb23df04ebaccd391f4e44635c31a0bf83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Wed, 26 Apr 2023 19:57:35 +0200 Subject: [PATCH] Fix isFirstIssue() logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also use /search API for finding first issue, as previous upstream implementation had issues when author had only done PRs recently. Fixes actions/first-interaction#233 Signed-off-by: Benjamin Cabé --- lib/main.js | 21 ++++++++++----------- src/main.ts | 25 ++++++++++++------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/lib/main.js b/lib/main.js index 148ce89..fbcccac 100644 --- a/lib/main.js +++ b/lib/main.js @@ -99,19 +99,18 @@ function run() { } function isFirstIssue(client, sender, curIssueNumber) { return __awaiter(this, void 0, void 0, function* () { - const { status, data: issues } = yield client.rest.issues.listForRepo(Object.assign(Object.assign({}, github.context.repo), { creator: sender, state: 'all' })); - if (status !== 200) { - throw new Error(`Received unexpected API status code ${status}`); - } - if (issues.length === 0) { - return true; + // get the issue details + const { status: getIssueStatus, data: issue } = yield client.rest.issues.get(Object.assign(Object.assign({}, github.context.repo), { issue_number: curIssueNumber })); + if (getIssueStatus !== 200) { + throw new Error(`Received unexpected API status code ${getIssueStatus}`); } - for (const issue of issues) { - if (issue.number < curIssueNumber && !issue.pull_request) { - return false; - } + let query = `repo:${github.context.repo.owner}/${github.context.repo.repo} author:${sender} created:<=${issue.created_at} type:issue`; + const { status: searchStatus, data: searchResults } = yield client.rest.search.issuesAndPullRequests({ q: query }); + if (searchStatus !== 200) { + throw new Error(`Received unexpected API status code ${searchStatus}`); } - return true; + // If current issue is the user's first, there should be exactly one result + return searchResults.total_count === 1; }); } function isFirstOpenedOrMergedPR(client, sender, curPullNumber, closed) { diff --git a/src/main.ts b/src/main.ts index 362dbae..cdc59ce 100644 --- a/src/main.ts +++ b/src/main.ts @@ -96,27 +96,26 @@ async function isFirstIssue( sender: string, curIssueNumber: number ): Promise { - const {status, data: issues} = await client.rest.issues.listForRepo({ + // get the issue details + const {status: getIssueStatus, data: issue} = await client.rest.issues.get({ ...github.context.repo, - creator: sender, - state: 'all' + issue_number: curIssueNumber }); - if (status !== 200) { - throw new Error(`Received unexpected API status code ${status}`); + if (getIssueStatus !== 200) { + throw new Error(`Received unexpected API status code ${getIssueStatus}`); } - if (issues.length === 0) { - return true; - } + let query = `repo:${github.context.repo.owner}/${github.context.repo.repo} author:${sender} created:<=${issue.created_at} type:issue`; - for (const issue of issues) { - if (issue.number < curIssueNumber && !issue.pull_request) { - return false; - } + const {status: searchStatus, data: searchResults} = await client.rest.search.issuesAndPullRequests({ q: query }); + + if (searchStatus !== 200) { + throw new Error(`Received unexpected API status code ${searchStatus}`); } - return true; + // If current issue is the user's first, there should be exactly one result + return searchResults.total_count === 1; } async function isFirstOpenedOrMergedPR(