2021-02-14 22:35:03 +03:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
2021-04-23 10:17:37 +03:00
|
|
|
// Issues returns a channel with gitlab project issues, ascending order.
|
2021-02-14 22:35:03 +03:00
|
|
|
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 {
|
2021-12-05 13:05:38 +03:00
|
|
|
issues, resp, err := client.Issues.ListProjectIssues(pid, &opts, gitlab.WithContext(ctx))
|
2021-02-14 22:35:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, issue := range issues {
|
|
|
|
out <- issue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.CurrentPage >= resp.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Page = resp.NextPage
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:17:37 +03:00
|
|
|
// Notes returns a channel with note events
|
2021-02-14 22:35:03 +03:00
|
|
|
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 {
|
2021-12-05 13:05:38 +03:00
|
|
|
notes, resp, err := client.Notes.ListIssueNotes(issue.ProjectID, issue.IID, &opts, gitlab.WithContext(ctx))
|
2021-02-14 22:35:03 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:17:37 +03:00
|
|
|
// LabelEvents returns a channel with label events.
|
2021-02-14 22:35:03 +03:00
|
|
|
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 {
|
2021-12-05 13:05:38 +03:00
|
|
|
events, resp, err := client.ResourceLabelEvents.ListIssueLabelEvents(issue.ProjectID, issue.IID, &opts, gitlab.WithContext(ctx))
|
2021-02-14 22:35:03 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:17:37 +03:00
|
|
|
// StateEvents returns a channel with state change events.
|
2021-02-14 22:35:03 +03:00
|
|
|
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 {
|
2021-12-05 13:05:38 +03:00
|
|
|
events, resp, err := client.ResourceStateEvents.ListIssueStateEvents(issue.ProjectID, issue.IID, &opts, gitlab.WithContext(ctx))
|
2021-02-14 22:35:03 +03:00
|
|
|
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
|
|
|
|
}
|