1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 120 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 158 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 193 194 195 196 197 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 235 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
package api
import (
"errors"
"net/http"
"sort"
gitlab "gitlab.com/gitlab-org/api/client-go"
)
var ErrTodoExists = errors.New("To-do already exists.")
var ApproveMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.ApproveMergeRequestOptions) (*gitlab.MergeRequestApprovals, error) {
if client == nil {
client = apiClient.Lab()
}
mr, _, err := client.MergeRequestApprovals.ApproveMergeRequest(projectID, mrID, opts)
if err != nil {
return nil, err
}
return mr, nil
}
var GetMRApprovalState = func(client *gitlab.Client, projectID interface{}, mrID int, opts ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovalState, error) {
if client == nil {
client = apiClient.Lab()
}
mrApprovals, _, err := client.MergeRequestApprovals.GetApprovalState(projectID, mrID, opts...)
if err != nil {
return nil, err
}
return mrApprovals, nil
}
var GetMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.GetMergeRequestsOptions) (*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
mr, _, err := client.MergeRequests.GetMergeRequest(projectID, mrID, opts)
if err != nil {
return nil, err
}
return mr, nil
}
// ListGroupMRs retrieves merge requests for a given group with optional filtering by assignees or reviewers.
//
// Parameters:
// - client: A GitLab client instance.
// - groupID: The ID or name of the group.
// - opts: GitLab-specific options for listing group merge requests.
// - listOpts: Optional list of arguments to filter by assignees or reviewers.
// May be any combination of api.WithMRAssignees and api.WithMRReviewers.
//
// Returns:
// - A slice of GitLab merge request objects and an error, if any.
//
// Example usage:
//
// groupMRs, err := api.ListGroupMRs(client, "my-group", &gitlab.ListGroupMergeRequestsOptions{},
// api.WithMRAssignees([]int{123}),
// api.WithMRReviewers([]int{456, 789}))
var ListGroupMRs = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListGroupMergeRequestsOptions, listOpts ...CliListMROption) ([]*gitlab.MergeRequest, error) {
composedListOpts := composeCliListMROptions(listOpts...)
assigneeIds, reviewerIds := composedListOpts.assigneeIds, composedListOpts.reviewerIds
if len(assigneeIds) > 0 || len(reviewerIds) > 0 {
return listGroupMRsWithAssigneesOrReviewers(client, projectID, opts, assigneeIds, reviewerIds)
} else {
return listGroupMRsBase(client, projectID, opts)
}
}
var listGroupMRsBase = func(client *gitlab.Client, groupID interface{}, opts *gitlab.ListGroupMergeRequestsOptions) ([]*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
mrs, _, err := client.MergeRequests.ListGroupMergeRequests(groupID, opts)
if err != nil {
return nil, err
}
return mrs, nil
}
var listGroupMRsWithAssigneesOrReviewers = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListGroupMergeRequestsOptions, assigneeIds []int, reviewerIds []int) ([]*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
mrMap := make(map[int]*gitlab.MergeRequest)
for _, id := range assigneeIds {
opts.AssigneeID = gitlab.AssigneeID(id)
assigneeMrs, err := listGroupMRsBase(client, projectID, opts)
if err != nil {
return nil, err
}
for _, mr := range assigneeMrs {
mrMap[mr.ID] = mr
}
}
opts.AssigneeID = nil // reset because it's Assignee OR Reviewer
for _, id := range reviewerIds {
opts.ReviewerID = gitlab.ReviewerID(id)
reviewerMrs, err := listGroupMRsBase(client, projectID, opts)
if err != nil {
return nil, err
}
for _, mr := range reviewerMrs {
mrMap[mr.ID] = mr
}
}
mrs := make([]*gitlab.MergeRequest, 0, len(mrMap))
for _, mr := range mrMap {
mrs = append(mrs, mr)
}
sort.Slice(mrs, func(i, j int) bool {
return mrs[i].CreatedAt.After(*mrs[j].CreatedAt)
})
return mrs, nil
}
var ProjectListMROptionsToGroup = func(l *gitlab.ListProjectMergeRequestsOptions) *gitlab.ListGroupMergeRequestsOptions {
return &gitlab.ListGroupMergeRequestsOptions{
ListOptions: l.ListOptions,
State: l.State,
OrderBy: l.OrderBy,
Sort: l.Sort,
Milestone: l.Milestone,
View: l.View,
Labels: l.Labels,
NotLabels: l.NotLabels,
WithLabelsDetails: l.WithLabelsDetails,
WithMergeStatusRecheck: l.WithMergeStatusRecheck,
CreatedAfter: l.CreatedAfter,
CreatedBefore: l.CreatedBefore,
UpdatedAfter: l.UpdatedAfter,
UpdatedBefore: l.UpdatedBefore,
Scope: l.Scope,
AuthorID: l.AuthorID,
AssigneeID: l.AssigneeID,
ReviewerID: l.ReviewerID,
ReviewerUsername: l.ReviewerUsername,
MyReactionEmoji: l.MyReactionEmoji,
SourceBranch: l.SourceBranch,
TargetBranch: l.TargetBranch,
Search: l.Search,
WIP: l.WIP,
}
}
// ListMRs retrieves merge requests for a given project with optional filtering by assignees or reviewers.
//
// Parameters:
// - client: A GitLab client instance.
// - projectID: The ID or name of the project.
// - opts: GitLab-specific options for listing merge requests.
// - listOpts: Optional list of arguments to filter by assignees or reviewers.
// May be any combination of api.WithMRAssignees and api.WithMRReviewers.
//
// Returns:
// - A slice of GitLab merge request objects and an error, if any.
//
// Example usage:
//
// mrs, err := api.ListMRs(client, "my-group", &gitlab.ListProjectMergeRequestsOptions{},
// api.WithMRAssignees([]int{123, 456}),
// api.WithMRReviewers([]int{789}))
var ListMRs = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions, listOpts ...CliListMROption) ([]*gitlab.MergeRequest, error) {
composedListOpts := composeCliListMROptions(listOpts...)
assigneeIds, reviewerIds := composedListOpts.assigneeIds, composedListOpts.reviewerIds
if len(assigneeIds) > 0 || len(reviewerIds) > 0 {
return listMRsWithAssigneesOrReviewers(client, projectID, opts, assigneeIds, reviewerIds)
} else {
return listMRsBase(client, projectID, opts)
}
}
var listMRsBase = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
mrs, _, err := client.MergeRequests.ListProjectMergeRequests(projectID, opts)
if err != nil {
return nil, err
}
return mrs, nil
}
var listMRsWithAssigneesOrReviewers = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListProjectMergeRequestsOptions, assigneeIds []int, reviewerIds []int) ([]*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
mrMap := make(map[int]*gitlab.MergeRequest)
for _, id := range assigneeIds {
opts.AssigneeID = gitlab.AssigneeID(id)
assigneeMrs, err := listMRsBase(client, projectID, opts)
if err != nil {
return nil, err
}
for _, mr := range assigneeMrs {
mrMap[mr.ID] = mr
}
}
opts.AssigneeID = nil // reset because it's Assignee OR Reviewer
for _, id := range reviewerIds {
opts.ReviewerID = gitlab.ReviewerID(id)
reviewerMrs, err := listMRsBase(client, projectID, opts)
if err != nil {
return nil, err
}
for _, mr := range reviewerMrs {
mrMap[mr.ID] = mr
}
}
mrs := make([]*gitlab.MergeRequest, 0, len(mrMap))
for _, mr := range mrMap {
mrs = append(mrs, mr)
}
sort.Slice(mrs, func(i, j int) bool {
return mrs[i].CreatedAt.After(*mrs[j].CreatedAt)
})
return mrs, nil
}
var UpdateMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.UpdateMergeRequestOptions) (*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
mr, _, err := client.MergeRequests.UpdateMergeRequest(projectID, mrID, opts)
if err != nil {
return nil, err
}
return mr, nil
}
var DeleteMR = func(client *gitlab.Client, projectID interface{}, mrID int) error {
if client == nil {
client = apiClient.Lab()
}
_, err := client.MergeRequests.DeleteMergeRequest(projectID, mrID)
if err != nil {
return err
}
return nil
}
var MergeMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.AcceptMergeRequestOptions) (*gitlab.MergeRequest, *gitlab.Response, error) {
if client == nil {
client = apiClient.Lab()
}
mrs, resp, err := client.MergeRequests.AcceptMergeRequest(projectID, mrID, opts)
if err != nil {
return nil, resp, err
}
return mrs, resp, nil
}
var CreateMR = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateMergeRequestOptions) (*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
mr, _, err := client.MergeRequests.CreateMergeRequest(projectID, opts)
if err != nil {
return nil, err
}
return mr, nil
}
var GetMRLinkedIssues = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.GetIssuesClosedOnMergeOptions) ([]*gitlab.Issue, error) {
if client == nil {
client = apiClient.Lab()
}
mrIssues, _, err := client.MergeRequests.GetIssuesClosedOnMerge(projectID, mrID, opts)
if err != nil {
return nil, err
}
return mrIssues, nil
}
var CreateMRNote = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.CreateMergeRequestNoteOptions) (*gitlab.Note, error) {
if client == nil {
client = apiClient.Lab()
}
note, _, err := client.Notes.CreateMergeRequestNote(projectID, mrID, opts)
if err != nil {
return note, err
}
return note, nil
}
var ListMRNotes = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.ListMergeRequestNotesOptions) ([]*gitlab.Note, error) {
if client == nil {
client = apiClient.Lab()
}
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
notes, _, err := client.Notes.ListMergeRequestNotes(projectID, mrID, opts)
if err != nil {
return notes, err
}
return notes, nil
}
var RebaseMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts *gitlab.RebaseMergeRequestOptions) error {
if client == nil {
client = apiClient.Lab()
}
_, err := client.MergeRequests.RebaseMergeRequest(projectID, mrID, opts)
if err != nil {
return err
}
return nil
}
var UnapproveMR = func(client *gitlab.Client, projectID interface{}, mrID int) error {
if client == nil {
client = apiClient.Lab()
}
_, err := client.MergeRequestApprovals.UnapproveMergeRequest(projectID, mrID)
if err != nil {
return err
}
return nil
}
var SubscribeToMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts gitlab.RequestOptionFunc) (*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
mr, _, err := client.MergeRequests.SubscribeToMergeRequest(projectID, mrID, opts)
if err != nil {
return nil, err
}
return mr, nil
}
var UnsubscribeFromMR = func(client *gitlab.Client, projectID interface{}, mrID int, opts gitlab.RequestOptionFunc) (*gitlab.MergeRequest, error) {
if client == nil {
client = apiClient.Lab()
}
mr, _, err := client.MergeRequests.UnsubscribeFromMergeRequest(projectID, mrID, opts)
if err != nil {
return nil, err
}
return mr, nil
}
var MRTodo = func(client *gitlab.Client, projectID interface{}, mrID int, opts gitlab.RequestOptionFunc) (*gitlab.Todo, error) {
if client == nil {
client = apiClient.Lab()
}
mr, resp, err := client.MergeRequests.CreateTodo(projectID, mrID, opts)
if resp.StatusCode == http.StatusNotModified {
return nil, ErrTodoExists
}
if err != nil {
return nil, err
}
return mr, nil
}
type cliListMROptions struct {
assigneeIds []int
reviewerIds []int
}
type CliListMROption func(*cliListMROptions)
func WithMRAssignees(assigneeIds []int) CliListMROption {
return func(c *cliListMROptions) {
c.assigneeIds = assigneeIds
}
}
func WithMRReviewers(reviewerIds []int) CliListMROption {
return func(c *cliListMROptions) {
c.reviewerIds = reviewerIds
}
}
func composeCliListMROptions(optionSetters ...CliListMROption) *cliListMROptions {
opts := &cliListMROptions{}
for _, setter := range optionSetters {
setter(opts)
}
return opts
}
|