Class: PeopleGroup::Connectors::GitLab

Inherits:
Object
  • Object
show all
Defined in:
lib/peoplegroup/connectors/gitlab.rb

Constant Summary collapse

API_URL =

The GitLab instance API url.

ENV['GITLAB_API_V4_URL'] || 'https://gitlab.com/api/v4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: ENV['GITLAB_API_TOKEN']) ⇒ GitLab

Create a new client instance with the provided token for authentication.

Parameters:

  • token (String) (defaults to: ENV['GITLAB_API_TOKEN'])

    The personal access token to use for authentication.



16
17
18
# File 'lib/peoplegroup/connectors/gitlab.rb', line 16

def initialize(token: ENV['GITLAB_API_TOKEN'])
  @client = Gitlab.client(endpoint: API_URL, private_token: token)
end

Instance Attribute Details

#clientObject

The Gitlab.client instance we wrap around.



9
10
11
# File 'lib/peoplegroup/connectors/gitlab.rb', line 9

def client
  @client
end

Instance Method Details

#add_group_member(group_id, user_id, access_level) ⇒ Gitlab::ObjectifiedHash

Add a group member with a set access level.

Parameters:

  • group_id (Integer|String)

    the id of the group to add the member to.

  • user_id (Integer|String)

    the id of the user to add to the group.

  • access_level (Integer)

    the access level to add the user with.

Returns:

  • (Gitlab::ObjectifiedHash)

    the created group member.

See Also:



75
76
77
# File 'lib/peoplegroup/connectors/gitlab.rb', line 75

def add_group_member(group_id, user_id, access_level)
  retry_on_error { @client.add_group_member(group_id, user_id, access_level) }
end

#commit_change_to_new_merge_request(project_id, branch_name, file_path, file_with_change, commit_message, description = nil, target_branch: 'master', assign_group: nil) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/peoplegroup/connectors/gitlab.rb', line 271

def commit_change_to_new_merge_request(project_id, branch_name, file_path, file_with_change, commit_message, description = nil, target_branch: 'master', assign_group: nil)
  create_branch(project_id, branch_name, target_branch)

  actions = [
    {
      action: 'update',
      file_path: file_path,
      content: file_with_change
    }
  ]

  create_commit(project_id, branch_name, commit_message, actions)

  options = {
    source_branch: branch_name,
    target_branch: target_branch,
    remove_source_branch: true
  }

  options[:description] = description if description
  options[:assignee_ids] = group_member_ids(assign_group) if assign_group

  create_merge_request(project_id, commit_message, options)
end

#commit_change_to_new_merge_request_v2(project_id, branch_name, commit_message, description = nil, files_to_delete = [], files_to_update = [], target_branch: 'master', assign_group: nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/peoplegroup/connectors/gitlab.rb', line 236

def commit_change_to_new_merge_request_v2(project_id, branch_name, commit_message, description = nil, files_to_delete = [], files_to_update = [], target_branch: 'master', assign_group: nil)
  actions = []

  files_to_delete.each do |file|
    actions << {
      action: 'delete',
      file_path: file
    }
  end

  files_to_update.each do |file|
    actions << {
      action: 'update',
      file_path: file[:file_path],
      content: File.read(file[:tmp_file_path])
    }
  end

  return unless actions.any?

  create_branch(project_id, branch_name, target_branch)
  create_commit(project_id, branch_name, commit_message, actions)

  options = {
    source_branch: branch_name,
    target_branch: target_branch,
    remove_source_branch: true
  }

  options[:description] = description if description
  options[:assignee_ids] = group_member_ids(assign_group) if assign_group

  create_merge_request(project_id, commit_message, options)
end

#commit_change_to_new_merge_request_v3(project_id, branch_name, commit_message, description, files_to_delete: [], files_to_update: [], files_to_add: [], target_branch: 'master') ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/peoplegroup/connectors/gitlab.rb', line 198

def commit_change_to_new_merge_request_v3(project_id, branch_name, commit_message, description, files_to_delete: [], files_to_update: [], files_to_add: [], target_branch: 'master')
  actions = []

  files_to_delete.each do |file|
    actions << { action: 'delete', file_path: file }
  end

  files_to_add.each do |file|
    actions << {
      action: 'create',
      file_path: file[:remote_path],
      content: File.read(file[:local_path])
    }
  end

  files_to_update.each do |file|
    actions << {
      action: 'update',
      file_path: file[:remote_path],
      content: File.read(file[:local_path])
    }
  end

  return unless actions.any?

  create_branch(project_id, branch_name, target_branch)
  create_commit(project_id, branch_name, commit_message, actions)

  options = {
    source_branch: branch_name,
    target_branch: target_branch,
    description: description,
    remove_source_branch: true
  }

  create_merge_request(project_id, commit_message, options)
