2020-02-16 06:51:14 +01:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2020-02-27 05:58:02 +01:00
|
|
|
"strings"
|
2020-05-07 03:09:31 +02:00
|
|
|
|
2020-09-17 18:25:09 +02:00
|
|
|
"gitea.com/jolheiser/sip/qualify"
|
2020-05-07 03:09:31 +02:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-02-16 06:51:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetIssues returns all matching Issues from a Gitea instance
|
|
|
|
func GetIssues(client *gitea.Client, owner, repo string, opts gitea.ListIssueOption) ([]*gitea.Issue, error) {
|
|
|
|
issues := make([]*gitea.Issue, 0)
|
|
|
|
p := 1
|
|
|
|
for {
|
|
|
|
opts.Page = p
|
2020-09-15 20:02:45 +02:00
|
|
|
list, _, err := client.ListRepoIssues(owner, repo, opts)
|
2020-02-16 06:51:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return issues, err
|
|
|
|
}
|
|
|
|
p++
|
|
|
|
issues = append(issues, list...)
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return issues, nil
|
|
|
|
}
|
2020-02-27 05:58:02 +01:00
|
|
|
|
|
|
|
type State int
|
|
|
|
|
|
|
|
const (
|
|
|
|
None State = iota
|
|
|
|
Open
|
|
|
|
Closed
|
|
|
|
Merged
|
|
|
|
)
|
|
|
|
|
|
|
|
type IssueFilter struct {
|
|
|
|
Query string
|
|
|
|
State State
|
|
|
|
Author string
|
|
|
|
Labels []string
|
|
|
|
Milestone string
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocognit
|
|
|
|
// This function is high in cognitive complexity,
|
|
|
|
// however it is hopefully structured to be less painful than gocognit thinks
|
|
|
|
func (f *IssueFilter) Match(issue *gitea.Issue) bool {
|
|
|
|
|
|
|
|
// Filter out state
|
|
|
|
if f.State == Open && issue.State != gitea.StateOpen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.State == Closed && issue.State != gitea.StateClosed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.State == Merged && (issue.PullRequest == nil || !issue.PullRequest.HasMerged) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out author
|
|
|
|
if f.Author != "" && (issue.Poster == nil || !strings.EqualFold(f.Author, issue.Poster.UserName)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out labels
|
|
|
|
for _, fl := range f.Labels {
|
|
|
|
hasLabel := false
|
|
|
|
for _, il := range issue.Labels {
|
|
|
|
if fl == strings.ToLower(il.Name) {
|
|
|
|
hasLabel = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasLabel {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out milestone
|
|
|
|
if f.Milestone != "" && (issue.Milestone == nil || !strings.EqualFold(f.Milestone, issue.Milestone.Title)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIssueFilter(query string) *IssueFilter {
|
|
|
|
filter := &IssueFilter{}
|
2020-03-06 05:18:36 +01:00
|
|
|
filters := qualify.Parse(query, "is", "author", "label", "milestone")
|
|
|
|
|
|
|
|
if state, ok := filters["is"]; ok {
|
|
|
|
switch state.Last() {
|
|
|
|
case "open", "opened":
|
|
|
|
filter.State = Open
|
|
|
|
case "close", "closed":
|
|
|
|
filter.State = Closed
|
|
|
|
case "merge", "merged":
|
|
|
|
filter.State = Merged
|
|
|
|
default:
|
|
|
|
filter.State = None
|
2020-02-27 05:58:02 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 05:18:36 +01:00
|
|
|
|
|
|
|
if author, ok := filters["author"]; ok {
|
|
|
|
filter.Author = author.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
if label, ok := filters["label"]; ok {
|
|
|
|
filter.Labels = append(filter.Labels, label...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if milestone, ok := filters["milestone"]; ok {
|
|
|
|
filter.Milestone = milestone.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
filter.Query = filters.Query()
|
|
|
|
|
2020-02-27 05:58:02 +01:00
|
|
|
return filter
|
|
|
|
}
|