github.com/secman-team/gh-api@v1.8.2/pkg/githubsearch/query.go (about)

     1  package githubsearch
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type EntityType string
     9  type State string
    10  type RepoVisibility string
    11  type SortField string
    12  type SortDirection int
    13  
    14  const (
    15  	Asc SortDirection = iota
    16  	Desc
    17  
    18  	UpdatedAt SortField = "updated"
    19  	CreatedAt SortField = "created"
    20  
    21  	Issue       EntityType = "issue"
    22  	PullRequest EntityType = "pr"
    23  
    24  	Open   State = "open"
    25  	Closed State = "closed"
    26  	Merged State = "merged"
    27  
    28  	Public  RepoVisibility = "public"
    29  	Private RepoVisibility = "private"
    30  )
    31  
    32  func NewQuery() *Query {
    33  	return &Query{}
    34  }
    35  
    36  type Query struct {
    37  	repo  string
    38  	owner string
    39  	sort  string
    40  	query string
    41  
    42  	entity     string
    43  	state      string
    44  	baseBranch string
    45  	headBranch string
    46  	labels     []string
    47  	assignee   string
    48  	author     string
    49  	mentions   string
    50  	milestone  string
    51  
    52  	language   string
    53  	forkState  string
    54  	visibility string
    55  	isArchived *bool
    56  }
    57  
    58  func (q *Query) InRepository(nameWithOwner string) {
    59  	q.repo = nameWithOwner
    60  }
    61  
    62  func (q *Query) OwnedBy(owner string) {
    63  	q.owner = owner
    64  }
    65  
    66  func (q *Query) SortBy(field SortField, direction SortDirection) {
    67  	var dir string
    68  	switch direction {
    69  	case Asc:
    70  		dir = "asc"
    71  	case Desc:
    72  		dir = "desc"
    73  	}
    74  	q.sort = fmt.Sprintf("%s-%s", field, dir)
    75  }
    76  
    77  func (q *Query) AddQuery(query string) {
    78  	q.query = query
    79  }
    80  
    81  func (q *Query) SetType(t EntityType) {
    82  	q.entity = string(t)
    83  }
    84  
    85  func (q *Query) SetState(s State) {
    86  	q.state = string(s)
    87  }
    88  
    89  func (q *Query) SetBaseBranch(name string) {
    90  	q.baseBranch = name
    91  }
    92  
    93  func (q *Query) SetHeadBranch(name string) {
    94  	q.headBranch = name
    95  }
    96  
    97  func (q *Query) AssignedTo(user string) {
    98  	q.assignee = user
    99  }
   100  
   101  func (q *Query) AuthoredBy(user string) {
   102  	q.author = user
   103  }
   104  
   105  func (q *Query) Mentions(handle string) {
   106  	q.mentions = handle
   107  }
   108  
   109  func (q *Query) InMilestone(title string) {
   110  	q.milestone = title
   111  }
   112  
   113  func (q *Query) AddLabel(name string) {
   114  	q.labels = append(q.labels, name)
   115  }
   116  
   117  func (q *Query) SetLanguage(name string) {
   118  	q.language = name
   119  }
   120  
   121  func (q *Query) SetVisibility(visibility RepoVisibility) {
   122  	q.visibility = string(visibility)
   123  }
   124  
   125  func (q *Query) OnlyForks() {
   126  	q.forkState = "only"
   127  }
   128  
   129  func (q *Query) IncludeForks(include bool) {
   130  	q.forkState = fmt.Sprintf("%v", include)
   131  }
   132  
   133  func (q *Query) SetArchived(isArchived bool) {
   134  	q.isArchived = &isArchived
   135  }
   136  
   137  func (q *Query) String() string {
   138  	var qs string
   139  
   140  	// context
   141  	if q.repo != "" {
   142  		qs += fmt.Sprintf("repo:%s ", q.repo)
   143  	} else if q.owner != "" {
   144  		qs += fmt.Sprintf("user:%s ", q.owner)
   145  	}
   146  
   147  	// type
   148  	if q.entity != "" {
   149  		qs += fmt.Sprintf("is:%s ", q.entity)
   150  	}
   151  	if q.state != "" {
   152  		qs += fmt.Sprintf("is:%s ", q.state)
   153  	}
   154  
   155  	// repositories
   156  	if q.visibility != "" {
   157  		qs += fmt.Sprintf("is:%s ", q.visibility)
   158  	}
   159  	if q.language != "" {
   160  		qs += fmt.Sprintf("language:%s ", quote(q.language))
   161  	}
   162  	if q.forkState != "" {
   163  		qs += fmt.Sprintf("fork:%s ", q.forkState)
   164  	}
   165  	if q.isArchived != nil {
   166  		qs += fmt.Sprintf("archived:%v ", *q.isArchived)
   167  	}
   168  
   169  	// issues
   170  	if q.assignee != "" {
   171  		qs += fmt.Sprintf("assignee:%s ", q.assignee)
   172  	}
   173  	for _, label := range q.labels {
   174  		qs += fmt.Sprintf("label:%s ", quote(label))
   175  	}
   176  	if q.author != "" {
   177  		qs += fmt.Sprintf("author:%s ", q.author)
   178  	}
   179  	if q.mentions != "" {
   180  		qs += fmt.Sprintf("mentions:%s ", q.mentions)
   181  	}
   182  	if q.milestone != "" {
   183  		qs += fmt.Sprintf("milestone:%s ", quote(q.milestone))
   184  	}
   185  
   186  	// pull requests
   187  	if q.baseBranch != "" {
   188  		qs += fmt.Sprintf("base:%s ", quote(q.baseBranch))
   189  	}
   190  	if q.headBranch != "" {
   191  		qs += fmt.Sprintf("head:%s ", quote(q.headBranch))
   192  	}
   193  
   194  	if q.sort != "" {
   195  		qs += fmt.Sprintf("sort:%s ", q.sort)
   196  	}
   197  	return strings.TrimRight(qs+q.query, " ")
   198  }
   199  
   200  func quote(v string) string {
   201  	if strings.ContainsAny(v, " \"\t\r\n") {
   202  		return fmt.Sprintf("%q", v)
   203  	}
   204  	return v
   205  }