end

#commit_new_files_to_new_merge_request(project_id, branch_name, new_files, commit_message, description = nil, target_branch: 'master', assign_group: nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/peoplegroup/connectors/gitlab.rb', line 172

def commit_new_files_to_new_merge_request(project_id, branch_name, new_files, commit_message, description = nil, target_branch: 'master', assign_group: nil)
  create_branch(project_id, branch_name, target_branch)

  actions = []
  new_files.each do |file|
    actions << {
      action: 'create',
      file_path: file[:remote_path],
      content: File.read(file[:local_path])
    }
  end

  create_commit(project_id, branch_name, commit_message, actions)

  options = {
    source_branch: branch_name,
    target_branch: target_branch,
    remove_source_branch: true
  }

  options[:description] = description if description
  options[:assignee_ids] = group_member_ids(assign_group) if assign_group

  create_merge_request(project_id, commit_message, options)
end

#create_epic(group_id, title, options) ⇒ Gitlab::ObjectifiedHash

Create an epic for a group.

Parameters:

  • group_id (Integer|String)

    the id of the group to create the epic for.

  • title (String)

    the title of the epic.

  • options (Hash)

    the options to create the epic with.

Returns:

  • (Gitlab::ObjectifiedHash)

    the created epic.

See Also:



302
303
304
# File 'lib/peoplegroup/connectors/gitlab.rb', line 302

def create_epic(group_id, title, options)
  retry_on_error { @client.create_epic(group_id, title, options) }
end

#create_epic_note(group_id, epic_id, text) ⇒ Object

Create a note on an epic.

Parameters:

  • group_id (Integer|String)

    the id of the group to search for the epic in.

  • epic_id (Integer|String)

    the id of the epic to create the note for.

  • text (String)

    the text of the note.



310
311
312
# File 'lib/peoplegroup/connectors/gitlab.rb', line 310

def create_epic_note(group_id, epic_id, text)
  retry_on_error { @client.create_epic_note(group_id, epic_id, text) }
end

#create_group(name, path, options = {}) ⇒ Gitlab::ObjectifiedHash

Creates a GitLab group.

Parameters:

  • name (String)

    The name of the group.

  • path (String)

    The path of the group.

  • options (Hash) (defaults to: {})

    The options to create the group with.

Returns:

  • (Gitlab::ObjectifiedHash)

    The created group.

See Also:



47
48
49
# File 'lib/peoplegroup/connectors/gitlab.rb', line 47

def create_group(name, path, options = {})
  retry_on_error { @client.create_group(name, path, options) }
end

#create_issue(project, title, options) ⇒ Gitlab::ObjectifiedHash

Create a new issue.

Examples:

gitlab = PeopleGroup::Connectors::GitLab.new
options = { description: 'This is a new issue', labels: ['bug'] }
gitlab.create_issue(123, 'New issue', options)

Parameters:

  • project (Integer|String)

    the id of the project to create the issue in.

  • title (String)

    the title of the issue.

  • options (Hash)

    the options to create the issue with.

Returns:

  • (Gitlab::ObjectifiedHash)

    the created issue.

See Also:



104
105
106
# File 'lib/peoplegroup/connectors/gitlab.rb', line 104

def create_issue(project, title, options)
  retry_on_error { @client.create_issue(project, title, options) }
end

#create_issue_note(project, id, text) ⇒ Gitlab::ObjectifiedHash

Create a note on an issue

Examples:

gitlab = PeopleGroup::Connectors::GitLab.new
gitlab.create_issue_note(123, 456, 'This is a new note')

Parameters:

  • project (Integer|String)

    the id of the project to create the note in.

  • id (Integer|String)

    the id of the issue to create the note on.

  • text (String)

    the text of the note.

Returns:

  • (Gitlab::ObjectifiedHash)

    the created issue note.



116
117
118
# File 'lib/peoplegroup/connectors/gitlab.rb', line 116

def create_issue_note(project, id, text)
  retry_on_error { @client.create_issue_note(project, id, text) }
