Problem merging a request using API with Ruby wrapper and CLI for Gitlab
I'm using Ruby wrapper and CLI for GitLab REST API (https://github.com/NARKOZ/gitlab) to create and accept requests in our CI.
I have 2 script, one create a Merge Request and one cycle through the merge request list of the project, find the correct one and accept the merge. The 2 ruby script are called in 2 successive stages inside a gitlab-ci.yaml. All was working correctly until 6 day ago. After that, today, I've done some work and the merge accept failed with a 405 error. I was using the old 3.7 version that was still using version 3 API so I upgraded the Ruby wrapper to 4.0 and changed to use API 4.0 but I still get the same error.
The strange thing is that if I go to the server where the runner executed the script, login with the gitlab-runner user, go into the build directory and run the command manually the merge succeed. I've thinked of a timing issue so I put a sleep but still get the error:
This is the output of the gitlab-runner with the 405 error message
Running with gitlab-ci-multi-runner 9.0.2 (fa8b86d)
on Puppet Master (04a94afd)
Using Shell executor...
Running on itrompm10...
Cloning repository...
Cloning into '/home/gitlab-runner/builds/04a94afd/0/puppet/control-repo'...
Checking out f3d67801 as development...
Skipping Git submodules setup
$ sleep 10
$ bin/gitlab_accept_merge_request.rb development testing
/usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/request.rb:80:in `validate': Server responded with code 405, message: 405 Method Not Allowed. Request URI: https://itromgit01.q8int.com/api/v3/projects/3/merge_requests/298/merge (Gitlab::Error::MethodNotAllowed)
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/request.rb:55:in `put'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/client/merge_requests.rb:82:in `accept_merge_request'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab.rb:25:in `method_missing'
from bin/gitlab_accept_merge_request.rb:35:in `block in <main>'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/paginated_response.rb:48:in `block (2 levels) in auto_paginate'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/paginated_response.rb:20:in `each'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/paginated_response.rb:20:in `method_missing'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/paginated_response.rb:47:in `block in auto_paginate'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/paginated_response.rb:36:in `each_page'
from /usr/local/share/gems/gems/gitlab-4.0.0/lib/gitlab/paginated_response.rb:45:in `auto_paginate'
from bin/gitlab_accept_merge_request.rb:27:in `<main>'
Merging id 3/298: Merged: f3d6780 Test merge request
from development to testing
Running after script...
$ bin/gitlab_after.sh
Cleanup keys
This is the code used. It get some parameters from a configuration file but at the end the line that get the error is this:
Gitlab.accept_merge_request(project_id,mr_id, { merge_commit_message: "#{mr_title}" })
Complete code
#!/usr/bin/env ruby
require 'gitlab'
GITLAB_CONFIG='/etc/gitlab-cli.conf'
config = {}
File.foreach GITLAB_CONFIG do |line|
k = line.split("=")[0].gsub("\n",'') if line =~/=/
v = line.split("=")[1].gsub("\n",'') if line =~/=/
config.store(k,v)
end
last_commit=`git log -1 --oneline`
source_branch = ARGV[0] ? ARGV[0] : 'development'
destination_branch = ARGV[1] ? ARGV[1] : 'testing'
mr_title = "Merged: #{last_commit} from #{source_branch} to #{destination_branch}"
project_id = config['GITLAB_API_PROJECT_ID']
endpoint = config['GITLAB_API_ENDPOINT']
private_token = config['GITLAB_API_PRIVATE_TOKEN']
httparty = config['GITLAB_API_HTTPARTY_OPTIONS'].gsub("'",'')
Gitlab.endpoint = endpoint
Gitlab.private_token = private_token
Gitlab.httparty = eval(httparty)
merge_requests = Gitlab.merge_requests(project_id)
#merge_requests.has_next_page?
#merge_requests.next_page
merge_requests.auto_paginate do |merge_request|
req=merge_request.to_h
if req["state"] == "opened"
mr_id=req["iid"]
sb=req["source_branch"]
db=req["target_branch"]
if source_branch == sb and destination_branch == db
print "Merging id #{project_id}/#{mr_id}: #{mr_title}\n"
Gitlab.accept_merge_request(project_id,mr_id, { merge_commit_message: "#{mr_title}" })
exit 0
end
end
end
#merge_requests.auto_paginate
print "Error: Merge Request NOT Found: #{mr_title}\n"
exit 1
If I run the code from the gitlab runner user I don't get any error and the merged will be correctly accepter:
This is the output of the run:
[gitlab-runner@itrompm10 control-repo]$ bin/gitlab_accept_merge_request.rb development testing
Merging id 3/298: Merged: f3d6780 Test merge request
from development to testing
[gitlab-runner@itrompm10 control-repo]$
I've also posted an Issue on the Ruby wrapper (https://github.com/NARKOZ/gitlab/issues/272)
The issue is present on Gitlab 9.0.5-ee and gitlab-runner-9.0.2, I've checked back on date of the update and this was working correctly on 9.0.5 without problem.