Skip to content

Commit

Permalink
chore: lowered complexity for updatePullRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Aug 14, 2024
1 parent 3d653df commit a4ab6dc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/helpers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ export function parseGitHubUrl(url: string) {
}

export type IssueParams = ReturnType<typeof parseGitHubUrl>;
export interface Requirements {
mergeTimeout: string;
requiredApprovalCount: number;
}

/**
* Gets the merge timeout depending on the status of the assignee. If there are multiple assignees with different
* statuses, the longest timeout is chosen.
*/
export async function getMergeTimeoutAndApprovalRequiredCount(context: Context, authorAssociation: string) {
export async function getMergeTimeoutAndApprovalRequiredCount(context: Context, authorAssociation: string): Promise<Requirements> {
const timeoutCollaborator = {
mergeTimeout: context.config.mergeTimeout.collaborator,
requiredApprovalCount: context.config.approvalsRequired.collaborator,
Expand Down
40 changes: 29 additions & 11 deletions src/helpers/update-pull-requests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RestEndpointMethodTypes } from "@octokit/rest";
import ms from "ms";
import { getAllTimelineEvents } from "../handlers/github-events";
import { Context } from "../types";
Expand All @@ -7,8 +8,10 @@ import {
getOpenPullRequests,
getPullRequestDetails,
isCiGreen,
IssueParams,
mergePullRequest,
parseGitHubUrl,
Requirements,
} from "./github";

type IssueEvent = {
Expand Down Expand Up @@ -51,17 +54,10 @@ export async function updatePullRequests(context: Context) {
context.logger.debug(
`Requirements according to association ${pullRequestDetails.author_association}: ${JSON.stringify(requirements)} with last activity date: ${lastActivityDate}`
);
if (isNaN(lastActivityDate.getTime()) || isPastOffset(lastActivityDate, requirements.mergeTimeout)) {
if ((await getApprovalCount(context, gitHubUrl)) >= requirements.requiredApprovalCount) {
if (await isCiGreen(context, pullRequestDetails.head.sha, gitHubUrl)) {
context.logger.info(`Pull-request ${html_url} is past its due date (${requirements.mergeTimeout} after ${lastActivityDate}), will merge.`);
await mergePullRequest(context, gitHubUrl);
} else {
context.logger.info(`Pull-request ${html_url} (sha: ${pullRequestDetails.head.sha}) does not pass all CI tests, won't merge.`);
}
} else {
context.logger.info(`Pull-request ${html_url} does not have sufficient reviewer approvals to be merged.`);
}
if (isNaN(lastActivityDate.getTime())) {
context.logger.info(`PR ${html_url} does not seem to have any activity, nothing to do.`);
} else if (isPastOffset(lastActivityDate, requirements.mergeTimeout)) {
await attemptMerging(context, { gitHubUrl, htmlUrl: html_url, requirements, lastActivityDate, pullRequestDetails });
} else {
context.logger.info(`PR ${html_url} has activity up until (${lastActivityDate}), nothing to do.`);
}
Expand All @@ -71,6 +67,28 @@ export async function updatePullRequests(context: Context) {
}
}

async function attemptMerging(
context: Context,
data: {
gitHubUrl: IssueParams;
htmlUrl: string;
requirements: Requirements;
lastActivityDate: Date;
pullRequestDetails: RestEndpointMethodTypes["pulls"]["get"]["response"]["data"];
}
) {
if ((await getApprovalCount(context, data.gitHubUrl)) >= data.requirements.requiredApprovalCount) {
if (await isCiGreen(context, data.pullRequestDetails.head.sha, data.gitHubUrl)) {
context.logger.info(`Pull-request ${data.htmlUrl} is past its due date (${data.requirements.mergeTimeout} after ${data.lastActivityDate}), will merge.`);
await mergePullRequest(context, data.gitHubUrl);
} else {
context.logger.info(`Pull-request ${data.htmlUrl} (sha: ${data.pullRequestDetails.head.sha}) does not pass all CI tests, won't merge.`);
}
} else {
context.logger.info(`Pull-request ${data.htmlUrl} does not have sufficient reviewer approvals to be merged.`);
}
}

function isPastOffset(lastActivityDate: Date, offset: string): boolean {
const currentDate = new Date();
const offsetTime = ms(offset);
Expand Down

0 comments on commit a4ab6dc

Please sign in to comment.