Class: SashimiTanpopo::Provider::GitLab
- Defined in:
- lib/sashimi_tanpopo/provider/gitlab.rb,
sig/sashimi_tanpopo/provider/gitlab.rbs
Overview
Apply recipe files and create Pull Request
Constant Summary collapse
- DEFAULT_API_ENDPOINT =
"https://gitlab.com/api/v4"- MAX_RETRY_COUNT =
5
Class Method Summary collapse
- .executable_mode?(mode) ⇒ Boolean
-
.gitlab_host(api_endpoint) ⇒ String
Get GitLab host from api_endpoint.
Instance Method Summary collapse
-
#create_branch_and_push_changes(changed_files)
Create branch on repository and push changes.
-
#create_merge_request ⇒ Hash{iid: Integer, web_url: String}
Created Merge Request info.
- #current_user_name ⇒ String
-
#exists_branch?(branch) ⇒ Boolean
Whether exists branch on repository.
- #get_default_branch ⇒ String
- #get_user_id_from_user_name(username) ⇒ Integer?
- #get_user_id_from_user_name!(username) ⇒ Integer
- #get_user_ids_from_user_names!(usernames) ⇒ Array<Integer>
- #git_email ⇒ String
- #git_username ⇒ String
-
#initialize(recipe_paths:, target_dir:, params:, dry_run:, is_colored:, git_username:, git_email:, commit_message:, repository:, access_token:, api_endpoint: DEFAULT_API_ENDPOINT, mr_title:, mr_body:, mr_source_branch:, mr_target_branch:, mr_assignees: [], mr_reviewers: [], mr_labels: [], is_draft_mr:, is_auto_merge:) ⇒ GitLab
constructor
A new instance of GitLab.
- #mr_target_branch ⇒ String
-
#perform ⇒ String?
Apply recipe files.
- #set_auto_merge(mr_iid)
- #with_retry
Methods inherited from Base
#apply_recipe_files, #cleanup_target_dir
Constructor Details
#initialize(recipe_paths:, target_dir:, params:, dry_run:, is_colored:, git_username:, git_email:, commit_message:, repository:, access_token:, api_endpoint: DEFAULT_API_ENDPOINT, mr_title:, mr_body:, mr_source_branch:, mr_target_branch:, mr_assignees: [], mr_reviewers: [], mr_labels: [], is_draft_mr:, is_auto_merge:) ⇒ GitLab
Returns a new instance of GitLab.
31 32 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 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 31 def initialize(recipe_paths:, target_dir:, params:, dry_run:, is_colored:, git_username:, git_email:, commit_message:, repository:, access_token:, api_endpoint: DEFAULT_API_ENDPOINT, mr_title:, mr_body:, mr_source_branch:, mr_target_branch:, mr_assignees: [], mr_reviewers: [], mr_labels: [], is_draft_mr:, is_auto_merge:) super( recipe_paths: recipe_paths, target_dir: target_dir, params: params, dry_run: dry_run, is_colored: is_colored, is_update_local: false, ) @commit_message = @repository = repository @mr_title = mr_title @mr_body = mr_body @mr_source_branch = mr_source_branch @mr_target_branch = mr_target_branch @mr_assignees = mr_assignees @mr_reviewers = mr_reviewers @mr_labels = mr_labels @is_draft_mr = is_draft_mr @is_auto_merge = is_auto_merge @git_username = git_username @git_email = git_email @api_endpoint = api_endpoint @gitlab = Gitlab.client(endpoint: api_endpoint, private_token: access_token) end |
Class Method Details
.executable_mode?(mode) ⇒ Boolean
135 136 137 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 135 def self.executable_mode?(mode) (mode.to_i(8) & 1) != 0 end |
.gitlab_host(api_endpoint) ⇒ String
Get GitLab host from api_endpoint
144 145 146 147 148 149 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 144 def self.gitlab_host(api_endpoint) matched = %r{^https?://(.+)/api}.match(api_endpoint) return matched[1] if matched # steep:ignore "example.com" end |
Instance Method Details
#create_branch_and_push_changes(changed_files)
This method returns an undefined value.
Create branch on repository and push changes
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 225 def create_branch_and_push_changes(changed_files) actions = changed_files.map do |file_path, changed_file| { action: "update", file_path: file_path, execute_filemode: self.class.executable_mode?(changed_file[:mode]), content: changed_file[:after_content], } end with_retry do @gitlab.create_commit( @repository, @mr_source_branch, @commit_message, actions, start_branch: mr_target_branch, author_email: git_email, author_name: git_username, ) end end |
#create_merge_request ⇒ Hash{iid: Integer, web_url: String}
Returns Created Merge Request info.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 251 def create_merge_request params = { source_branch: @mr_source_branch, target_branch: mr_target_branch, remove_source_branch: true, description: @mr_body, } params[:labels] = @mr_labels.join(",") unless @mr_labels.empty? unless @mr_assignees.empty? params[:assignee_ids] = get_user_ids_from_user_names!(@mr_assignees) # steep:ignore end unless @mr_reviewers.empty? params[:reviewer_ids] = get_user_ids_from_user_names!(@mr_reviewers) # steep:ignore end mr_title = if @is_draft_mr "Draft: " + @mr_title else @mr_title end mr = with_retry do @gitlab.create_merge_request( @repository, mr_title, params, ) end { iid: mr["iid"], web_url: mr["web_url"], } end |
#current_user_name ⇒ String
188 189 190 191 192 193 194 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 188 def current_user_name user = with_retry do @gitlab.user end user["username"] end |
#exists_branch?(branch) ⇒ Boolean
Whether exists branch on repository
211 212 213 214 215 216 217 218 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 211 def exists_branch?(branch) with_retry do @gitlab.branch(@repository, branch) end true rescue Gitlab::Error::NotFound false end |
#get_default_branch ⇒ String
197 198 199 200 201 202 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 197 def get_default_branch project = with_retry do @gitlab.project(@repository) end project["default_branch"] end |
#get_user_id_from_user_name(username) ⇒ Integer?
96 97 98 99 100 101 102 103 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 96 def get_user_id_from_user_name(username) user = with_retry do @gitlab.users(username: username).first end return nil unless user user["id"].to_i end |
#get_user_id_from_user_name!(username) ⇒ Integer
112 113 114 115 116 117 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 112 def get_user_id_from_user_name!(username) user_id = get_user_id_from_user_name(username) raise NotFoundUserError, "#{username} isn't found" unless user_id user_id end |
#get_user_ids_from_user_names!(usernames) ⇒ Array<Integer>
126 127 128 129 130 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 126 def get_user_ids_from_user_names!(usernames) Parallel.map(usernames, in_threads: 2) do |username| get_user_id_from_user_name!(username) end end |
#git_email ⇒ String
164 165 166 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 164 def git_email @git_email ||= "#{git_username}@noreply.#{self.class.gitlab_host(@api_endpoint)}" end |
#git_username ⇒ String
159 160 161 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 159 def git_username @git_username ||= current_user_name end |
#mr_target_branch ⇒ String
154 155 156 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 154 def mr_target_branch @mr_target_branch ||= get_default_branch end |
#perform ⇒ String?
Apply recipe files
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 67 def perform changed_files = apply_recipe_files return nil if changed_files.empty? || @dry_run if exists_branch?(@mr_source_branch) SashimiTanpopo.logger.info "Skipped because branch #{@pr_source_branch} already exists on #{@repository}" return nil end create_branch_and_push_changes(changed_files) mr = create_merge_request SashimiTanpopo.logger.info "Merge Request is created: #{mr[:web_url]}" if @is_auto_merge set_auto_merge(mr[:iid]) SashimiTanpopo.logger.info "Set auto-merge to #{mr[:web_url]}" end mr[:web_url] end |
#set_auto_merge(mr_iid)
This method returns an undefined value.
293 294 295 296 297 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 293 def set_auto_merge(mr_iid) with_retry do @gitlab.accept_merge_request(@repository, mr_iid, auto_merge: true, should_remove_source_branch: true) end end |
#with_retry
This method returns an undefined value.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/sashimi_tanpopo/provider/gitlab.rb', line 168 def with_retry retry_count ||= 0 # steep:ignore yield rescue Gitlab::Error::MethodNotAllowed, Gitlab::Error::NotAcceptable, Gitlab::Error::Unprocessable => error retry_count += 1 # steep:ignore raise error if retry_count > MAX_RETRY_COUNT SashimiTanpopo.logger.warn "Error is occurred and auto retry (#{retry_count}/#{MAX_RETRY_COUNT}): #{error}" # 1, 2, 4, 8, 16 ... sleep_time = 2 ** (retry_count - 1) sleep sleep_time # steep:ignore retry end |