Skip to content

Commit

Permalink
do not merge directly if we cannot enable auto-merge (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
meorphis authored Jul 24, 2024
1 parent 0d3bf54 commit 188096b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 60 deletions.
4 changes: 2 additions & 2 deletions __snapshots__/github.ts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,7 @@ export class GitHub {
async enablePullRequestAutoMerge(
pullRequestNumber: number,
mergeMethod: MergeMethod
): Promise<'auto-merged' | 'direct-merged' | 'none'> {
): Promise<'auto-merged' | 'none'> {
try {
this.logger.debug('Enable PR auto-merge');
const prId = await this.queryPullRequestId(pullRequestNumber);
Expand All @@ -2381,15 +2381,9 @@ export class GitHub {
)
) {
this.logger.debug(
'PR can be merged directly, do it instead of via GitHub auto-merge'
'Auto-merge cannot be enabled - user probably has auto-merge disabled for their repo'
);
await this.octokit.pulls.merge({
owner: this.repository.owner,
repo: this.repository.repo,
pull_number: pullRequestNumber,
merge_method: mergeMethod,
});
return 'direct-merged';
return 'none';
} else {
throw e;
}
Expand Down
42 changes: 15 additions & 27 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,22 +1121,18 @@ export class Manifest {
}
);

let directlyMerged = false;
const autoMerge = this.pullRequestAutoMergeOption(pullRequest);
if (autoMerge) {
const result = await this.github.enablePullRequestAutoMerge(
await this.github.enablePullRequestAutoMerge(
newPullRequest.number,
autoMerge.mergeMethod
);
directlyMerged = result === 'direct-merged';
}

if (!directlyMerged) {
this.github.addPullRequestReviewers({
pullRequestNumber: newPullRequest.number,
reviewers: this.reviewers,
});
}
this.github.addPullRequestReviewers({
pullRequestNumber: newPullRequest.number,
reviewers: this.reviewers,
});

return newPullRequest;
}
Expand Down Expand Up @@ -1169,22 +1165,18 @@ export class Manifest {
}
);

let directlyMerged = false;
const autoMerge = this.pullRequestAutoMergeOption(pullRequest);
if (autoMerge) {
const result = await this.github.enablePullRequestAutoMerge(
await this.github.enablePullRequestAutoMerge(
updatedPullRequest.number,
autoMerge.mergeMethod
);
directlyMerged = result === 'direct-merged';
}

if (!directlyMerged) {
this.github.addPullRequestReviewers({
pullRequestNumber: updatedPullRequest.number,
reviewers: this.reviewers,
});
}
this.github.addPullRequestReviewers({
pullRequestNumber: updatedPullRequest.number,
reviewers: this.reviewers,
});

return updatedPullRequest;
}
Expand Down Expand Up @@ -1215,22 +1207,18 @@ export class Manifest {
// TODO: consider leaving the snooze label
await this.github.removeIssueLabels([SNOOZE_LABEL], snoozed.number);

let directlyMerged = false;
const autoMerge = this.pullRequestAutoMergeOption(pullRequest);
if (autoMerge) {
const result = await this.github.enablePullRequestAutoMerge(
await this.github.enablePullRequestAutoMerge(
updatedPullRequest.number,
autoMerge.mergeMethod
);
directlyMerged = result === 'direct-merged';
}

if (!directlyMerged) {
this.github.addPullRequestReviewers({
pullRequestNumber: updatedPullRequest.number,
reviewers: this.reviewers,
});
}
this.github.addPullRequestReviewers({
pullRequestNumber: updatedPullRequest.number,
reviewers: this.reviewers,
});

return updatedPullRequest;
}
Expand Down
18 changes: 5 additions & 13 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ describe('GitHub', () => {
req.done();
});

it('merges release PR directly when an auto-merge given but PR in "clean status"', async () => {
it('does nothing when an auto-merge given but PR in "clean status"', async () => {
const mutatePullRequestEnableAutoMergeStub = sandbox
.stub(github, <any>'mutatePullRequestEnableAutoMerge') // eslint-disable-line @typescript-eslint/no-explicit-any
.throws(
Expand Down Expand Up @@ -1312,14 +1312,10 @@ describe('GitHub', () => {
},
},
},
})
.put('/repos/fake/fake/pulls/123/merge', {
merge_method: 'rebase',
})
.reply(200);
});

const result = await github.enablePullRequestAutoMerge(123, 'rebase');
expect(result).to.equal('direct-merged');
expect(result).to.equal('none');
sinon.assert.calledOnce(mutatePullRequestEnableAutoMergeStub);
req.done();
});
Expand Down Expand Up @@ -1366,14 +1362,10 @@ describe('GitHub', () => {
},
},
},
})
.put('/repos/fake/fake/pulls/123/merge', {
merge_method: 'rebase',
})
.reply(200);
});

const result = await github.enablePullRequestAutoMerge(123, 'rebase');
expect(result).to.equal('direct-merged');
expect(result).to.equal('none');
sinon.assert.calledOnce(mutatePullRequestEnableAutoMergeStub);
req.done();
});
Expand Down
15 changes: 6 additions & 9 deletions test/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4995,7 +4995,7 @@ version = "3.0.0"
});
const enablePullRequestAutoMergeStub = sandbox
.stub(github, 'enablePullRequestAutoMerge')
.resolves('direct-merged');
.resolves('none');
const addPullRequestReviewersStub = sandbox
.stub(github, 'addPullRequestReviewers')
.resolves();
Expand Down Expand Up @@ -5228,8 +5228,7 @@ version = "3.0.0"

expect(enablePullRequestAutoMergeStub.callCount).to.equal(1);

// only called when not auto-merged
expect(addPullRequestReviewersStub.callCount).to.equal(5);
expect(addPullRequestReviewersStub.callCount).to.equal(6);
});

it('enables auto-merge when filters are provided (filters: only commit type, match-all)', async () => {
Expand All @@ -5246,7 +5245,7 @@ version = "3.0.0"
});
const enablePullRequestAutoMergeStub = sandbox
.stub(github, 'enablePullRequestAutoMerge')
.resolves('direct-merged');
.resolves('none');
const addPullRequestReviewersStub = sandbox
.stub(github, 'addPullRequestReviewers')
.resolves();
Expand Down Expand Up @@ -5418,8 +5417,7 @@ version = "3.0.0"
);

expect(enablePullRequestAutoMergeStub.callCount).to.equal(3);
// only called when not auto-merged
expect(addPullRequestReviewersStub.callCount).to.equal(1);
expect(addPullRequestReviewersStub.callCount).to.equal(4);
});

it('enables auto-merge when filters are provided (filters: build-patch-minor version bump, commit filters, match-at-least-one)', async () => {
Expand All @@ -5436,7 +5434,7 @@ version = "3.0.0"
});
const enablePullRequestAutoMergeStub = sandbox
.stub(github, 'enablePullRequestAutoMerge')
.resolves('direct-merged');
.resolves('none');
const addPullRequestReviewersStub = sandbox
.stub(github, 'addPullRequestReviewers')
.resolves();
Expand Down Expand Up @@ -5755,8 +5753,7 @@ version = "3.0.0"
);

expect(enablePullRequestAutoMergeStub.callCount).to.equal(4);
// only called when not auto-merged
expect(addPullRequestReviewersStub.callCount).to.equal(3);
expect(addPullRequestReviewersStub.callCount).to.equal(7);
});

it('updates an existing pull request', async () => {
Expand Down

0 comments on commit 188096b

Please sign in to comment.