Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

fix: get all commits from pr #723

Open
wants to merge 16 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/handlers/wildcard/unassign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ const checkBountyToUnassign = async (issue: Issue): Promise<boolean> => {
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);
Expand Down Expand Up @@ -110,7 +110,7 @@ const lastActivityTime = async (issue: Issue, comments: Comment[]): Promise<Date

if (lastCommentsOfHunterForIssue.length > 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) {
Expand Down
38 changes: 26 additions & 12 deletions src/helpers/issue.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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;
Expand All @@ -683,23 +683,37 @@ 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) => {
const logger = getLogger();
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 [];
Expand All @@ -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 = [];

Expand Down
6 changes: 6 additions & 0 deletions src/types/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading