Class: Gitlabci::Bundle::Update::Mr::Client
- Inherits:
-
Object
- Object
- Gitlabci::Bundle::Update::Mr::Client
- Defined in:
- lib/gitlabci/bundle/update/mr/client.rb
Overview
rubocop:disable Metrics/ClassLength
Constant Summary collapse
- BRANCH_PREFIX =
"bundle-update-".freeze
- TITLE_PREFIX =
"bundle update at ".freeze
- MAX_RETRY_COUNT =
3
Instance Method Summary collapse
- #commit_gemfile_lock(lockfile) ⇒ Object
- #create_merge_request(description:, mr_labels:, assignee_ids:) ⇒ Object
- #find_by_username(username) ⇒ Gitlab::ObjectifiedHash?
- #find_by_username!(username) ⇒ Gitlab::ObjectifiedHash
-
#initialize(gitlab_api_endpoint:, gitlab_api_private_token:, project_name:, branch:, author_email:, author_name:) ⇒ Client
constructor
A new instance of Client.
- #merge_request_description(old_lockfile, new_lockfile) ⇒ String
- #perform(allow_dup_mr:, mr_labels:, update_bundled_with:, merge_when_pipeline_succeeds:, assignees:) ⇒ Object
Constructor Details
#initialize(gitlab_api_endpoint:, gitlab_api_private_token:, project_name:, branch:, author_email:, author_name:) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 18 def initialize(gitlab_api_endpoint:, gitlab_api_private_token:, project_name:, branch:, author_email:, author_name:) @gitlab = Gitlab.client(endpoint: gitlab_api_endpoint, private_token: gitlab_api_private_token) @project_name = project_name @branch = branch @author_email = @author_name = end |
Instance Method Details
#commit_gemfile_lock(lockfile) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 67 def commit_gemfile_lock(lockfile) @gitlab.create_commit( @project_name, new_branch, "$ bundle update && bundle update --ruby", [ { action: "update", file_path: lockfile_name, content: lockfile, }, ], start_branch: @branch, author_email: @author_email, author_name: @author_name, ) end |
#create_merge_request(description:, mr_labels:, assignee_ids:) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 90 def create_merge_request(description:, mr_labels:, assignee_ids:) params = { source_branch: new_branch, target_branch: @branch, remove_source_branch: true, description:, } unless mr_labels.empty? params[:labels] = mr_labels.join(",") end if assignee_ids.length == 1 params[:assignee_id] = assignee_ids[0] elsif assignee_ids.length >= 2 params[:assignee_ids] = assignee_ids end @gitlab.create_merge_request( @project_name, "#{TITLE_PREFIX}#{current_time.strftime("%Y-%m-%d %H:%M:%S %Z")}", params, ) end |
#find_by_username(username) ⇒ Gitlab::ObjectifiedHash?
141 142 143 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 141 def find_by_username(username) @gitlab.users(username:).first end |
#find_by_username!(username) ⇒ Gitlab::ObjectifiedHash
152 153 154 155 156 157 158 159 160 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 152 def find_by_username!(username) user = find_by_username(username) unless user raise NotFoundUserError, "#{username} isn't found" end user end |
#merge_request_description(old_lockfile, new_lockfile) ⇒ String
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 119 def merge_request_description(old_lockfile, new_lockfile) compare_linker = CompareLinker.new("dummy", "dummy") compare_linker.formatter = CompareLinker::Formatter::Markdown.new compare_links = compare_linker. make_compare_links_from_lockfiles(Bundler::LockfileParser.new(old_lockfile), Bundler::LockfileParser.new(new_lockfile)). to_a.join("\n") <<~MARKDOWN **Updated RubyGems:** #{compare_links} Powered by [gitlabci-bundle-update-mr](https://rubygems.org/gems/gitlabci-bundle-update-mr) MARKDOWN end |
#perform(allow_dup_mr:, mr_labels:, update_bundled_with:, merge_when_pipeline_succeeds:, assignees:) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gitlabci/bundle/update/mr/client.rb', line 33 def perform(allow_dup_mr:, mr_labels:, update_bundled_with:, merge_when_pipeline_succeeds:, assignees:) if !allow_dup_mr && exists_bundle_update_mr? puts "Skip because it has already existed." return end assignee_ids = get_assignee_ids(assignees) old_lockfile = File.read(lockfile_name) system!("bundle update") system!("bundle update --ruby") restore_bundled_with unless update_bundled_with new_lockfile = File.read(lockfile_name) if old_lockfile == new_lockfile puts "#{lockfile_name} is not updated" return end commit_gemfile_lock(new_lockfile) description = merge_request_description(old_lockfile, new_lockfile) mr = create_merge_request(description:, mr_labels:, assignee_ids:) puts "MR is created: #{mr.web_url}" if merge_when_pipeline_succeeds accept_merge_request(mr.iid) puts "Set merge_when_pipeline_succeeds to #{mr.web_url}" end end |