Skip to content

Commit 0d4f37c

Browse files
committed
Switch from bool to ForcePush enum
1 parent f3c51fa commit 0d4f37c

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

src/bors/handlers/trybuild.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::database::RunId;
1616
use crate::database::{BuildModel, BuildStatus, PullRequestModel};
1717
use crate::github::GithubRepoName;
1818
use crate::github::api::client::GithubRepositoryClient;
19+
use crate::github::api::operations::ForcePush;
1920
use crate::github::{CommitSha, GithubUser, LabelTrigger, MergeError, PullRequestNumber};
2021
use crate::permissions::PermissionType;
2122
use crate::utils::text::suppress_github_mentions;
@@ -146,7 +147,7 @@ async fn attempt_merge(
146147

147148
// First set the try branch to our base commit (either the selected parent or the main branch).
148149
client
149-
.set_branch_to_sha(TRY_MERGE_BRANCH_NAME, base_sha, true)
150+
.set_branch_to_sha(TRY_MERGE_BRANCH_NAME, base_sha, ForcePush::Yes)
150151
.await
151152
.map_err(|error| anyhow!("Cannot set try merge branch to {}: {error:?}", base_sha.0))?;
152153

@@ -177,7 +178,7 @@ async fn run_try_build(
177178
parent_sha: CommitSha,
178179
) -> anyhow::Result<()> {
179180
client
180-
.set_branch_to_sha(TRY_BRANCH_NAME, &commit_sha, true)
181+
.set_branch_to_sha(TRY_BRANCH_NAME, &commit_sha, ForcePush::Yes)
181182
.await
182183
.map_err(|error| anyhow!("Cannot set try branch to main branch: {error:?}"))?;
183184

src/bors/merge_queue.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
handlers::labels::handle_label_trigger,
1515
},
1616
database::{BuildStatus, MergeableState, PullRequestModel},
17-
github::{CommitSha, LabelTrigger, MergeError, api::client::GithubRepositoryClient},
17+
github::{CommitSha, LabelTrigger, MergeError, api::client::GithubRepositoryClient, api::operations::ForcePush},
1818
utils::sort_queue::sort_queue_prs,
1919
};
2020

@@ -80,7 +80,7 @@ pub async fn handle_merge_queue(ctx: Arc<BorsContext>) -> anyhow::Result<()> {
8080

8181
match repo
8282
.client
83-
.set_branch_to_sha(&pr.base_branch, &commit_sha, false)
83+
.set_branch_to_sha(&pr.base_branch, &commit_sha, ForcePush::No)
8484
.await
8585
{
8686
Ok(()) => {
@@ -202,7 +202,7 @@ async fn start_auto_build(
202202
MergeResult::Success(merge_sha) => {
203203
// 2. Push merge commit to `AUTO_BRANCH_NAME` where CI runs
204204
client
205-
.set_branch_to_sha(AUTO_BRANCH_NAME, &merge_sha, true)
205+
.set_branch_to_sha(AUTO_BRANCH_NAME, &merge_sha, ForcePush::Yes)
206206
.await?;
207207

208208
// 3. Record the build in the database
@@ -282,7 +282,7 @@ async fn attempt_merge(
282282

283283
// Reset auto merge branch to point to base branch
284284
client
285-
.set_branch_to_sha(AUTO_MERGE_BRANCH_NAME, base_sha, true)
285+
.set_branch_to_sha(AUTO_MERGE_BRANCH_NAME, base_sha, ForcePush::Yes)
286286
.await
287287
.map_err(|error| anyhow!("Cannot set auto merge branch to {}: {error:?}", base_sha.0))?;
288288

src/github/api/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::bors::{CheckSuite, CheckSuiteStatus, Comment};
99
use crate::config::{CONFIG_FILE_PATH, RepositoryConfig};
1010
use crate::database::RunId;
1111
use crate::github::api::base_github_html_url;
12-
use crate::github::api::operations::{MergeError, merge_branches, set_branch_to_commit};
12+
use crate::github::api::operations::{MergeError, merge_branches, set_branch_to_commit, ForcePush};
1313
use crate::github::{CommitSha, GithubRepoName, PullRequest, PullRequestNumber};
1414
use crate::utils::timing::{measure_network_request, perform_network_request_with_retry};
1515
use futures::TryStreamExt;
@@ -144,7 +144,7 @@ impl GithubRepositoryClient {
144144
&self,
145145
branch: &str,
146146
sha: &CommitSha,
147-
force: bool,
147+
force: ForcePush,
148148
) -> anyhow::Result<()> {
149149
perform_network_request_with_retry("set_branch_to_sha", || async {
150150
Ok(set_branch_to_commit(self, branch.to_string(), sha, force).await?)

src/github/api/operations.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ use thiserror::Error;
55
use crate::github::CommitSha;
66
use crate::github::api::client::GithubRepositoryClient;
77

8+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9+
pub enum ForcePush {
10+
Yes,
11+
No,
12+
}
13+
14+
impl From<ForcePush> for bool {
15+
fn from(force_push: ForcePush) -> bool {
16+
match force_push {
17+
ForcePush::Yes => true,
18+
ForcePush::No => false,
19+
}
20+
}
21+
}
22+
823
#[derive(Error, Debug)]
924
pub enum MergeError {
1025
#[error("Branch not found")]
@@ -94,7 +109,7 @@ pub async fn set_branch_to_commit(
94109
repo: &GithubRepositoryClient,
95110
branch_name: String,
96111
sha: &CommitSha,
97-
force: bool,
112+
force: ForcePush,
98113
) -> Result<(), BranchUpdateError> {
99114
// Fast-path: assume that the branch exists
100115
match update_branch(repo, branch_name.clone(), sha, force).await {
@@ -138,7 +153,7 @@ async fn update_branch(
138153
repo: &GithubRepositoryClient,
139154
branch_name: String,
140155
sha: &CommitSha,
141-
force: bool,
156+
force: ForcePush,
142157
) -> Result<(), BranchUpdateError> {
143158
let url = format!(
144159
"/repos/{}/git/refs/{}",
@@ -154,7 +169,7 @@ async fn update_branch(
154169
url.as_str(),
155170
Some(&serde_json::json!({
156171
"sha": sha.as_ref(),
157-
"force": force
172+
"force": bool::from(force)
158173
})),
159174
)
160175
.await?;

0 commit comments

Comments
 (0)