forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

automerge.rs112 lines, 4.5 KiBHistoryRawDownload
1//! Auto-merge: a pull request with auto-merge enabled merges by itself, as the user who enabled
2//! it, the moment its merge gate passes.
3//!
4//! The gate (`pulls_merge::merge_gate`) is re-evaluated whenever something that can clear it
5//! arrives: a commit status (the token API, CI job completion) or a review. A new push to the head
6//! branch disables auto-merge (see `pulls_sync`). Nothing here polls; every call is a reaction to
7//! an event.
8
9use forge_core::{Action, IssueState, PullRequest, Repo, authorize};
10
11use crate::merge::{MergeOutcome, merge_gate, perform_merge};
12use crate::state::AppState;
13
14/// Re-evaluates every open pull request of `repo_id` that has auto-merge on and whose head is
15/// `sha`. Called after a commit status for `sha` was written. Failures are logged: the status
16/// that triggered this has already been stored.
17pub async fn on_status(state: &AppState, repo_id: &str, sha: &str) {
18 crate::live::bump_head(state, repo_id, sha).await;
19 let pulls = match state.store.auto_merge_pulls_for_head(repo_id, sha).await {
20 Ok(pulls) => pulls,
21 Err(e) => {
22 tracing::error!(error = %e, repo_id, "auto-merge: listing pull requests failed");
23 return;
24 }
25 };
26 if pulls.is_empty() {
27 return;
28 }
29 let repo = match state.store.repo_by_id(repo_id).await {
30 Ok(Some(repo)) => repo,
31 Ok(None) => return,
32 Err(e) => {
33 tracing::error!(error = %e, repo_id, "auto-merge: loading repo failed");
34 return;
35 }
36 };
37 for pr in pulls {
38 evaluate_logged(state, &repo, &pr).await;
39 }
40}
41
42/// Re-evaluates one pull request, reloaded from the store. Called after a review arrived.
43pub async fn on_pull(state: &AppState, repo: &Repo, number: i64) {
44 match state.store.pull_by_number(&repo.id, number).await {
45 Ok(Some(pr)) => evaluate_logged(state, repo, &pr).await,
46 Ok(None) => {}
47 Err(e) => {
48 tracing::error!(error = %e, repo_id = %repo.id, number, "auto-merge: loading pull failed");
49 }
50 }
51}
52
53async fn evaluate_logged(state: &AppState, repo: &Repo, pr: &PullRequest) {
54 if let Err(e) = Box::pin(evaluate(state, repo, pr)).await {
55 tracing::error!(error = %e, repo_id = %repo.id, number = pr.issue.number, "auto-merge: evaluation failed");
56 }
57 // The evaluation may have merged the pull or left it as it was; either way its page re-reads.
58 crate::live::bump_pull(state, &repo.id, &pr.issue.id);
59}
60
61/// Merges `pr` when auto-merge is on and nothing blocks it any more. Returns whether it merged.
62///
63/// The merge runs as the user who enabled auto-merge, and only while that user still has Write on
64/// the repo; otherwise auto-merge is switched off with a timeline notice. A merge the repository
65/// refuses (a conflict, a moved base) also switches it off, so it never retries forever.
66pub async fn evaluate(state: &AppState, repo: &Repo, pr: &PullRequest) -> forge_core::Result<bool> {
67 let Some(auto) = pr.auto_merge.as_ref() else {
68 return Ok(false);
69 };
70 if pr.merged_at.is_some() || pr.issue.state != IssueState::Open || repo.archived {
71 return Ok(false);
72 }
73 if pr.draft || pr.head_repo_id.is_none() {
74 return Ok(false);
75 }
76 // A branch that requires the merge queue takes no direct merges, auto-merge included.
77 if crate::merge_queue::requires_queue(state, repo, &pr.base_branch).await? {
78 return Ok(false);
79 }
80 if merge_gate(state, repo, pr).await?.blocked().is_some() {
81 return Ok(false);
82 }
83 let Some(user) = state.store.user_by_id(&auto.user_id).await? else {
84 state
85 .store
86 .disable_auto_merge(&pr.issue.id, None, "permission")
87 .await?;
88 return Ok(false);
89 };
90 let collaborator = state.store.collaborator_role(&repo.id, &user.id).await?;
91 let org = state
92 .store
93 .org_repo_role(&repo.owner_id, &user.id, &repo.id)
94 .await?;
95 if authorize(Some(&user), repo, collaborator, org, Action::Write).is_err() {
96 state
97 .store
98 .disable_auto_merge(&pr.issue.id, None, "permission")
99 .await?;
100 return Ok(false);
101 }
102 match perform_merge(state, repo, pr, &user, auto.style, None, None).await? {
103 MergeOutcome::Merged { .. } => Ok(true),
104 MergeOutcome::Refused { .. } => {
105 state
106 .store
107 .disable_auto_merge(&pr.issue.id, Some(&user.id), "refused")
108 .await?;
109 Ok(false)
110 }
111 }
112}