@@ -44,6 +44,7 @@ def update_merge_request_and_accept(self, approvals):
44
44
api = self ._api
45
45
merge_request = self ._merge_request
46
46
updated_into_up_to_date_target_branch = False
47
+ temp_branch = self .opts .temp_branch
47
48
48
49
while not updated_into_up_to_date_target_branch :
49
50
self .ensure_mergeable_mr (merge_request )
@@ -67,16 +68,36 @@ def update_merge_request_and_accept(self, approvals):
67
68
68
69
trust = True
69
70
if self ._project .only_allow_merge_if_pipeline_succeeds :
71
+ if temp_branch and source_project is not self ._project :
72
+ trust = False
70
73
self .wait_for_ci_to_pass (merge_request , actual_sha )
71
74
time .sleep (2 )
72
- if self .opts .temp_branch and source_project is not self ._project :
73
- trust = False
74
75
75
76
self .ensure_mergeable_mr (merge_request )
76
77
77
78
try :
78
- merge_request .accept (remove_branch = True , sha = actual_sha , trust_pipeline = trust ,
79
- project = self ._project )
79
+ if trust :
80
+ merge_request .accept (remove_branch = True , sha = actual_sha )
81
+ else :
82
+ # Cannot accept the MR using the API, because it requires a passing pipeline
83
+ # but the pipeline associated to the MR cannot be trusted. Use manual merging
84
+ log .info ('Merging %r manually (into: %r)' , temp_branch , merge_request .target_branch )
85
+ try :
86
+ final_sha = self .repo .fast_forward (
87
+ merge_request .target_branch ,
88
+ temp_branch ,
89
+ )
90
+ assert final_sha == actual_sha
91
+ self .repo .push (merge_request .target_branch , force = False )
92
+ except AssertionError :
93
+ log .warning ('sha mismatch!' )
94
+ raise CannotMerge ('Manually merged branch does not match source branch!' )
95
+ except git .GitError :
96
+ log .warning ('Failed to push!' )
97
+ raise CannotMerge (
98
+ 'Failed to push manually merged branch!'
99
+ 'Do I have permission to push to **%s**?' % merge_request .target_branch
100
+ )
80
101
except gitlab .NotAcceptable as err :
81
102
new_target_sha = Commit .last_on_branch (self ._project .id , merge_request .target_branch , api ).id
82
103
# target_branch has moved under us since we updated, just try again
0 commit comments