Class: PeopleGroup::Connectors::GitLab

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

Constant Summary collapse

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

Returns a new instance of GitLab.



12
13
14
# File 'lib/peoplegroup/connectors/gitlab.rb', line 12

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#add_group_member(group_id, user_id, access_level) ⇒ Object



43
44
45
# File 'lib/peoplegroup/connectors/gitlab.rb', line 43

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/peoplegroup/connectors/gitlab.rb', line 194

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/peoplegroup/connectors/gitlab.rb', line 159

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/peoplegroup/connectors/gitlab.rb', line 121

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/peoplegroup/connectors/gitlab.rb', line 95

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) ⇒ Object



219
220
221
# File 'lib/peoplegroup/connectors/gitlab.rb', line 219

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



223
224
225
# File 'lib/peoplegroup/connectors/gitlab.rb', line 223

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 = {}) ⇒ Object



31
32
33
# File 'lib/peoplegroup/connectors/gitlab.rb', line 31

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

#create_issue(project, title, options = {}) ⇒ Object



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

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

#create_issue_note(project, id, text) ⇒ Object



60
61
62
# File 'lib/peoplegroup/connectors/gitlab.rb', line 60

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

#edit_issue(project, id, options) ⇒ Object



68
69
70
# File 'lib/peoplegroup/connectors/gitlab.rb', line 68

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

#find_gitlabber(field, query) ⇒ Object



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

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

#find_gitlabber_on(field, query, group) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/peoplegroup/connectors/gitlab.rb', line 20

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 = {}) ⇒ Object



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

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 = {}) ⇒ Object



227
228
229
# File 'lib/peoplegroup/connectors/gitlab.rb', line 227

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

#get_group_members(group_id, options = {}) ⇒ Object



39
40
41
# File 'lib/peoplegroup/connectors/gitlab.rb', line 39

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

#get_issue(project, issue_id) ⇒ Object



80
81
82
# File 'lib/peoplegroup/connectors/gitlab.rb', line 80

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

#get_issue_epics(group_id, epic_iid) ⇒ Object



231
232
233
# File 'lib/peoplegroup/connectors/gitlab.rb', line 231

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

#get_issues(project, args) ⇒ Object



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

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

#get_onboarding_issues(project, args) ⇒ Object



72
73
74
# File 'lib/peoplegroup/connectors/gitlab.rb', line 72

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

#get_subgroups(group_id, options = {}) ⇒ Object



35
36
37
# File 'lib/peoplegroup/connectors/gitlab.rb', line 35

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

#group_member_ids(group_id) ⇒ Object



51
52
53
54
# File 'lib/peoplegroup/connectors/gitlab.rb', line 51

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

#issue_notes(project, id, options = {}) ⇒ Object



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

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

#remove_group_member(group_id, user_id) ⇒ Object



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

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



84
85
86
# File 'lib/peoplegroup/connectors/gitlab.rb', line 84

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