end

#edit_issue(project, id, options) ⇒ Gitlab::ObjectifiedHash

Edit an issue.

Parameters:

  • project (Integer|String)

    the id of the project to edit the issue in.

  • id (Integer|String)

    the id of the issue to edit.

  • options (Hash)

    the options to edit the issue with.

Returns:

  • (Gitlab::ObjectifiedHash)

    the updated issue.



134
135
136
# File 'lib/peoplegroup/connectors/gitlab.rb', line 134

def edit_issue(project, id, options)
  retry_on_error { @client.edit_issue(project, id, options) }
end

#find_gitlabber(field, query) ⇒ Object

Serach for a GitLab user under the gitlab-com group.



21
22
23
# File 'lib/peoplegroup/connectors/gitlab.rb', line 21

def find_gitlabber(field, query)
  find_gitlabber_on(field, query, 'gitlab-com')
end

#find_gitlabber_on(field, query, group) ⇒ Gitlab::ObjectifiedHash

Finds a GitLab user by the field.

Parameters:

  • field (Symbol)

    The field to search for.

  • query (String)

    The query to search for.

  • group (String|Integer)

    The group to search in.

Returns:

  • (Gitlab::ObjectifiedHash)

    The found GitLab user if any.



30
31
32
33
34
35
36
37
38
39
# File 'lib/peoplegroup/connectors/gitlab.rb', line 30

def find_gitlabber_on(field, query, group)
  return if !query || query.empty?

  possible_members = get_group_members(group, query: query)
  if field == :email
    possible_members.first
  else
    possible_members.find { |team_member| team_member.public_send(field) == query }
  end
end

#find_or_create_epic(group_id, title, options = {}) ⇒ Gitlab::ObjectifiedHash

Find or create an epic by title.

Parameters:

  • group_id (Integer|String)

    the id of the group to search for the epic in.

  • title (String)

    the title of the epic.

  • options (Hash) (defaults to: {})

    the options to create the epic with.

Returns:

  • (Gitlab::ObjectifiedHash)

    the created or found epic.



165
166
167
168
169
170
# File 'lib/peoplegroup/connectors/gitlab.rb', line 165

def find_or_create_epic(group_id, title, options = {})
  epic = find_epic(group_id, title)
  reopen_epic(epic) if epic && epic.state == 'closed'
  options[:confidential] = true
  epic || create_epic(group_id, title, options)
end

#get_epics(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Get all epics of a specific group.

Parameters:

  • group_id (Integer|String)

    the id of the group to search for the epics in.

  • options (Hash) (defaults to: {})

    the options to search for the epics with.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

    the list of epics associated

See Also:



319
320
321
# File 'lib/peoplegroup/connectors/gitlab.rb', line 319

def get_epics(group_id, options = {})
  retry_on_error { @client.epics(group_id, options).auto_paginate }
end

#get_file(project_id, file_path, ref = 'main') ⇒ Gitlab::ObjectifiedHash

Get the file at a specific path in a specific project.

Examples:

client = PeopleGroup::Connectors::GitLab.new
client.get_file(00000, 'path/to/file.txt')

Parameters:

  • project_id (Integer|String)

    the id of the project to search for the file in.

  • file_path (String)

    the files path relative to the project root.

  • ref (String) (defaults to: 'main')

    the branch or tag name, or the commit SHA, to retrieve the file from.

Returns:

  • (Gitlab::ObjectifiedHash)

    The get repository file response.

See Also:



341
342
343
# File 'lib/peoplegroup/connectors/gitlab.rb', line 341

def get_file(project_id, file_path, ref = 'main')
  retry_on_error { @client.get_file(project_id, file_path, ref) }
end

#get_file_contents(project_id, file_path, ref = 'main') ⇒ String

Get the contents of the specified file at a specific path in a project.

Examples:

client = PeopleGroup::Connectors::GitLab.new
client.get_file_contents(00000, 'path/to/file.txt')

Parameters:

  • project_id (Integer|String)

    the id of the project to search for the file in.

  • file_path (String)

    the file path relative to the project root.

  • ref (String) (defaults to: 'main')

    the branch or tag name, or the commit SHA, to retrieve the file from.

Returns:

  • (String)

    The contents of the file.



353
354
355
356
# File 'lib/peoplegroup/connectors/gitlab.rb', line 353

