Refactor RepoSearch to be easy usable (#346)

Allow EdgeCases

Transform Uggly API to Prity Search Function

Update & add description of SearchRepoOptions

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/346
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
This commit is contained in:
6543 2020-07-11 19:54:57 +00:00 committed by lafriks
parent eb32e0454f
commit 1026b1014c
2 changed files with 113 additions and 39 deletions

View file

@ -58,6 +58,20 @@ type Repository struct {
AvatarURL string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
} }
// RepoType represent repo type
type RepoType string
const (
// RepoTypeNone dont specify a type
RepoTypeNone RepoType = ""
// RepoTypeSource is the default repo type
RepoTypeSource RepoType = "source"
// RepoTypeFork is a repo witch was forked from an other one
RepoTypeFork RepoType = "fork"
// RepoTypeMirror represents an mirror repo
RepoTypeMirror RepoType = "mirror"
)
// ListReposOptions options for listing repositories // ListReposOptions options for listing repositories
type ListReposOptions struct { type ListReposOptions struct {
ListOptions ListOptions
@ -92,17 +106,52 @@ func (c *Client) ListOrgRepos(org string, opt ListOrgReposOptions) ([]*Repositor
// SearchRepoOptions options for searching repositories // SearchRepoOptions options for searching repositories
type SearchRepoOptions struct { type SearchRepoOptions struct {
ListOptions ListOptions
// The keyword to query
Keyword string Keyword string
Topic bool // Limit search to repositories with keyword as topic
IncludeDesc bool KeywordIsTopic bool
UID int64 // Include search of keyword within repository description
PriorityOwnerID int64 KeywordInDescription bool
StarredBy int64
Private bool /*
Template bool User Filter
Mode string */
Exclusive bool
// Repo Owner
OwnerID int64
// Stared By UserID
StarredByUserID int64
/*
Repo Attributes
*/
// pubic, private or all repositories (defaults to all)
IsPrivate *bool
// archived, non-archived or all repositories (defaults to all)
IsArchived *bool
// Exclude template repos from search
ExcludeTemplate bool
// Filter by "fork", "source", "mirror"
Type RepoType
/*
Sort Filters
*/
// sort repos by attribute. Supported values are "alpha", "created", "updated", "size", and "id". Default is "alpha"
Sort string Sort string
// sort order, either "asc" (ascending) or "desc" (descending). Default is "asc", ignored if "sort" is not specified.
Order string
// Repo owner to prioritize in the results
PrioritizedByOwnerID int64
/*
Cover EdgeCases
*/
// if set all other options are ignored and this string is used as query
RawQuery string
} }
// QueryEncode turns options into querystring argument // QueryEncode turns options into querystring argument
@ -111,34 +160,46 @@ func (opt *SearchRepoOptions) QueryEncode() string {
if opt.Keyword != "" { if opt.Keyword != "" {
query.Add("q", opt.Keyword) query.Add("q", opt.Keyword)
} }
if opt.KeywordIsTopic {
query.Add("topic", fmt.Sprintf("%t", opt.Topic)) query.Add("topic", "true")
query.Add("includeDesc", fmt.Sprintf("%t", opt.IncludeDesc)) }
if opt.KeywordInDescription {
if opt.UID > 0 { query.Add("includeDesc", "true")
query.Add("uid", fmt.Sprintf("%d", opt.UID))
} }
if opt.PriorityOwnerID > 0 { // User Filter
query.Add("priority_owner_id", fmt.Sprintf("%d", opt.PriorityOwnerID)) if opt.OwnerID > 0 {
query.Add("uid", fmt.Sprintf("%d", opt.OwnerID))
query.Add("exclusive", "true")
}
if opt.StarredByUserID > 0 {
query.Add("starredBy", fmt.Sprintf("%d", opt.StarredByUserID))
} }
if opt.StarredBy > 0 { // Repo Attributes
query.Add("starredBy", fmt.Sprintf("%d", opt.StarredBy)) if opt.IsPrivate != nil {
query.Add("is_private", fmt.Sprintf("%v", opt.IsPrivate))
}
if opt.IsArchived != nil {
query.Add("archived", fmt.Sprintf("%v", opt.IsArchived))
}
if opt.ExcludeTemplate {
query.Add("template", "false")
}
if len(opt.Type) != 0 {
query.Add("mode", string(opt.Type))
} }
query.Add("private", fmt.Sprintf("%t", opt.Private)) // Sort Filters
query.Add("template", fmt.Sprintf("%t", opt.Template))
if opt.Mode != "" {
query.Add("mode", opt.Mode)
}
query.Add("exclusive", fmt.Sprintf("%t", opt.Exclusive))
if opt.Sort != "" { if opt.Sort != "" {
query.Add("sort", opt.Sort) query.Add("sort", opt.Sort)
} }
if opt.PrioritizedByOwnerID > 0 {
query.Add("priority_owner_id", fmt.Sprintf("%d", opt.PrioritizedByOwnerID))
}
if opt.Order != "" {
query.Add("order", opt.Order)
}
return query.Encode() return query.Encode()
} }
@ -153,7 +214,20 @@ func (c *Client) SearchRepos(opt SearchRepoOptions) ([]*Repository, error) {
resp := new(searchRepoResponse) resp := new(searchRepoResponse)
link, _ := url.Parse("/repos/search") link, _ := url.Parse("/repos/search")
if len(opt.RawQuery) != 0 {
link.RawQuery = opt.RawQuery
} else {
link.RawQuery = opt.QueryEncode() link.RawQuery = opt.QueryEncode()
// IsPrivate only works on gitea >= 1.12.0
if err := c.CheckServerVersionConstraint(">=1.12.0"); err != nil && opt.IsPrivate != nil {
if *opt.IsPrivate {
// private repos only not supported on gitea <= 1.11.x
return nil, err
}
link.Query().Add("private", "false")
}
}
err := c.getParsedResponse("GET", link.String(), nil, nil, &resp) err := c.getParsedResponse("GET", link.String(), nil, nil, &resp)
return resp.Repos, err return resp.Repos, err

View file

@ -46,7 +46,7 @@ func TestSearchRepo(t *testing.T) {
repos, err := c.SearchRepos(SearchRepoOptions{ repos, err := c.SearchRepos(SearchRepoOptions{
Keyword: "Search1", Keyword: "Search1",
IncludeDesc: true, KeywordInDescription: true,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, repos) assert.NotNil(t, repos)
@ -54,7 +54,7 @@ func TestSearchRepo(t *testing.T) {
repos, err = c.SearchRepos(SearchRepoOptions{ repos, err = c.SearchRepos(SearchRepoOptions{
Keyword: "Search", Keyword: "Search",
IncludeDesc: true, KeywordInDescription: true,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, repos) assert.NotNil(t, repos)
@ -62,7 +62,7 @@ func TestSearchRepo(t *testing.T) {
repos, err = c.SearchRepos(SearchRepoOptions{ repos, err = c.SearchRepos(SearchRepoOptions{
Keyword: "TestTopic1", Keyword: "TestTopic1",
Topic: true, KeywordInDescription: true,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, repos) assert.NotNil(t, repos)
@ -70,7 +70,7 @@ func TestSearchRepo(t *testing.T) {
repos, err = c.SearchRepos(SearchRepoOptions{ repos, err = c.SearchRepos(SearchRepoOptions{
Keyword: "TestTopic2", Keyword: "TestTopic2",
Topic: true, KeywordInDescription: true,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, repos) assert.NotNil(t, repos)