...

Source file src/github.com/xanzy/go-gitlab/search.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  )
    23  
    24  // SearchService handles communication with the search related methods of the
    25  // GitLab API.
    26  //
    27  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html
    28  type SearchService struct {
    29  	client *Client
    30  }
    31  
    32  // SearchOptions represents the available options for all search methods.
    33  //
    34  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html
    35  type SearchOptions struct {
    36  	ListOptions
    37  	Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
    38  }
    39  
    40  type searchOptions struct {
    41  	SearchOptions
    42  	Scope  string `url:"scope" json:"scope"`
    43  	Search string `url:"search" json:"search"`
    44  }
    45  
    46  // Projects searches the expression within projects
    47  //
    48  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-projects
    49  func (s *SearchService) Projects(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Project, *Response, error) {
    50  	var ps []*Project
    51  	resp, err := s.search("projects", query, &ps, opt, options...)
    52  	return ps, resp, err
    53  }
    54  
    55  // ProjectsByGroup searches the expression within projects for
    56  // the specified group
    57  //
    58  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#group-search-api
    59  func (s *SearchService) ProjectsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Project, *Response, error) {
    60  	var ps []*Project
    61  	resp, err := s.searchByGroup(gid, "projects", query, &ps, opt, options...)
    62  	return ps, resp, err
    63  }
    64  
    65  // Issues searches the expression within issues
    66  //
    67  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-issues
    68  func (s *SearchService) Issues(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
    69  	var is []*Issue
    70  	resp, err := s.search("issues", query, &is, opt, options...)
    71  	return is, resp, err
    72  }
    73  
    74  // IssuesByGroup searches the expression within issues for
    75  // the specified group
    76  //
    77  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-issues-1
    78  func (s *SearchService) IssuesByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
    79  	var is []*Issue
    80  	resp, err := s.searchByGroup(gid, "issues", query, &is, opt, options...)
    81  	return is, resp, err
    82  }
    83  
    84  // IssuesByProject searches the expression within issues for
    85  // the specified project
    86  //
    87  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-issues-2
    88  func (s *SearchService) IssuesByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Issue, *Response, error) {
    89  	var is []*Issue
    90  	resp, err := s.searchByProject(pid, "issues", query, &is, opt, options...)
    91  	return is, resp, err
    92  }
    93  
    94  // MergeRequests searches the expression within merge requests
    95  //
    96  // GitLab API docs:
    97  // https://docs.gitlab.com/ee/api/search.html#scope-merge_requests
    98  func (s *SearchService) MergeRequests(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
    99  	var ms []*MergeRequest
   100  	resp, err := s.search("merge_requests", query, &ms, opt, options...)
   101  	return ms, resp, err
   102  }
   103  
   104  // MergeRequestsByGroup searches the expression within merge requests for
   105  // the specified group
   106  //
   107  // GitLab API docs:
   108  // https://docs.gitlab.com/ee/api/search.html#scope-merge_requests-1
   109  func (s *SearchService) MergeRequestsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
   110  	var ms []*MergeRequest
   111  	resp, err := s.searchByGroup(gid, "merge_requests", query, &ms, opt, options...)
   112  	return ms, resp, err
   113  }
   114  
   115  // MergeRequestsByProject searches the expression within merge requests for
   116  // the specified project
   117  //
   118  // GitLab API docs:
   119  // https://docs.gitlab.com/ee/api/search.html#scope-merge_requests-2
   120  func (s *SearchService) MergeRequestsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*MergeRequest, *Response, error) {
   121  	var ms []*MergeRequest
   122  	resp, err := s.searchByProject(pid, "merge_requests", query, &ms, opt, options...)
   123  	return ms, resp, err
   124  }
   125  
   126  // Milestones searches the expression within milestones
   127  //
   128  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-milestones
   129  func (s *SearchService) Milestones(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
   130  	var ms []*Milestone
   131  	resp, err := s.search("milestones", query, &ms, opt, options...)
   132  	return ms, resp, err
   133  }
   134  
   135  // MilestonesByGroup searches the expression within milestones for
   136  // the specified group
   137  //
   138  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-milestones-1
   139  func (s *SearchService) MilestonesByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
   140  	var ms []*Milestone
   141  	resp, err := s.searchByGroup(gid, "milestones", query, &ms, opt, options...)
   142  	return ms, resp, err
   143  }
   144  
   145  // MilestonesByProject searches the expression within milestones for
   146  // the specified project
   147  //
   148  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-milestones-2
   149  func (s *SearchService) MilestonesByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Milestone, *Response, error) {
   150  	var ms []*Milestone
   151  	resp, err := s.searchByProject(pid, "milestones", query, &ms, opt, options...)
   152  	return ms, resp, err
   153  }
   154  
   155  // SnippetTitles searches the expression within snippet titles
   156  //
   157  // GitLab API docs:
   158  // https://docs.gitlab.com/ee/api/search.html#scope-snippet_titles
   159  func (s *SearchService) SnippetTitles(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Snippet, *Response, error) {
   160  	var ss []*Snippet
   161  	resp, err := s.search("snippet_titles", query, &ss, opt, options...)
   162  	return ss, resp, err
   163  }
   164  
   165  // SnippetBlobs searches the expression within snippet blobs
   166  //
   167  // GitLab API docs:
   168  // https://docs.gitlab.com/ee/api/search.html#scope-snippet_blobs
   169  func (s *SearchService) SnippetBlobs(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Snippet, *Response, error) {
   170  	var ss []*Snippet
   171  	resp, err := s.search("snippet_blobs", query, &ss, opt, options...)
   172  	return ss, resp, err
   173  }
   174  
   175  // NotesByProject searches the expression within notes for the specified
   176  // project
   177  //
   178  // GitLab API docs: // https://docs.gitlab.com/ee/api/search.html#scope-notes
   179  func (s *SearchService) NotesByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Note, *Response, error) {
   180  	var ns []*Note
   181  	resp, err := s.searchByProject(pid, "notes", query, &ns, opt, options...)
   182  	return ns, resp, err
   183  }
   184  
   185  // WikiBlobs searches the expression within all wiki blobs
   186  //
   187  // GitLab API docs:
   188  // https://docs.gitlab.com/ee/api/search.html#scope-wiki_blobs
   189  func (s *SearchService) WikiBlobs(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Wiki, *Response, error) {
   190  	var ws []*Wiki
   191  	resp, err := s.search("wiki_blobs", query, &ws, opt, options...)
   192  	return ws, resp, err
   193  }
   194  
   195  // WikiBlobsByGroup searches the expression within wiki blobs for
   196  // specified group
   197  //
   198  // GitLab API docs:
   199  // https://docs.gitlab.com/ee/api/search.html#scope-wiki_blobs-premium-1
   200  func (s *SearchService) WikiBlobsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Wiki, *Response, error) {
   201  	var ws []*Wiki
   202  	resp, err := s.searchByGroup(gid, "wiki_blobs", query, &ws, opt, options...)
   203  	return ws, resp, err
   204  }
   205  
   206  // WikiBlobsByProject searches the expression within wiki blobs for
   207  // the specified project
   208  //
   209  // GitLab API docs:
   210  // https://docs.gitlab.com/ee/api/search.html#scope-wiki_blobs-premium-2
   211  func (s *SearchService) WikiBlobsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Wiki, *Response, error) {
   212  	var ws []*Wiki
   213  	resp, err := s.searchByProject(pid, "wiki_blobs", query, &ws, opt, options...)
   214  	return ws, resp, err
   215  }
   216  
   217  // Commits searches the expression within all commits
   218  //
   219  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-commits
   220  func (s *SearchService) Commits(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
   221  	var cs []*Commit
   222  	resp, err := s.search("commits", query, &cs, opt, options...)
   223  	return cs, resp, err
   224  }
   225  
   226  // CommitsByGroup searches the expression within commits for the specified
   227  // group
   228  //
   229  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-commits-premium-1
   230  func (s *SearchService) CommitsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
   231  	var cs []*Commit
   232  	resp, err := s.searchByGroup(gid, "commits", query, &cs, opt, options...)
   233  	return cs, resp, err
   234  }
   235  
   236  // CommitsByProject searches the expression within commits for the
   237  // specified project
   238  //
   239  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-commits-premium-2
   240  func (s *SearchService) CommitsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Commit, *Response, error) {
   241  	var cs []*Commit
   242  	resp, err := s.searchByProject(pid, "commits", query, &cs, opt, options...)
   243  	return cs, resp, err
   244  }
   245  
   246  // Blob represents a single blob.
   247  type Blob struct {
   248  	Basename  string `json:"basename"`
   249  	Data      string `json:"data"`
   250  	Path      string `json:"path"`
   251  	Filename  string `json:"filename"`
   252  	ID        string `json:"id"`
   253  	Ref       string `json:"ref"`
   254  	Startline int    `json:"startline"`
   255  	ProjectID int    `json:"project_id"`
   256  }
   257  
   258  // Blobs searches the expression within all blobs
   259  //
   260  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-blobs
   261  func (s *SearchService) Blobs(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Blob, *Response, error) {
   262  	var bs []*Blob
   263  	resp, err := s.search("blobs", query, &bs, opt, options...)
   264  	return bs, resp, err
   265  }
   266  
   267  // BlobsByGroup searches the expression within blobs for the specified
   268  // group
   269  //
   270  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-blobs-premium-1
   271  func (s *SearchService) BlobsByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Blob, *Response, error) {
   272  	var bs []*Blob
   273  	resp, err := s.searchByGroup(gid, "blobs", query, &bs, opt, options...)
   274  	return bs, resp, err
   275  }
   276  
   277  // BlobsByProject searches the expression within blobs for the specified
   278  // project
   279  //
   280  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-blobs-premium-2
   281  func (s *SearchService) BlobsByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*Blob, *Response, error) {
   282  	var bs []*Blob
   283  	resp, err := s.searchByProject(pid, "blobs", query, &bs, opt, options...)
   284  	return bs, resp, err
   285  }
   286  
   287  // Users searches the expression within all users
   288  //
   289  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-users
   290  func (s *SearchService) Users(query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
   291  	var ret []*User
   292  	resp, err := s.search("users", query, &ret, opt, options...)
   293  	return ret, resp, err
   294  }
   295  
   296  // UsersByGroup searches the expression within users for the specified
   297  // group
   298  //
   299  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-users-1
   300  func (s *SearchService) UsersByGroup(gid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
   301  	var ret []*User
   302  	resp, err := s.searchByGroup(gid, "users", query, &ret, opt, options...)
   303  	return ret, resp, err
   304  }
   305  
   306  // UsersByProject searches the expression within users for the
   307  // specified project
   308  //
   309  // GitLab API docs: https://docs.gitlab.com/ee/api/search.html#scope-users-2
   310  func (s *SearchService) UsersByProject(pid interface{}, query string, opt *SearchOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
   311  	var ret []*User
   312  	resp, err := s.searchByProject(pid, "users", query, &ret, opt, options...)
   313  	return ret, resp, err
   314  }
   315  
   316  func (s *SearchService) search(scope, query string, result interface{}, opt *SearchOptions, options ...RequestOptionFunc) (*Response, error) {
   317  	opts := &searchOptions{SearchOptions: *opt, Scope: scope, Search: query}
   318  
   319  	req, err := s.client.NewRequest(http.MethodGet, "search", opts, options)
   320  	if err != nil {
   321  		return nil, err
   322  	}
   323  
   324  	return s.client.Do(req, result)
   325  }
   326  
   327  func (s *SearchService) searchByGroup(gid interface{}, scope, query string, result interface{}, opt *SearchOptions, options ...RequestOptionFunc) (*Response, error) {
   328  	group, err := parseID(gid)
   329  	if err != nil {
   330  		return nil, err
   331  	}
   332  	u := fmt.Sprintf("groups/%s/-/search", PathEscape(group))
   333  
   334  	opts := &searchOptions{SearchOptions: *opt, Scope: scope, Search: query}
   335  
   336  	req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
   337  	if err != nil {
   338  		return nil, err
   339  	}
   340  
   341  	return s.client.Do(req, result)
   342  }
   343  
   344  func (s *SearchService) searchByProject(pid interface{}, scope, query string, result interface{}, opt *SearchOptions, options ...RequestOptionFunc) (*Response, error) {
   345  	project, err := parseID(pid)
   346  	if err != nil {
   347  		return nil, err
   348  	}
   349  	u := fmt.Sprintf("projects/%s/-/search", PathEscape(project))
   350  
   351  	opts := &searchOptions{SearchOptions: *opt, Scope: scope, Search: query}
   352  
   353  	req, err := s.client.NewRequest(http.MethodGet, u, opts, options)
   354  	if err != nil {
   355  		return nil, err
   356  	}
   357  
   358  	return s.client.Do(req, result)
   359  }
   360  

View as plain text