Skip to content

Commit

Permalink
Fix isFirstIssue() logic
Browse files Browse the repository at this point in the history
Also use /search API for finding first issue, as previous upstream
implementation had issues when author had only done PRs recently.
Fixes actions#233

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
  • Loading branch information
kartben authored and stephanosio committed Apr 27, 2023
1 parent 4920091 commit a771855
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
21 changes: 10 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "first-interaction-action",
"version": "1.1.1-zephyr-1",
"version": "1.1.1-zephyr-2",
"description": "An action for greeting first time contributors.",
"main": "lib/main.js",
"scripts": {
Expand Down
25 changes: 12 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,26 @@ async function isFirstIssue(
sender: string,
curIssueNumber: number
): Promise<boolean> {
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(
Expand Down

0 comments on commit a771855

Please sign in to comment.