Skip to content

Commit

Permalink
refactor list branches
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
  • Loading branch information
PetrusZ committed May 27, 2024
1 parent 1d8fbcf commit 16242a8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/code/client/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Client) ListBranches(opt client.ListOpt) ([]*client.Branch, error) {
bList, err := c.Client.ListBranches(opt.Namespace, opt.ProjectName, opt.Key, &gitlab.ListOptions{
Page: opt.Page,
PerPage: opt.PerPage,
NoPaginated: true,
NoPaginated: opt.GetAll,
})
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/microservice/aslan/core/code/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ListOpt struct {
Page int
PerPage int
TargetBranch string
GetAll bool
}

type Branch struct {
Expand Down
40 changes: 30 additions & 10 deletions pkg/microservice/aslan/core/code/handler/codehost.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,40 @@ func ListRepoInfos(c *gin.Context) {
ctx.Resp, ctx.Err = service.ListRepoInfos(args.Infos, ctx.Logger)
}

type BranchesRequest struct {
Regular string `json:"regular"`
Branches []string `json:"branches"`
}

func MatchBranchesList(c *gin.Context) {
ctx := internalhandler.NewContext(c)
defer func() { internalhandler.JSONResponse(c, ctx) }()

args := new(BranchesRequest)
err := c.ShouldBindJSON(args)
if err != nil {
ctx.Err = e.ErrInvalidParam.AddDesc("invalid branches args")
codehostID := c.Param("codehostId")
repoOwner := c.Query("repoOwner")
repoName := c.Query("repoName") // pro Name, id/name -> gitlab = id
regular := c.Query("regular")

if codehostID == "" {
ctx.Err = e.ErrInvalidParam.AddDesc("empty codehostId")
return
}
if repoOwner == "" {
ctx.Err = e.ErrInvalidParam.AddDesc("empty repoOwner")
return
}
ctx.Resp = service.MatchBranchesList(args.Regular, args.Branches)
if repoName == "" {
ctx.Err = e.ErrInvalidParam.AddDesc("empty repoName")
return
}
if regular == "" {
ctx.Err = e.ErrInvalidParam.AddDesc("empty regular")
return
}

chID, _ := strconv.Atoi(codehostID)
ctx.Resp, ctx.Err = service.MatchBranchesList(
chID,
repoName,
strings.Replace(repoOwner, "%2F", "/", -1),
"",
1,
500,
regular,
ctx.Logger)
}
2 changes: 1 addition & 1 deletion pkg/microservice/aslan/core/code/handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (*Router) Inject(router *gin.RouterGroup) {
codehost.GET("/:codehostId/prs", CodeHostGetPRList)
codehost.GET("/:codehostId/commits", CodeHostGetCommits)
codehost.PUT("/infos", ListRepoInfos)
codehost.POST("/branches/regular/check", MatchBranchesList)
codehost.GET("/:codehostId/branches/regular/check", MatchBranchesList)
}

// ---------------------------------------------------------------------------------------
Expand Down
27 changes: 23 additions & 4 deletions pkg/microservice/aslan/core/code/service/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,31 @@ func ListRepoInfos(infos []*GitRepoInfo, log *zap.SugaredLogger) ([]*GitRepoInfo
return infos, nil
}

func MatchBranchesList(regular string, branches []string) []string {
matchBranches := make([]string, 0)
func MatchBranchesList(codeHostID int, projectName, namespace, key string, page, perPage int, regular string, log *zap.SugaredLogger) ([]*client.Branch, error) {
ch, err := systemconfig.New().GetCodeHost(codeHostID)
if err != nil {
log.Errorf("get code host info err:%s", err)
return nil, err
}
if ch.Type == setting.SourceFromOther {
return []*client.Branch{}, nil
}
cli, err := open.OpenClient(ch, log)
if err != nil {
log.Errorf("open client err:%s", err)
return nil, err
}
branches, err := cli.ListBranches(client.ListOpt{Namespace: namespace, ProjectName: projectName, Key: key, Page: page, PerPage: perPage, GetAll: true})
if err != nil {
log.Errorf("list branch err:%s", err)
return nil, err
}

matchBranches := make([]*client.Branch, 0)
for _, branch := range branches {
if matched, _ := regexp.MatchString(regular, branch); matched {
if matched, _ := regexp.MatchString(regular, branch.Name); matched {
matchBranches = append(matchBranches, branch)
}
}
return matchBranches
return matchBranches, nil
}
19 changes: 15 additions & 4 deletions pkg/tool/git/gitlab/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ limitations under the License.

package gitlab

import "github.com/xanzy/go-gitlab"
import (
"github.com/koderover/zadig/v2/pkg/tool/log"
"github.com/xanzy/go-gitlab"
)

// ListBranches lists branches by projectID <- urlEncode(namespace/projectName)
func (c *Client) ListBranches(owner, repo, key string, opts *ListOptions) ([]*gitlab.Branch, error) {
got := 0
req := opts.PerPage
limit := 100
res := []*gitlab.Branch{}

req := opts.PerPage
opts.Page = 1
opts.PerPage = limit
res := []*gitlab.Branch{}

log.Debugf("opts: %+v", opts)

log.Debugf("got: %v, req: %v", got, req)
for got < req {
branches, err := wrap(paginated(func(o *gitlab.ListOptions) ([]interface{}, *gitlab.Response, error) {
bs, r, err := c.Branches.ListBranches(generateProjectName(owner, repo), &gitlab.ListBranchesOptions{ListOptions: *o, Search: &key})
Expand All @@ -50,9 +56,14 @@ func (c *Client) ListBranches(owner, repo, key string, opts *ListOptions) ([]*gi
}

got += len(bs)
log.Debugf("got: %v, req: %v", got, req)
if len(bs) < limit {
break
}
if opts.NoPaginated {
break
}

opts.Page++
}

Expand Down

0 comments on commit 16242a8

Please sign in to comment.