/merge api mergeable_state check ignoring merge_when_build_succeeds

Summary

When I try to merge an MR via the api with merge_when_build_succeeds set while ci is running , it returns a 405 because mergeable_ci_state returns false (my project doesn't allow MRs to be merged without ci checks passing). I would expect that check to pass because I'm specifically trying to merge once ci passes.

Steps to reproduce

  • Set project's only_allow_merge_if_build_succeeds setting to true. Screen_Shot_2016-09-28_at_10.10.43_AM
  • update .gitlab-ci.yml with a job to be run on your MRs (just do something on all branches)...just something so that ci runs on your MR long enough to run the following curl command. (script: sleep 1000000 would probably be fine)
  • Create a MR.
  • call /api/v3/projects/:id/merge_requests/:merge_request_id/merge with merge_when_build_succeeds set to true.
curl -X PUT -H "Content-Type: application/json" -H "PRIVATE-TOKEN: XXXXX" -d '{
    "merge_when_build_succeeds": true
}' "https://gitlab.porch.com/api/v3/projects/:id/merge_requests/:merge_request_id/merge"

Expected behavior

I would expect the MR to merge once ci checks pass.

Actual behavior

I receive a 405.

Results of GitLab environment info

GitLab Enterprise Edition 8.12.1-ee 8a575a57

Possible fixes

https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/api/merge_requests.rb#L232

set skip_ci_check with merge_when_build_succeeds is true by replacing
not_allowed! unless merge_request.mergeable_state?
with
not_allowed! unless merge_request.mergeable_state?(to_boolean(params[:merge_when_build_succeeds]))