2019-02-21 15:06:34 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-06-11 09:07:39 +00:00
|
|
|
"regexp"
|
2022-03-04 05:41:28 +00:00
|
|
|
"sort"
|
2019-02-21 15:06:34 +00:00
|
|
|
"strconv"
|
2019-06-11 09:07:39 +00:00
|
|
|
"strings"
|
2019-02-21 15:06:34 +00:00
|
|
|
"time"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
gitsource "agola.io/agola/internal/gitsources"
|
2019-05-23 09:23:14 +00:00
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
gitlab "github.com/xanzy/go-gitlab"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-03-04 05:32:45 +00:00
|
|
|
PAGE_MAX = 100
|
2019-02-21 15:06:34 +00:00
|
|
|
GitlabOauth2Scopes = []string{"api"}
|
2019-06-11 09:07:39 +00:00
|
|
|
|
|
|
|
branchRefPrefix = "refs/heads/"
|
|
|
|
tagRefPrefix = "refs/tags/"
|
|
|
|
pullRequestRefRegex = regexp.MustCompile("refs/merge-requests/(.*)/head")
|
|
|
|
pullRequestRefFmt = "refs/merge-requests/%s/head"
|
2019-02-21 15:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Opts struct {
|
2019-05-15 21:46:21 +00:00
|
|
|
APIURL string
|
2019-02-21 15:06:34 +00:00
|
|
|
Token string
|
|
|
|
SkipVerify bool
|
|
|
|
Oauth2ClientID string
|
|
|
|
Oauth2Secret string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2020-02-11 14:54:53 +00:00
|
|
|
client *gitlab.Client
|
|
|
|
oauth2HTTPClient *http.Client
|
|
|
|
APIURL string
|
|
|
|
oauth2ClientID string
|
|
|
|
oauth2Secret string
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 09:07:12 +00:00
|
|
|
// fromCommitStatus converts a gitsource commit status to a gitlab commit status
|
2019-04-29 15:35:07 +00:00
|
|
|
func fromCommitStatus(status gitsource.CommitStatus) gitlab.BuildStateValue {
|
|
|
|
switch status {
|
|
|
|
case gitsource.CommitStatusPending:
|
|
|
|
return gitlab.Pending
|
|
|
|
case gitsource.CommitStatusSuccess:
|
|
|
|
return gitlab.Success
|
2019-05-15 09:26:50 +00:00
|
|
|
case gitsource.CommitStatusError:
|
|
|
|
return gitlab.Failed
|
2019-04-29 15:35:07 +00:00
|
|
|
case gitsource.CommitStatusFailed:
|
|
|
|
return gitlab.Failed
|
|
|
|
default:
|
2022-02-22 14:01:29 +00:00
|
|
|
panic(errors.Errorf("unknown commit status %q", status))
|
2019-04-29 15:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
func New(opts Opts) (*Client, error) {
|
|
|
|
// copied from net/http until it has a clone function: https://github.com/golang/go/issues/26013
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify},
|
|
|
|
}
|
|
|
|
httpClient := &http.Client{Transport: transport}
|
2020-02-11 14:54:53 +00:00
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
client := gitlab.NewOAuthClient(httpClient, opts.Token)
|
2019-07-02 14:10:39 +00:00
|
|
|
if err := client.SetBaseURL(opts.APIURL); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to set gitlab client base url")
|
2019-07-02 14:10:39 +00:00
|
|
|
}
|
2019-02-21 15:06:34 +00:00
|
|
|
|
|
|
|
return &Client{
|
2020-02-11 14:54:53 +00:00
|
|
|
client: client,
|
|
|
|
oauth2HTTPClient: httpClient,
|
|
|
|
APIURL: opts.APIURL,
|
|
|
|
oauth2ClientID: opts.Oauth2ClientID,
|
|
|
|
oauth2Secret: opts.Oauth2Secret,
|
2019-02-21 15:06:34 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:39:59 +00:00
|
|
|
func (c *Client) oauth2Config(callbackURL string) *oauth2.Config {
|
|
|
|
return &oauth2.Config{
|
2019-02-21 15:06:34 +00:00
|
|
|
ClientID: c.oauth2ClientID,
|
|
|
|
ClientSecret: c.oauth2Secret,
|
|
|
|
Scopes: GitlabOauth2Scopes,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
2019-05-15 21:46:21 +00:00
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", c.APIURL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", c.APIURL),
|
2019-02-21 15:06:34 +00:00
|
|
|
},
|
|
|
|
RedirectURL: callbackURL,
|
|
|
|
}
|
2019-04-29 13:39:59 +00:00
|
|
|
}
|
2019-02-21 15:06:34 +00:00
|
|
|
|
2019-04-29 13:39:59 +00:00
|
|
|
func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) {
|
|
|
|
var config = c.oauth2Config(callbackURL)
|
2019-02-21 15:06:34 +00:00
|
|
|
return config.AuthCodeURL(state), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
|
2020-02-11 14:54:53 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
|
|
|
|
|
2019-04-29 13:39:59 +00:00
|
|
|
var config = c.oauth2Config(callbackURL)
|
2020-02-11 14:54:53 +00:00
|
|
|
token, err := config.Exchange(ctx, code)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "cannot get oauth2 token")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:39:59 +00:00
|
|
|
func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
|
2020-02-11 14:54:53 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
|
|
|
|
|
2019-04-29 13:39:59 +00:00
|
|
|
var config = c.oauth2Config("")
|
|
|
|
token := &oauth2.Token{RefreshToken: refreshToken}
|
2020-02-11 14:54:53 +00:00
|
|
|
ts := config.TokenSource(ctx, token)
|
2022-02-22 14:01:29 +00:00
|
|
|
ntoken, err := ts.Token()
|
|
|
|
|
|
|
|
return ntoken, errors.WithStack(err)
|
2019-04-29 13:39:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
|
2019-10-24 08:58:57 +00:00
|
|
|
rr, _, err := c.client.Projects.GetProject(repopath, nil)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-03 09:07:54 +00:00
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
return fromGitlabRepo(rr), nil
|
2019-04-03 09:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) {
|
|
|
|
user, _, err := c.client.Users.CurrentUser()
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
return &gitsource.UserInfo{
|
|
|
|
ID: strconv.Itoa(user.ID),
|
|
|
|
LoginName: user.Username,
|
|
|
|
Email: user.Email,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) {
|
|
|
|
f, _, err := c.client.RepositoryFiles.GetFile(repopath, file, &gitlab.GetFileOptions{Ref: gitlab.String(commit)})
|
2019-05-29 13:51:34 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-29 13:51:34 +00:00
|
|
|
}
|
2019-02-21 15:06:34 +00:00
|
|
|
data, err := base64.StdEncoding.DecodeString(f.Content)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return data, errors.WithStack(err)
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) CreateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
2019-05-23 09:23:14 +00:00
|
|
|
if _, _, err := c.client.DeployKeys.AddDeployKey(repopath, &gitlab.AddDeployKeyOptions{
|
2019-02-21 15:06:34 +00:00
|
|
|
Title: gitlab.String(title),
|
|
|
|
Key: gitlab.String(pubKey),
|
2019-05-23 09:23:14 +00:00
|
|
|
}); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error creating deploy key")
|
2019-05-23 09:23:14 +00:00
|
|
|
}
|
2019-02-21 15:06:34 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) UpdateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
|
|
|
keys, _, err := c.client.DeployKeys.ListProjectDeployKeys(repopath, nil)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.Title == title {
|
|
|
|
if key.Key == pubKey {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-03 13:01:21 +00:00
|
|
|
if _, err := c.client.DeployKeys.DeleteDeployKey(repopath, key.ID); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error removing existing deploy key")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
if _, _, err := c.client.DeployKeys.AddDeployKey(repopath, &gitlab.AddDeployKeyOptions{
|
2019-02-21 15:06:34 +00:00
|
|
|
Title: &title,
|
|
|
|
Key: &pubKey,
|
|
|
|
}); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error creating deploy key")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) DeleteDeployKey(repopath, title string) error {
|
|
|
|
keys, _, err := c.client.DeployKeys.ListProjectDeployKeys(repopath, nil)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.Title == title {
|
2019-04-03 13:01:21 +00:00
|
|
|
if _, err := c.client.DeployKeys.DeleteDeployKey(repopath, key.ID); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error removing existing deploy key")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) CreateRepoWebhook(repopath, url, secret string) error {
|
2019-02-21 15:06:34 +00:00
|
|
|
opts := &gitlab.AddProjectHookOptions{
|
2019-04-03 13:01:21 +00:00
|
|
|
URL: gitlab.String(url),
|
|
|
|
PushEvents: gitlab.Bool(true),
|
2019-04-03 13:01:21 +00:00
|
|
|
TagPushEvents: gitlab.Bool(true),
|
2019-04-03 13:01:21 +00:00
|
|
|
MergeRequestsEvents: gitlab.Bool(true),
|
2019-05-07 16:29:31 +00:00
|
|
|
Token: gitlab.String(secret),
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
2019-05-23 09:23:14 +00:00
|
|
|
if _, _, err := c.client.Projects.AddProjectHook(repopath, opts); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error creating repository webhook")
|
2019-05-23 09:23:14 +00:00
|
|
|
}
|
2019-02-21 15:06:34 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) DeleteRepoWebhook(repopath, u string) error {
|
|
|
|
hooks, _, err := c.client.Projects.ListProjectHooks(repopath, nil)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error retrieving repository webhooks")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 16:19:53 +00:00
|
|
|
// match the full url so we can have multiple webhooks for different agola
|
|
|
|
// projects
|
2019-02-21 15:06:34 +00:00
|
|
|
for _, hook := range hooks {
|
2019-02-28 16:19:53 +00:00
|
|
|
if hook.URL == u {
|
2019-04-03 13:01:21 +00:00
|
|
|
if _, err := c.client.Projects.DeleteProjectHook(repopath, hook.ID); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.Wrapf(err, "error deleting existing repository webhook")
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-29 15:35:07 +00:00
|
|
|
func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error {
|
|
|
|
_, _, err := c.client.Commits.SetCommitStatus(repopath, commitSHA, &gitlab.SetCommitStatusOptions{
|
|
|
|
State: fromCommitStatus(status),
|
|
|
|
TargetURL: gitlab.String(targetURL),
|
|
|
|
Description: gitlab.String(description),
|
|
|
|
Context: gitlab.String(context),
|
|
|
|
})
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
|
|
|
|
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
|
2022-03-04 05:32:45 +00:00
|
|
|
baseOpts := gitlab.ListProjectsOptions{
|
|
|
|
MinAccessLevel: gitlab.AccessLevel(gitlab.MaintainerPermissions),
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 100,
|
|
|
|
},
|
2019-04-29 15:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 05:32:45 +00:00
|
|
|
return c.listUserRepos(baseOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) listUserRepos(fromOptions gitlab.ListProjectsOptions) ([]*gitsource.RepoInfo, error) {
|
2019-04-29 15:36:29 +00:00
|
|
|
repos := []*gitsource.RepoInfo{}
|
2022-03-04 05:32:45 +00:00
|
|
|
// get only repos with permission greater or equal to maintainer
|
|
|
|
opts := &fromOptions
|
|
|
|
for i := 1; i <= PAGE_MAX; i++ {
|
|
|
|
remoteRepos, pageResp, err := c.client.Projects.ListProjects(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
|
2022-03-04 05:32:45 +00:00
|
|
|
for _, rr := range remoteRepos {
|
|
|
|
repos = append(repos, fromGitlabRepo(rr))
|
|
|
|
}
|
|
|
|
opts.ListOptions.Page = pageResp.NextPage
|
|
|
|
if opts.ListOptions.Page == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(remoteRepos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if pageResp.TotalPages != 0 {
|
|
|
|
if pageResp.TotalPages >= pageResp.CurrentPage {
|
2022-03-04 10:12:00 +00:00
|
|
|
break
|
2022-03-04 05:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 05:41:28 +00:00
|
|
|
sort.Slice(repos, func(i, j int) bool {
|
|
|
|
a := repos[i]
|
|
|
|
b := repos[j]
|
|
|
|
return a.Path < b.Path
|
|
|
|
})
|
|
|
|
|
2019-04-29 15:36:29 +00:00
|
|
|
return repos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromGitlabRepo(rr *gitlab.Project) *gitsource.RepoInfo {
|
|
|
|
return &gitsource.RepoInfo{
|
|
|
|
ID: strconv.Itoa(rr.ID),
|
|
|
|
Path: rr.PathWithNamespace,
|
2019-06-11 09:07:12 +00:00
|
|
|
HTMLURL: rr.WebURL,
|
2019-04-29 15:36:29 +00:00
|
|
|
SSHCloneURL: rr.SSHURLToRepo,
|
|
|
|
HTTPCloneURL: rr.HTTPURLToRepo,
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 09:07:39 +00:00
|
|
|
|
|
|
|
// NOTE(sgotti) gitlab doesn't provide a get ref api
|
|
|
|
func (c *Client) GetRef(repopath, ref string) (*gitsource.Ref, error) {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(ref, "refs/heads/"):
|
|
|
|
|
|
|
|
branch := strings.TrimPrefix(ref, "refs/heads/")
|
|
|
|
remoteBranch, _, err := c.client.Branches.GetBranch(repopath, branch)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gitsource.Ref{
|
|
|
|
Ref: ref,
|
|
|
|
CommitSHA: remoteBranch.Commit.ID,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case strings.HasPrefix(ref, "refs/tags/"):
|
|
|
|
tag := strings.TrimPrefix(ref, "refs/heads/")
|
|
|
|
remoteTag, _, err := c.client.Tags.GetTag(repopath, tag)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gitsource.Ref{
|
|
|
|
Ref: ref,
|
|
|
|
CommitSHA: remoteTag.Commit.ID,
|
|
|
|
}, nil
|
|
|
|
default:
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Errorf("unsupported ref: %s", ref)
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RefType(ref string) (gitsource.RefType, string, error) {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(ref, branchRefPrefix):
|
|
|
|
return gitsource.RefTypeBranch, strings.TrimPrefix(ref, branchRefPrefix), nil
|
|
|
|
|
|
|
|
case strings.HasPrefix(ref, tagRefPrefix):
|
|
|
|
return gitsource.RefTypeTag, strings.TrimPrefix(ref, tagRefPrefix), nil
|
|
|
|
|
|
|
|
case pullRequestRefRegex.MatchString(ref):
|
|
|
|
m := pullRequestRefRegex.FindStringSubmatch(ref)
|
2019-08-02 08:03:28 +00:00
|
|
|
return gitsource.RefTypePullRequest, m[1], nil
|
2019-06-11 09:07:39 +00:00
|
|
|
|
|
|
|
default:
|
2022-02-22 14:01:29 +00:00
|
|
|
return -1, "", errors.Errorf("unsupported ref: %s", ref)
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetCommit(repopath, commitSHA string) (*gitsource.Commit, error) {
|
|
|
|
commit, _, err := c.client.Commits.GetCommit(repopath, commitSHA, nil)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gitsource.Commit{
|
|
|
|
SHA: commit.ID,
|
|
|
|
Message: commit.Message,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) BranchRef(branch string) string {
|
|
|
|
return branchRefPrefix + branch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) TagRef(tag string) string {
|
|
|
|
return tagRefPrefix + tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PullRequestRef(prID string) string {
|
|
|
|
return fmt.Sprintf(pullRequestRefFmt, prID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) CommitLink(repoInfo *gitsource.RepoInfo, commitSHA string) string {
|
|
|
|
return fmt.Sprintf("%s/commit/%s", repoInfo.HTMLURL, commitSHA)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) BranchLink(repoInfo *gitsource.RepoInfo, branch string) string {
|
|
|
|
return fmt.Sprintf("%s/tree/%s", repoInfo.HTMLURL, branch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) TagLink(repoInfo *gitsource.RepoInfo, tag string) string {
|
|
|
|
return fmt.Sprintf("%s/tree/%s", repoInfo.HTMLURL, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PullRequestLink(repoInfo *gitsource.RepoInfo, prID string) string {
|
|
|
|
return fmt.Sprintf("%s/merge_requests/%s", repoInfo.HTMLURL, prID)
|
|
|
|
}
|