Class: Gitlabci::Bundle::Update::Mr::Client

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

Constructor Details

#initialize(gitlab_api_endpoint:, gitlab_api_private_token:, project_name:, branch:, author_email:, author_name:) ⇒ Client

Returns a new instance of Client.

Parameters:

  • gitlab_api_endpoint (String)
  • gitlab_api_private_token (String)
  • project_name (String)
  • branch (String)
  • author_email (String)
  • author_name (String)


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_email
  @author_name = author_name
end

Instance Method Details

#commit_gemfile_lock(lockfile) ⇒ Object

Parameters:

  • lockfile (String)


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

Parameters:

  • description (String)
  • mr_labels (Array<String>)
  • assignee_ids (Array<Integer>)

See Also:



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?

Parameters:

  • username (String)

Returns:

  • (Gitlab::ObjectifiedHash)
  • (nil)

    User isn't found

See Also:



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

Parameters:

  • username (String)

Returns:

  • (Gitlab::ObjectifiedHash)

Raises:

See Also:



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

Parameters:

  • old_lockfile (String)
  • new_lockfile (String)

Returns:

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

Parameters:

  • allow_dup_mr (Boolean)
  • mr_labels (Array<String>)
  • update_bundled_with (Boolean)
  • merge_when_pipeline_succeeds (Boolean)
  • assignees (Array<String>)


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