You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-scm/scm/driver/bitbucket/util.go

152 lines
3.6 KiB
Go

// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bitbucket
import (
"net/url"
"regexp"
"strconv"
"strings"
"git.awesome-for.me/liuzhiguo/go-scm/scm"
)
// regex for git author fields ("name <name@mail.tld>")
var reGitMail = regexp.MustCompile("<(.*)>")
// extracts the email from a git commit author string
func extractEmail(gitauthor string) (author string) {
matches := reGitMail.FindAllStringSubmatch(gitauthor, -1)
if len(matches) == 1 {
author = matches[0][1]
}
return
}
func encodeBranchListOptions(opts scm.BranchListOptions) string {
params := url.Values{}
if opts.SearchTerm != "" {
var sb strings.Builder
sb.WriteString("name~\"")
sb.WriteString(opts.SearchTerm)
sb.WriteString("\"")
params.Set("q", sb.String())
}
if opts.PageListOptions != (scm.ListOptions{}) {
if opts.PageListOptions.Page != 0 {
params.Set("page", strconv.Itoa(opts.PageListOptions.Page))
}
if opts.PageListOptions.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.PageListOptions.Size))
}
}
return params.Encode()
}
func encodeListOptions(opts scm.ListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.Size))
}
return params.Encode()
}
func encodeListRoleOptions(opts scm.ListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.Size))
}
params.Set("role", "member")
return params.Encode()
}
func encodeRepoListOptions(opts scm.RepoListOptions) string {
params := url.Values{}
if opts.RepoSearchTerm.RepoName != "" {
var sb strings.Builder
sb.WriteString("name~\"")
sb.WriteString(opts.RepoSearchTerm.RepoName)
sb.WriteString("\"")
params.Set("q", sb.String())
}
if opts.ListOptions != (scm.ListOptions{}) {
if opts.ListOptions.Page != 0 {
params.Set("page", strconv.Itoa(opts.ListOptions.Page))
}
if opts.ListOptions.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.ListOptions.Size))
}
}
params.Set("role", "member")
return params.Encode()
}
func encodeCommitListOptions(opts scm.CommitListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.Size))
}
if opts.Path != "" {
params.Set("path", opts.Path)
}
return params.Encode()
}
func encodeIssueListOptions(opts scm.IssueListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.Size))
}
if opts.Open && opts.Closed {
params.Set("state", "all")
} else if opts.Closed {
params.Set("state", "closed")
}
return params.Encode()
}
func encodePullRequestListOptions(opts scm.PullRequestListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("pagelen", strconv.Itoa(opts.Size))
}
if opts.Open && opts.Closed {
params.Set("state", "all")
} else if opts.Closed {
params.Set("state", "closed")
}
return params.Encode()
}
func copyPagination(from pagination, to *scm.Response) error {
if to == nil {
return nil
}
to.Page.NextURL = from.Next
uri, err := url.Parse(from.Next)
if err != nil {
return err
}
page := uri.Query().Get("page")
to.Page.First = 1
to.Page.Next, _ = strconv.Atoi(page)
return nil
}