Class: PeopleGroup::Connectors::GitLab
- Inherits:
-
Object
- Object
- PeopleGroup::Connectors::GitLab
- 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
-
#client ⇒ Object
The Gitlab.client instance we wrap around.
Instance Method Summary collapse
-
#add_group_member(group_id, user_id, access_level) ⇒ Gitlab::ObjectifiedHash
Add a group member with a set access level.
- #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
- #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
- #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
- #commit_new_files_to_new_merge_request(project_id, branch_name, new_files, commit_message, description = nil, target_branch: 'master', assign_group: nil) ⇒ Object
-
#create_epic(group_id, title, options) ⇒ Gitlab::ObjectifiedHash
Create an epic for a group.
-
#create_epic_note(group_id, epic_id, text) ⇒ Object
Create a note on an epic.
-
#create_group(name, path, options = {}) ⇒ Gitlab::ObjectifiedHash
Creates a GitLab group.
-
#create_issue(project, title, options) ⇒ Gitlab::ObjectifiedHash
Create a new issue.
-
#create_issue_note(project, id, text) ⇒ Gitlab::ObjectifiedHash
Create a note on an issue.
-
#edit_issue(project, id, options) ⇒ Gitlab::ObjectifiedHash
Edit an issue.
-
#find_gitlabber(field, query) ⇒ Object
Serach for a GitLab user under the gitlab-com group.
-
#find_gitlabber_on(field, query, group) ⇒ Gitlab::ObjectifiedHash
Finds a GitLab user by the field.
-
#find_or_create_epic(group_id, title, options = {}) ⇒ Gitlab::ObjectifiedHash
Find or create an epic by title.
-
#get_epics(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Get all epics of a specific group.
-
#get_file(project_id, file_path, ref = 'main') ⇒ Gitlab::ObjectifiedHash
Get the file at a specific path in a specific project.
-
#get_file_contents(project_id, file_path, ref = 'main') ⇒ String
Get the contents of the specified file at a specific path in a project.
-
#get_group_members(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Get members of a group.
-
#get_issue(project, issue_id) ⇒ Gitlab::ObjectifiedHash
Gets an issue from the associated project.
-
#get_issue_epics(group_id, epic_iid) ⇒ Array<Gitlab::ObjectifiedHash>
Get the the issues for a specific epic.
-
#get_issues(project, args) ⇒ Array<Gitlab::ObjectifiedHash>
(also: #get_onboarding_issues)
Get a list of issues from a project.
-
#get_subgroups(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Get subgroups of a group.
-
#group_member_ids(group_id) ⇒ Array<Integer>
Get a list of a groups user ids.
-
#initialize(token: ENV['GITLAB_API_TOKEN']) ⇒ GitLab
constructor
Create a new client instance with the provided token for authentication.
-
#issue_notes(project, id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
List notes on an issue.
-
#remove_group_member(group_id, user_id) ⇒ Object
Remove a group member.
- #update_variable(project, key, value, **opts) ⇒ Object
Constructor Details
#initialize(token: ENV['GITLAB_API_TOKEN']) ⇒ GitLab
Create a new client instance with the provided token 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
#client ⇒ Object
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.
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, , 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, , actions) = { source_branch: branch_name, target_branch: target_branch, remove_source_branch: true } [:description] = description if description [:assignee_ids] = group_member_ids(assign_group) if assign_group create_merge_request(project_id, , ) 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, , 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, , actions) = { source_branch: branch_name, target_branch: target_branch, remove_source_branch: true } [:description] = description if description [:assignee_ids] = group_member_ids(assign_group) if assign_group create_merge_request(project_id, , ) 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, , 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, , actions) = { source_branch: branch_name, target_branch: target_branch, description: description, remove_source_branch: true } create_merge_request(project_id, , ) 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, , 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, , actions) = { source_branch: branch_name, target_branch: target_branch, remove_source_branch: true } [:description] = description if description [:assignee_ids] = group_member_ids(assign_group) if assign_group create_merge_request(project_id, , ) end |
#create_epic(group_id, title, options) ⇒ Gitlab::ObjectifiedHash
Create an epic for a group.
302 303 304 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 302 def create_epic(group_id, title, ) retry_on_error { @client.create_epic(group_id, title, ) } end |
#create_epic_note(group_id, epic_id, text) ⇒ Object
Create a note on an epic.
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.
47 48 49 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 47 def create_group(name, path, = {}) retry_on_error { @client.create_group(name, path, ) } end |
#create_issue(project, title, options) ⇒ Gitlab::ObjectifiedHash
Create a new issue.
104 105 106 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 104 def create_issue(project, title, ) retry_on_error { @client.create_issue(project, title, ) } end |
#create_issue_note(project, id, text) ⇒ Gitlab::ObjectifiedHash
Create a note on an issue
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.
134 135 136 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 134 def edit_issue(project, id, ) retry_on_error { @client.edit_issue(project, id, ) } 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.
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.
165 166 167 168 169 170 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 165 def find_or_create_epic(group_id, title, = {}) epic = find_epic(group_id, title) reopen_epic(epic) if epic && epic.state == 'closed' [:confidential] = true epic || create_epic(group_id, title, ) end |
#get_epics(group_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Get all epics of a specific group.
319 320 321 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 319 def get_epics(group_id, = {}) retry_on_error { @client.epics(group_id, ).auto_paginate } end |
#get_file(project_id, file_path, ref = 'main') ⇒ Gitlab::ObjectifiedHash
Get the file at a specific path in a specific project.
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.
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.
65 66 67 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 65 def get_group_members(group_id, = {}) retry_on_error { @client.group_members(group_id, ).auto_paginate } end |
#get_issue(project, issue_id) ⇒ Gitlab::ObjectifiedHash
Gets an issue from the associated project.
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.
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.
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.
56 57 58 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 56 def get_subgroups(group_id, = {}) retry_on_error { @client.group_subgroups(group_id, ).auto_paginate } end |
#group_member_ids(group_id) ⇒ Array<Integer>
Get a list of a groups user ids
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.
125 126 127 |
# File 'lib/peoplegroup/connectors/gitlab.rb', line 125 def issue_notes(project, id, = {}) retry_on_error { @client.issue_notes(project, id, ) } end |
#remove_group_member(group_id, user_id) ⇒ Object
Remove a group member
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 |