Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use github automerge as experimental feature
- Loading branch information
1 parent
668ed28
commit ac14990
Showing
10 changed files
with
449 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/events/pr-handlers/actions/enableGithubAutoMerge.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { EventsWithRepository, RepoContext } from 'context/repoContext'; | ||
import type { AutoMergeRequest } from '../../../utils/github/pullRequest/autoMerge'; | ||
import { | ||
enableGithubAutoMergeMutation, | ||
disableGithubAutoMergeMutation, | ||
} from '../../../utils/github/pullRequest/autoMerge'; | ||
import type { ProbotEvent } from '../../probot-types'; | ||
import type { PullRequestWithDecentData } from '../utils/PullRequestData'; | ||
import type { ReviewflowPrContext } from '../utils/createPullRequestContext'; | ||
import { createCommitMessage } from './autoMergeIfPossible'; | ||
import { parseBody } from './utils/body/parseBody'; | ||
|
||
export const enableGithubAutoMerge = async < | ||
EventName extends EventsWithRepository, | ||
>( | ||
pullRequest: PullRequestWithDecentData, | ||
context: ProbotEvent<EventName>, | ||
repoContext: RepoContext, | ||
reviewflowPrContext: ReviewflowPrContext, | ||
login?: string, | ||
): Promise<AutoMergeRequest | null> => { | ||
// TODO prevent merge when statuses are failing (see autoMergeIfPossible) => add in reviewflow status check | ||
|
||
const parsedBody = parseBody( | ||
reviewflowPrContext.commentBody, | ||
repoContext.config.prDefaultOptions, | ||
); | ||
const options = parsedBody?.options || repoContext.config.prDefaultOptions; | ||
|
||
const [commitHeadline, commitBody] = createCommitMessage({ | ||
pullRequest, | ||
parsedBody, | ||
options, | ||
}); | ||
|
||
try { | ||
/* Conditions: | ||
Allow auto-merge enabled in settings. | ||
The pull request base must have a branch protection rule with at least one requirement enabled. | ||
The pull request must be in a state where requirements have not yet been satisfied. If the pull request can already be merged, attempting to enable auto-merge will fail. | ||
*/ | ||
const response = await enableGithubAutoMergeMutation(context, { | ||
pullRequestId: pullRequest.node_id, | ||
mergeMethod: 'SQUASH', | ||
commitHeadline, | ||
commitBody, | ||
}); | ||
return response.enablePullRequestAutoMerge.pullRequest.autoMergeRequest; | ||
} catch (err) { | ||
context.log.error( | ||
'Could not enable automerge', | ||
context.repo({ | ||
issue_number: pullRequest.number, | ||
}), | ||
err, | ||
); | ||
context.octokit.issues.createComment( | ||
context.repo({ | ||
issue_number: pullRequest.number, | ||
body: `${login ? `@${login} ` : ''}Could not enable automerge`, | ||
}), | ||
); | ||
} | ||
return null; | ||
}; | ||
|
||
export const disableGithubAutoMerge = async < | ||
EventName extends EventsWithRepository, | ||
>( | ||
pullRequest: PullRequestWithDecentData, | ||
context: ProbotEvent<EventName>, | ||
repoContext: RepoContext, | ||
reviewflowPrContext: ReviewflowPrContext, | ||
login?: string, | ||
): Promise<boolean> => { | ||
try { | ||
/* Conditions: | ||
Allow auto-merge enabled in settings. | ||
The pull request base must have a branch protection rule with at least one requirement enabled. | ||
The pull request must be in a state where requirements have not yet been satisfied. If the pull request can already be merged, attempting to enable auto-merge will fail. | ||
*/ | ||
const response = await disableGithubAutoMergeMutation(context, { | ||
pullRequestId: pullRequest.node_id, | ||
}); | ||
return ( | ||
response.disablePullRequestAutoMerge.pullRequest.autoMergeRequest === null | ||
); | ||
} catch (err) { | ||
context.log.error( | ||
'Could not disable automerge', | ||
context.repo({ | ||
issue_number: pullRequest.number, | ||
}), | ||
err, | ||
); | ||
context.octokit.issues.createComment( | ||
context.repo({ | ||
issue_number: pullRequest.number, | ||
body: `${login ? `@${login} ` : ''}Could not disable automerge`, | ||
}), | ||
); | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { Probot } from 'probot'; | ||
import type { AppContext } from '../../context/AppContext'; | ||
import { checkIfIsThisBot } from '../../utils/github/isBotUser'; | ||
import { updatePrCommentBodyOptions } from './actions/updatePrCommentBody'; | ||
import { syncLabels } from './actions/utils/syncLabel'; | ||
import { createPullRequestHandler } from './utils/createPullRequestHandler'; | ||
|
||
export default function autoMergeChangedHandler( | ||
app: Probot, | ||
appContext: AppContext, | ||
): void { | ||
createPullRequestHandler( | ||
app, | ||
appContext, | ||
'pull_request.auto_merge_enabled', | ||
(payload, context, repoContext) => { | ||
if (repoContext.shouldIgnore) return null; | ||
|
||
if (checkIfIsThisBot(payload.sender)) { | ||
// ignore from this bot | ||
return null; | ||
} | ||
|
||
return payload.pull_request; | ||
}, | ||
async (pullRequest, context, repoContext, reviewflowPrContext) => { | ||
const autoMergeLabel = repoContext.labels['merge/automerge']; | ||
|
||
await Promise.all([ | ||
reviewflowPrContext && | ||
(await updatePrCommentBodyOptions( | ||
context, | ||
repoContext, | ||
reviewflowPrContext, | ||
{ | ||
autoMerge: true, | ||
}, | ||
)), | ||
syncLabels(pullRequest, context, [ | ||
{ | ||
shouldHaveLabel: true, | ||
label: autoMergeLabel, | ||
}, | ||
]), | ||
]); | ||
}, | ||
); | ||
createPullRequestHandler( | ||
app, | ||
appContext, | ||
'pull_request.auto_merge_disabled', | ||
(payload, context, repoContext) => { | ||
if (repoContext.shouldIgnore) return null; | ||
return payload.pull_request; | ||
}, | ||
async (pullRequest, context, repoContext, reviewflowPrContext) => { | ||
const autoMergeLabel = repoContext.labels['merge/automerge']; | ||
|
||
await Promise.all([ | ||
reviewflowPrContext && | ||
(await updatePrCommentBodyOptions( | ||
context, | ||
repoContext, | ||
reviewflowPrContext, | ||
{ | ||
autoMerge: false, | ||
}, | ||
)), | ||
syncLabels(pullRequest, context, [ | ||
{ | ||
shouldHaveLabel: false, | ||
label: autoMergeLabel, | ||
}, | ||
]), | ||
]); | ||
}, | ||
); | ||
} |
Oops, something went wrong.