Skip to content

Commit

Permalink
fix: try to prevent infinite rescheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Dec 7, 2023
1 parent 3286b4c commit 176054f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/context/repoContext.ts
Expand Up @@ -85,7 +85,7 @@ export type EventsWithRepository = CustomExtract<
// | 'workflow_run.in_progress'
>;

type RescheduleTime = 'long+timeout' | 'short';
export type RescheduleTime = 'long+timeout' | 'short';

interface RepoContextWithoutTeamContext {
appContext: AppContext;
Expand Down Expand Up @@ -189,8 +189,7 @@ async function initRepoContext<

if (res) {
if (
!res.settings ||
!res.settings.lastUpdated ||
!res.settings?.lastUpdated ||
isSettingsLastUpdatedExpired(res.settings)
) {
const repoSettingsResult = await getRepositorySettings(context);
Expand Down Expand Up @@ -417,6 +416,8 @@ async function initRepoContext<
repoContext,
reviewflowPrContext,
user,
undefined,
time,
);
} else {
const didMerge = await autoMergeIfPossibleLegacy(
Expand Down
47 changes: 36 additions & 11 deletions src/events/pr-handlers/actions/enableGithubAutoMerge.ts
@@ -1,6 +1,7 @@
import type {
EventsWithRepository,
RepoContext,
RescheduleTime,
} from '../../../context/repoContext';
import { checkIfUserIsBot } from '../../../utils/github/isBotUser';
import type { AutoMergeRequest } from '../../../utils/github/pullRequest/autoMerge';
Expand Down Expand Up @@ -35,6 +36,7 @@ export const mergeOrEnableGithubAutoMerge = async <
reviewflowPrContext: ReviewflowPrContext,
user?: BasicUser,
skipCheckMergeableState?: boolean,
fromRescheduleTime?: RescheduleTime,
): Promise<MergeOrEnableGithubAutoMergeResult> => {
if (pullRequest.merged_at || pullRequest.draft) {
return {
Expand Down Expand Up @@ -90,17 +92,40 @@ export const mergeOrEnableGithubAutoMerge = async <
!('mergeable_state' in pullRequest) ||
pullRequest.mergeable_state === 'unknown'
) {
// GitHub is determining whether the pull request is mergeable
await repoContext.reschedule(
context,
createMergeLockPrFromPr(pullRequest),
'short',
user,
);
return {
wasMerged: false,
isRescheduled: true,
};
if (!fromRescheduleTime || fromRescheduleTime === 'short') {
const rescheduleTime =
fromRescheduleTime === 'short' ? 'long+timeout' : 'short';
context.log.info(
`mergeOrEnableGithubAutomerge mergeable_state is ${
'mergeable_state' in pullRequest
? pullRequest.mergeable_state
: '[missing]'
}, rescheduling with ${rescheduleTime}`,
);
// GitHub is determining whether the pull request is mergeable
await repoContext.reschedule(
context,
createMergeLockPrFromPr(pullRequest),
rescheduleTime,
user,
);
return {
wasMerged: false,
isRescheduled: true,
};
} else {
context.log.info(
`mergeOrEnableGithubAutomerge mergeable_state is ${
'mergeable_state' in pullRequest
? pullRequest.mergeable_state
: '[missing]'
}, give up on rescheduling`,
);
return {
wasMerged: false,
isRescheduled: false,
};
}
}

let triedToMerge = false;
Expand Down

0 comments on commit 176054f

Please sign in to comment.