def get_file_contents(project_id, file_path, ref = 'main')
  response = get_file(project_id, file_path, ref)
  Base64.decode64(response&.content)
end

#get_group_members(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Get members of a group.

Parameters:

  • group_id (Integer|String)

    the id of the group to get the members of.

  • options (Hash) (defaults to: {})

    the options to get the members with.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

    the members of the group.

See Also:



65
66
67
# File 'lib/peoplegroup/connectors/gitlab.rb', line 65

def get_group_members(group_id, options = {})
  retry_on_error { @client.group_members(group_id, options).auto_paginate }
end

#get_issue(project, issue_id) ⇒ Gitlab::ObjectifiedHash

Gets an issue from the associated project.

Parameters:

  • project (Integer|String)

    the id of the project to search for the issue in.

  • issue_id (Integer|String)

    the id of the issue to get.

Returns:

  • (Gitlab::ObjectifiedHash)

    the issue object.



152
153
154
# File 'lib/peoplegroup/connectors/gitlab.rb', line 152

def get_issue(project, issue_id)
  retry_on_error { @client.issue(project, issue_id) }
end

#get_issue_epics(group_id, epic_iid) ⇒ Array<Gitlab::ObjectifiedHash>

Get the the issues for a specific epic.

Parameters:

  • group_id (Integer|String)

    the id of the group to search for the epic in.

  • epic_iid (Integer|String)

    the iid of the epic to search for the issues of.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

    the list of issues associated with the epic.

See Also:



328
329
330
# File 'lib/peoplegroup/connectors/gitlab.rb', line 328

def get_issue_epics(group_id, epic_iid)
  retry_on_error { @client.epic_issues(group_id, epic_iid) }
end

#get_issues(project, args) ⇒ Array<Gitlab::ObjectifiedHash> Also known as: get_onboarding_issues

Get a list of issues from a project.

Parameters:

  • project (Integer|String)

    the id of the project to search for the issue in.

  • args (Hash)

    the options to search for the issue with.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

    the list of issues matching the search criteria.

See Also:



143
144
145
# File 'lib/peoplegroup/connectors/gitlab.rb', line 143

def get_issues(project, args)
  retry_on_error { @client.issues(project, args).auto_paginate }
end

#get_subgroups(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Get subgroups of a group.

Parameters:

  • group_id (Integer|String)

    the id of the group to get the subgroups of.

  • options (Hash) (defaults to: {})

    the options to get the subgroups with.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

    the subgroups of the group.

See Also:



56
57
58
# File 'lib/peoplegroup/connectors/gitlab.rb', line 56

def get_subgroups(group_id, options = {})
  retry_on_error { @client.group_subgroups(group_id, options).auto_paginate }
end

#group_member_ids(group_id) ⇒ Array<Integer>

Get a list of a groups user ids

Parameters:

  • group_id (Integer|String)

    the id of the group to get the members of.

Returns:

  • (Array<Integer>)

    the user ids of the members of the group.



89
90
91
92
# File 'lib/peoplegroup/connectors/gitlab.rb', line 89

def group_member_ids(group_id)
  members = retry_on_error { @client.group_members(group_id) }
  members.map(&:id)
end

#issue_notes(project, id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

List notes on an issue.

Parameters:

  • project (Integer|String)

    the id of the project to list the notes from.

  • id (Integer|String)

    the id of the issue to list the notes from.

  • options (Hash) (defaults to: {})

    the options to list the notes with.

Returns:

  • (Array<Gitlab::ObjectifiedHash>)

    a list of notes associated to the issue.



125
126
127
# File 'lib/peoplegroup/connectors/gitlab.rb', line 125

def issue_notes(project, id, options = {})
  retry_on_error { @client.issue_notes(project, id, options) }
end

#remove_group_member(group_id, user_id) ⇒ Object

Remove a group member

Parameters:

  • group_id (Integer|String)

    the id of the group to remove the member from.

  • user_id (Integer|String)

    the id of the user to remove from the group.



82
83
84
# File 'lib/peoplegroup/connectors/gitlab.rb', line 82

def remove_group_member(group_id, user_id)
  retry_on_error { @client.remove_group_member(group_id, user_id) }
end

#update_variable(project, key, value, **opts) ⇒ Object



156
157
158
# File 'lib/peoplegroup/connectors/gitlab.rb', line 156

def update_variable(project, key, value, **opts)
  retry_on_error { @client.update_variable(project, key, value, **opts) }
end