@@ -10,6 +10,7 @@ use crate::bors::Comment;
10
10
use crate :: bors:: RepositoryState ;
11
11
use crate :: database:: RunId ;
12
12
use crate :: database:: { BuildModel , BuildStatus , PullRequestModel , WorkflowStatus , WorkflowType } ;
13
+ use crate :: github:: api:: client:: GithubRepositoryClient ;
13
14
use crate :: github:: GithubRepoName ;
14
15
use crate :: github:: {
15
16
CommitSha , GithubUser , LabelTrigger , MergeError , PullRequest , PullRequestNumber ,
@@ -62,66 +63,94 @@ pub(super) async fn command_try_build(
62
63
}
63
64
} ;
64
65
66
+ match attempt_merge (
67
+ & repo. ci_client ,
68
+ & pr. head . sha ,
69
+ & base_sha,
70
+ & auto_merge_commit_message ( pr, repo. client . repository ( ) , "<try>" , jobs) ,
71
+ )
72
+ . await ?
73
+ {
74
+ MergeResult :: Success ( merge_sha) => {
75
+ // If the merge was succesful, run CI with merged commit
76
+ run_try_build ( & repo. ci_client , & db, pr_model, merge_sha. clone ( ) , base_sha) . await ?;
77
+
78
+ handle_label_trigger ( repo, pr. number , LabelTrigger :: TryBuildStarted ) . await ?;
79
+
80
+ repo. client
81
+ . post_comment ( pr. number , trying_build_comment ( & pr. head . sha , & merge_sha) )
82
+ . await
83
+ }
84
+ MergeResult :: Conflict => {
85
+ repo. client
86
+ . post_comment ( pr. number , merge_conflict_comment ( & pr. head . name ) )
87
+ . await
88
+ }
89
+ }
90
+ }
91
+
92
+ async fn attempt_merge (
93
+ ci_client : & GithubRepositoryClient ,
94
+ head_sha : & CommitSha ,
95
+ base_sha : & CommitSha ,
96
+ merge_message : & str ,
97
+ ) -> anyhow:: Result < MergeResult > {
65
98
tracing:: debug!( "Attempting to merge with base SHA {base_sha}" ) ;
66
99
67
100
// First set the try branch to our base commit (either the selected parent or the main branch).
68
- repo . client
69
- . set_branch_to_sha ( TRY_MERGE_BRANCH_NAME , & base_sha)
101
+ ci_client
102
+ . set_branch_to_sha ( TRY_MERGE_BRANCH_NAME , base_sha)
70
103
. await
71
104
. map_err ( |error| anyhow ! ( "Cannot set try merge branch to {}: {error:?}" , base_sha. 0 ) ) ?;
72
105
73
106
// Then merge the PR commit into the try branch
74
- match repo
75
- . client
76
- . merge_branches (
77
- TRY_MERGE_BRANCH_NAME ,
78
- & pr. head . sha ,
79
- & auto_merge_commit_message ( pr, repo. client . repository ( ) , "<try>" , jobs) ,
80
- )
107
+ match ci_client
108
+ . merge_branches ( TRY_MERGE_BRANCH_NAME , head_sha, merge_message)
81
109
. await
82
110
{
83
111
Ok ( merge_sha) => {
84
112
tracing:: debug!( "Merge successful, SHA: {merge_sha}" ) ;
85
- // If the merge was succesful, then set the actual try branch that will run CI to the
86
- // merged commit.
87
- repo. client
88
- . set_branch_to_sha ( TRY_BRANCH_NAME , & merge_sha)
89
- . await
90
- . map_err ( |error| anyhow ! ( "Cannot set try branch to main branch: {error:?}" ) ) ?;
91
-
92
- db. attach_try_build (
93
- pr_model,
94
- TRY_BRANCH_NAME . to_string ( ) ,
95
- merge_sha. clone ( ) ,
96
- base_sha. clone ( ) ,
97
- )
98
- . await ?;
99
- tracing:: info!( "Try build started" ) ;
100
-
101
- handle_label_trigger ( repo, pr. number , LabelTrigger :: TryBuildStarted ) . await ?;
102
113
103
- let comment = Comment :: new ( format ! (
104
- ":hourglass: Trying commit {} with merge {}…" ,
105
- pr. head. sha. clone( ) ,
106
- merge_sha
107
- ) ) ;
108
- repo. client . post_comment ( pr. number , comment) . await ?;
109
- Ok ( ( ) )
114
+ Ok ( MergeResult :: Success ( merge_sha) )
110
115
}
111
116
Err ( MergeError :: Conflict ) => {
112
117
tracing:: warn!( "Merge conflict" ) ;
113
- repo. client
114
- . post_comment (
115
- pr. number ,
116
- Comment :: new ( merge_conflict_message ( & pr. head . name ) ) ,
117
- )
118
- . await ?;
119
- Ok ( ( ) )
118
+
119
+ Ok ( MergeResult :: Conflict )
120
120
}
121
121
Err ( error) => Err ( error. into ( ) ) ,
122
122
}
123
123
}
124
124
125
+ async fn run_try_build (
126
+ ci_client : & GithubRepositoryClient ,
127
+ db : & PgDbClient ,
128
+ pr_model : PullRequestModel ,
129
+ commit_sha : CommitSha ,
130
+ parent_sha : CommitSha ,
131
+ ) -> anyhow:: Result < ( ) > {
132
+ ci_client
133
+ . set_branch_to_sha ( TRY_BRANCH_NAME , & commit_sha)
134
+ . await
135
+ . map_err ( |error| anyhow ! ( "Cannot set try branch to main branch: {error:?}" ) ) ?;
136
+
137
+ db. attach_try_build (
138
+ pr_model,
139
+ TRY_BRANCH_NAME . to_string ( ) ,
140
+ commit_sha,
141
+ parent_sha,
142
+ )
143
+ . await ?;
144
+
145
+ tracing:: info!( "Try build started" ) ;
146
+ Ok ( ( ) )
147
+ }
148
+
149
+ enum MergeResult {
150
+ Success ( CommitSha ) ,
151
+ Conflict ,
152
+ }
153
+
125
154
fn get_base_sha (
126
155
pr_model : & PullRequestModel ,
127
156
parent : Option < Parent > ,
@@ -267,8 +296,14 @@ fn auto_merge_commit_message(
267
296
message
268
297
}
269
298
270
- fn merge_conflict_message ( branch : & str ) -> String {
271
- format ! (
299
+ fn trying_build_comment ( head_sha : & CommitSha , merge_sha : & CommitSha ) -> Comment {
300
+ Comment :: new ( format ! (
301
+ ":hourglass: Trying commit {head_sha} with merge {merge_sha}…"
302
+ ) )
303
+ }
304
+
305
+ fn merge_conflict_comment ( branch : & str ) -> Comment {
306
+ let message = format ! (
272
307
r#":lock: Merge conflict
273
308
274
309
This pull request and the master branch diverged in a way that cannot
@@ -298,7 +333,8 @@ handled during merge and rebase. This is normal, and you should still perform st
298
333
299
334
</details>
300
335
"#
301
- )
336
+ ) ;
337
+ Comment :: new ( message)
302
338
}
303
339
304
340
async fn check_try_permissions (
0 commit comments