diff options
author | Michael Muré <batolettre@gmail.com> | 2022-02-01 09:24:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-01 09:24:58 +0100 |
commit | 05d73e1b5321c97cd05133b5ae49d1798bc2fe5d (patch) | |
tree | 66eeb4ada52d282ad3ed9e231b3e74b42cbcfa48 /bridge/gitlab/gitlab_api.go | |
parent | 843da8e1f56504efb979f8e5eb43be5c176e7338 (diff) | |
parent | e888391b36307d0d4a1e12ba3a57b602c8b1528a (diff) | |
download | git-bug-05d73e1b5321c97cd05133b5ae49d1798bc2fe5d.tar.gz |
Merge pull request #571 from 5nord/add-new-gitlab-iterator
[gitlab] Add new iterator with state change events
Diffstat (limited to 'bridge/gitlab/gitlab_api.go')
-rw-r--r-- | bridge/gitlab/gitlab_api.go | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/bridge/gitlab/gitlab_api.go b/bridge/gitlab/gitlab_api.go new file mode 100644 index 00000000..c00baf9d --- /dev/null +++ b/bridge/gitlab/gitlab_api.go @@ -0,0 +1,140 @@ +package gitlab + +import ( + "context" + "time" + + "github.com/MichaelMure/git-bug/util/text" + "github.com/xanzy/go-gitlab" +) + +// Issues returns a channel with gitlab project issues, ascending order. +func Issues(ctx context.Context, client *gitlab.Client, pid string, since time.Time) <-chan *gitlab.Issue { + out := make(chan *gitlab.Issue) + + go func() { + defer close(out) + + opts := gitlab.ListProjectIssuesOptions{ + UpdatedAfter: &since, + Scope: gitlab.String("all"), + Sort: gitlab.String("asc"), + } + + for { + issues, resp, err := client.Issues.ListProjectIssues(pid, &opts, gitlab.WithContext(ctx)) + if err != nil { + return + } + + for _, issue := range issues { + out <- issue + } + + if resp.CurrentPage >= resp.TotalPages { + break + } + + opts.Page = resp.NextPage + } + }() + + return out +} + +// Notes returns a channel with note events +func Notes(ctx context.Context, client *gitlab.Client, issue *gitlab.Issue) <-chan Event { + out := make(chan Event) + + go func() { + defer close(out) + + opts := gitlab.ListIssueNotesOptions{ + OrderBy: gitlab.String("created_at"), + Sort: gitlab.String("asc"), + } + + for { + notes, resp, err := client.Notes.ListIssueNotes(issue.ProjectID, issue.IID, &opts, gitlab.WithContext(ctx)) + + if err != nil { + out <- ErrorEvent{Err: err, Time: time.Now()} + } + + for _, note := range notes { + out <- NoteEvent{*note} + } + + if resp.CurrentPage >= resp.TotalPages { + break + } + + opts.Page = resp.NextPage + } + }() + + return out +} + +// LabelEvents returns a channel with label events. +func LabelEvents(ctx context.Context, client *gitlab.Client, issue *gitlab.Issue) <-chan Event { + out := make(chan Event) + + go func() { + defer close(out) + + opts := gitlab.ListLabelEventsOptions{} + + for { + events, resp, err := client.ResourceLabelEvents.ListIssueLabelEvents(issue.ProjectID, issue.IID, &opts, gitlab.WithContext(ctx)) + + if err != nil { + out <- ErrorEvent{Err: err, Time: time.Now()} + } + + for _, e := range events { + le := LabelEvent{*e} + le.Label.Name = text.CleanupOneLine(le.Label.Name) + out <- le + } + + if resp.CurrentPage >= resp.TotalPages { + break + } + + opts.Page = resp.NextPage + } + }() + + return out +} + +// StateEvents returns a channel with state change events. +func StateEvents(ctx context.Context, client *gitlab.Client, issue *gitlab.Issue) <-chan Event { + out := make(chan Event) + + go func() { + defer close(out) + + opts := gitlab.ListStateEventsOptions{} + + for { + events, resp, err := client.ResourceStateEvents.ListIssueStateEvents(issue.ProjectID, issue.IID, &opts, gitlab.WithContext(ctx)) + if err != nil { + out <- ErrorEvent{Err: err, Time: time.Now()} + } + + for _, e := range events { + out <- StateEvent{*e} + } + + if resp.CurrentPage >= resp.TotalPages { + break + } + + opts.Page = resp.NextPage + } + }() + + return out +} |