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

Commit

Permalink
Always refetch mergeable state
Browse files Browse the repository at this point in the history
  • Loading branch information
tibdex authored and autorebase[bot] committed Jan 15, 2019
1 parent 03533b2 commit abeebd9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
7 changes: 3 additions & 4 deletions src/app.test.ts
Expand Up @@ -621,9 +621,8 @@ describe("rebase failed", () => {
["payload", "pull_request", "mergeable_state"],
"dirty",
);
// Pretend that the pull request is actually mergeable so that Autorebase try to rebase it.
labeledEvent.payload.pull_request.mergeable = true;
labeledEvent.payload.pull_request.mergeable_state = "behind";
// Tell Autorebase to attempt rebasing regardless of the mergeable state.
return true;
},
],
}),
Expand All @@ -649,5 +648,5 @@ describe("rebase failed", () => {

const comment = await getLastIssueComment(pullRequestNumber);
expect(comment).toMatch(/The rebase failed/);
}, 40000);
}, 35000);
});
6 changes: 4 additions & 2 deletions src/app.ts
Expand Up @@ -5,7 +5,7 @@ import { LabelName } from "./utils";

type ActionHandler = (action: Action) => Promise<void>;

type EventHandler = (event: Event) => Promise<void>;
type EventHandler = (event: Event) => Promise<boolean | void>;

type Options = {
handleAction: ActionHandler;
Expand All @@ -31,12 +31,14 @@ const createApplicationFunction = (options: Options) => (app: Application) => {

// @ts-ignore The event is of the good type because Autorebase only subscribes to a subset of webhooks.
const event: Event = { name: context.name, payload: context.payload };
await options.handleEvent(event);
const handlerResult = await options.handleEvent(event);
const forceRebase = handlerResult === true;

let action;
try {
action = await autorebase({
event,
forceRebase,
label: options.label,
// @ts-ignore The value is the good one even if the type doesn't match.
octokit: context.github,
Expand Down
26 changes: 17 additions & 9 deletions src/autorebase.ts
Expand Up @@ -195,31 +195,35 @@ const findAndRebasePullRequestOnSameBase = async ({
};

const autorebasePullRequest = async ({
forceRebase,
label,
octokit,
owner,
pullRequest,
repo,
}: {
forceRebase: boolean;
label: LabelName;
octokit: Octokit;
owner: RepoOwner;
pullRequest: PullRequestInfo;
repo: RepoName;
}): Promise<Action> => {
debug("autorebasing pull request", { pullRequest });
const shouldBeAutosquashed = await needAutosquashing({
octokit,
owner,
pullRequestNumber: pullRequest.pullRequestNumber,
repo,
});
debug("should be autosquashed", {
pullRequestNumber: pullRequest.pullRequestNumber,
debug("autorebasing pull request", {
forceRebase,
pullRequest,
shouldBeAutosquashed,
});
const shouldBeRebased =
shouldBeAutosquashed || pullRequest.mergeableState === "behind";
forceRebase ||
shouldBeAutosquashed ||
pullRequest.mergeableState === "behind";
if (shouldBeRebased) {
return rebase({
label,
Expand All @@ -243,12 +247,14 @@ const autorebasePullRequest = async ({

const autorebase = async ({
event,
forceRebase,
label,
octokit,
owner,
repo,
}: {
event: Event;
forceRebase: boolean;
label: LabelName;
octokit: Octokit;
owner: RepoOwner;
Expand Down Expand Up @@ -307,13 +313,15 @@ const autorebase = async ({

if (event.name === "pull_request") {
if (
pullRequest.labeledAndOpenedAndRebaseable &&
(event.payload.action === "opened" ||
event.payload.action === "synchronize" ||
(event.payload.action === "labeled" &&
event.payload.label.name === label))
forceRebase ||
(pullRequest.labeledAndOpenedAndRebaseable &&
(event.payload.action === "opened" ||
event.payload.action === "synchronize" ||
(event.payload.action === "labeled" &&
event.payload.label.name === label)))
) {
return autorebasePullRequest({
forceRebase,
label,
octokit,
owner,
Expand Down
5 changes: 3 additions & 2 deletions src/tests-utils.ts
Expand Up @@ -193,7 +193,7 @@ const protectBranch = async ({
};
};

type Handler<T> = (arg: T) => Promise<void>;
type Handler<T> = (arg: T) => Promise<any>;

const waitForMockedHandlerCalls = <T>({
handler,
Expand All @@ -207,10 +207,11 @@ const waitForMockedHandlerCalls = <T>({
implementations.reduce(
(mock, implementation, index) =>
mock.mockImplementationOnce(async arg => {
await implementation(arg);
const result = await implementation(arg);
if (index === implementations.length - 1) {
resolve();
}
return result;
}),
initialMock,
);
Expand Down
20 changes: 7 additions & 13 deletions src/utils.ts
Expand Up @@ -152,19 +152,13 @@ const getPullRequestInfoWithKnownMergeableState = async ({
pullRequest: PullRequestPayload;
repo: RepoName;
}) => {
if (
isMergeableStateKnown(pullRequest) &&
// Sometimes, a webhook is sent with `mergeable_state: 'clean'` when the
// pull request actual mergeable state is `behind`.
// Making a request to the GitHub API to retrieve the pull request details
// will return the actual mergeable state.
// Thus, we don't try to see if the pull request passed as an argument
// has already a known mergeable state, we always ask the GitHub API for it.
pullRequest.mergeable_state !== "clean"
) {
return getPullRequestInfo({ label, pullRequest });
}

// Sometimes, a webhook is sent with `mergeable_state: 'clean'` when the
// pull request actual mergeable state is `behind`.
// Or with `mergeable_state: 'unstable'` when it's actually `behind` too.
// Making a request to the GitHub API to retrieve the pull request details
// will return the actual mergeable state.
// Thus, we don't try to see if the pull request passed as an argument
// has already a known mergeable state, we always ask the GitHub API for it.
const pullRequestWithKnownMergeableState = await waitForKnownMergeableState({
octokit,
owner,
Expand Down

0 comments on commit abeebd9

Please sign in to comment.