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 6 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
2 changes: 1 addition & 1 deletion src/handlers/assign/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const closePullRequestForAnIssue = async (): Promise<void> => {
const payload = context.payload as Payload;
if (!payload.issue?.number) return;

const prs = await getOpenedPullRequestsForAnIssue(payload.issue.number, "");
const prs = await getOpenedPullRequestsForAnIssue(payload.issue.number, "", "all");
if (!prs.length) return;
logger.info(`Opened prs for this issue: ${JSON.stringify(prs)}`);
let comment = `These linked pull requests are closed: `;
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/wildcard/unassign.ts
Original file line number Diff line number Diff line change
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, "ready");
kamaalsultan marked this conversation as resolved.
Show resolved Hide resolved

if (pullRequest.length > 0) {
const reviewRequests = await getReviewRequests(context, pullRequest[0].number, payload.repository.owner.login, payload.repository.name);
Expand Down Expand Up @@ -106,7 +106,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], "all");
const pr = openedPrsForIssue.length > 0 ? openedPrsForIssue[0] : undefined;
// get last commit and last comment on the linked pr
if (pr) {
Expand Down
36 changes: 25 additions & 11 deletions src/helpers/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,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: "draft" | "ready" | "all") => {
const pulls = await getOpenedPullRequests(userName, state);

return pulls.filter((pull) => {
if (!pull.body) return false;
Expand All @@ -630,23 +630,37 @@ export const getOpenedPullRequestsForAnIssue = async (issueNumber: number, userN
});
};

export const getOpenedPullRequests = async (username: string) => {
export const getOpenedPullRequests = async (username: string, state: "ready" | "draft" | "all") => {
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 === "ready" ? !pr.draft : state === "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 = [];
let fetchDone = false;
while (!fetchDone) {
const response = await context.octokit.rest.pulls.listCommits({
owner: payload.repository.owner.login,
repo: payload.repository.name,
pull_number: pullNumber,
per_page: 100,
0xcodercrane marked this conversation as resolved.
Show resolved Hide resolved
page: curPage,
});
await checkRateLimitGit(response.headers);
kamaalsultan marked this conversation as resolved.
Show resolved Hide resolved
allCommits.push(...response.data);
if (response.data.length < perPage) {
fetchDone = true;
return allCommits;
0xcodercrane marked this conversation as resolved.
Show resolved Hide resolved
} else curPage++;
}
return allCommits;
} catch (e: unknown) {
logger.debug(`Fetching pull request commits failed!, reason: ${e}`);
return [];
Expand All @@ -660,7 +674,7 @@ export const getAvailableOpenedPullRequests = async (username: string) => {
} = await getBotConfig();
if (!timeRangeForMaxIssueEnabled) return [];

const opened_prs = await getOpenedPullRequests(username);
const opened_prs = await getOpenedPullRequests(username, "ready");

const result = [];

Expand Down