Class: SashimiTanpopo::Provider::GitLab
- Defined in:
- lib/sashimi_tanpopo/provider/gitlab.rb
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
- #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>
-
#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.
-
#perform ⇒ String?
Apply recipe files.
Methods inherited from Base
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
#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 |
#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 |