Class: SashimiTanpopo::Provider::GitLab

Inherits:
Base
  • Object
show all
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

Instance Method Summary collapse

Methods inherited from Base

#apply_recipe_files

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.

Parameters:

  • recipe_paths (Array<String>)
  • target_dir (String, nil)
  • params (Hash<Symbol, String>)
  • dry_run (Boolean)
  • is_colored (Boolean)

    Whether show color diff

  • git_username (String, nil)
  • git_email (String, nil)
  • commit_message (String)
  • repository (String)
  • access_token (String)
  • api_endpoint (String) (defaults to: DEFAULT_API_ENDPOINT)
  • mr_title (String)
  • mr_body (String)
  • mr_source_branch (String)

    Merge Request source branch

  • mr_target_branch (String, nil)

    Merge Request target branch

  • mr_assignees (Array<String>) (defaults to: [])
  • mr_reviewers (Array<String>) (defaults to: [])
  • mr_labels (Array<String>) (defaults to: [])
  • is_draft_mr (Boolean)

    Whether create draft Pull Request

  • is_auto_merge (Boolean)

    Whether enable auto-merge



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 = 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

Parameters:

  • mode (String)

    e.g. 100644, 100755

Returns:

  • (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

Parameters:

  • api_endpoint (String)

Returns:

  • (String)


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?

Parameters:

  • username (String)

Returns:

  • (Integer)
  • (nil)

    user is not found

See Also:



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

Parameters:

  • username (String)

Returns:

  • (Integer)

Raises:

See Also:



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>

Parameters:

  • usernames (Array<String>)

Returns:

  • (Array<Integer>)

Raises:

See Also:



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

#performString?

Apply recipe files

Returns:

  • (String)

    Created Merge Request URL

  • (nil)

    Merge Request isn’t created



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