diff --git a/src/handlers/wildcard/unassign.ts b/src/handlers/wildcard/unassign.ts index ec057ce73..e9b8c05dc 100644 --- a/src/handlers/wildcard/unassign.ts +++ b/src/handlers/wildcard/unassign.ts @@ -9,7 +9,7 @@ import { listAllIssuesForRepo, removeAssignees, } from "../../helpers"; -import { Comment, Issue, IssueType, Payload, UserType } from "../../types"; +import { Comment, Issue, IssueType, Payload, PullRequestState, UserType } from "../../types"; import { deadLinePrefix } from "../shared"; /** @@ -52,7 +52,7 @@ const checkBountyToUnassign = async (issue: Issue): Promise => { const curTimestamp = new Date().getTime(); const lastActivity = await lastActivityTime(issue, comments); const passedDuration = curTimestamp - lastActivity.getTime(); - const pullRequest = await getOpenedPullRequestsForAnIssue(issue.number, issue.assignee.login); + const pullRequest = await getOpenedPullRequestsForAnIssue(issue.number, issue.assignee.login, PullRequestState.READY); if (pullRequest.length > 0) { const reviewRequests = await getReviewRequests(context, pullRequest[0].number, payload.repository.owner.login, payload.repository.name); @@ -110,7 +110,7 @@ const lastActivityTime = async (issue: Issue, comments: Comment[]): Promise 0) activities.push(new Date(lastCommentsOfHunterForIssue[0].created_at)); - const openedPrsForIssue = await getOpenedPullRequestsForAnIssue(issue.number, assignees[0]); + const openedPrsForIssue = await getOpenedPullRequestsForAnIssue(issue.number, assignees[0], PullRequestState.ALL); const pr = openedPrsForIssue.length > 0 ? openedPrsForIssue[0] : undefined; // get last commit and last comment on the linked pr if (pr) { diff --git a/src/helpers/issue.ts b/src/helpers/issue.ts index c3cc89972..245bbaf8e 100644 --- a/src/helpers/issue.ts +++ b/src/helpers/issue.ts @@ -1,6 +1,6 @@ import { Context } from "probot"; import { getBotConfig, getBotContext, getLogger } from "../bindings"; -import { AssignEvent, Comment, IssueType, Payload, StreamlinedComment, UserType } from "../types"; +import { AssignEvent, Comment, IssueType, Payload, PullRequestState, StreamlinedComment, UserType } from "../types"; import { checkRateLimitGit } from "../utils"; export const getAllIssueEvents = async () => { @@ -668,8 +668,8 @@ export const getAssignedIssues = async (username: string) => { return assigned_issues; }; -export const getOpenedPullRequestsForAnIssue = async (issueNumber: number, userName: string) => { - const pulls = await getOpenedPullRequests(userName); +export const getOpenedPullRequestsForAnIssue = async (issueNumber: number, userName: string, state: PullRequestState) => { + const pulls = await getOpenedPullRequests(userName, state); return pulls.filter((pull) => { if (!pull.body) return false; @@ -683,10 +683,12 @@ export const getOpenedPullRequestsForAnIssue = async (issueNumber: number, userN }); }; -export const getOpenedPullRequests = async (username: string) => { +export const getOpenedPullRequests = async (username: string, state: PullRequestState) => { const context = getBotContext(); const prs = await getAllPullRequests(context, "open"); - return prs.filter((pr) => !pr.draft && (pr.user?.login === username || !username)); + return prs.filter( + (pr) => (state === PullRequestState.READY ? !pr.draft : state === PullRequestState.DRAFT ? pr.draft : true) && (pr.user?.login === username || !username) + ); }; export const getCommitsOnPullRequest = async (pullNumber: number) => { @@ -694,12 +696,24 @@ export const getCommitsOnPullRequest = async (pullNumber: number) => { const context = getBotContext(); const payload = getBotContext().payload as Payload; try { - const { data: commits } = await context.octokit.rest.pulls.listCommits({ - owner: payload.repository.owner.login, - repo: payload.repository.name, - pull_number: pullNumber, - }); - return commits; + const perPage = 100; + let curPage = 1; + const allCommits = []; + + // eslint-disable-next-line no-constant-condition + while (true) { + const response = await context.octokit.rest.pulls.listCommits({ + owner: payload.repository.owner.login, + repo: payload.repository.name, + pull_number: pullNumber, + per_page: perPage, + page: curPage, + }); + allCommits.push(...response.data); + if (response.data.length < perPage) { + return allCommits; + } else curPage++; + } } catch (e: unknown) { logger.debug(`Fetching pull request commits failed! reason: ${e}`); return []; @@ -713,7 +727,7 @@ export const getAvailableOpenedPullRequests = async (username: string) => { } = await getBotConfig(); if (!timeRangeForMaxIssueEnabled) return []; - const opened_prs = await getOpenedPullRequests(username); + const opened_prs = await getOpenedPullRequests(username, PullRequestState.READY); const result = []; diff --git a/src/types/payload.ts b/src/types/payload.ts index 404991852..61a007b1c 100644 --- a/src/types/payload.ts +++ b/src/types/payload.ts @@ -48,6 +48,12 @@ export enum StateReason { REOPENED = "reopened", } +export enum PullRequestState { + DRAFT = "draft", + READY = "ready", + ALL = "all", +} + const UserSchema = Type.Object({ login: Type.String(), id: Type.Number(),