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 gitea
|
|
|
|
|
|
|
|
import (
|
2019-05-09 12:14:13 +00:00
|
|
|
"context"
|
2019-02-21 15:06:34 +00:00
|
|
|
"crypto/tls"
|
2019-05-13 12:21:41 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2019-04-29 15:35:07 +00:00
|
|
|
"fmt"
|
2019-04-29 09:24:03 +00:00
|
|
|
"net"
|
2019-02-21 15:06:34 +00:00
|
|
|
"net/http"
|
2019-04-29 15:36:29 +00:00
|
|
|
"path"
|
2019-06-11 09:07:39 +00:00
|
|
|
"regexp"
|
2019-02-21 15:06:34 +00:00
|
|
|
"strconv"
|
2019-04-03 13:01:21 +00:00
|
|
|
"strings"
|
2019-04-29 09:24:03 +00:00
|
|
|
"time"
|
2019-02-21 15:06:34 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
gitsource "agola.io/agola/internal/gitsources"
|
2019-02-21 15:06:34 +00:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2019-05-23 09:23:14 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 15:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TODO(sgotti) The gitea client doesn't provide an easy way to detect http response codes...
|
|
|
|
// we should probably use our own client implementation
|
|
|
|
|
|
|
|
ClientNotFound = "404 Not Found"
|
|
|
|
)
|
|
|
|
|
2019-05-09 12:14:13 +00:00
|
|
|
var (
|
|
|
|
// gitea corrently doesn't have any auth scope
|
|
|
|
GiteaOauth2Scopes = []string{""}
|
2019-06-11 09:07:39 +00:00
|
|
|
|
|
|
|
branchRefPrefix = "refs/heads/"
|
|
|
|
tagRefPrefix = "refs/tags/"
|
|
|
|
pullRequestRefRegex = regexp.MustCompile("refs/pull/(.*)/head")
|
|
|
|
pullRequestRefFmt = "refs/pull/%s/head"
|
2019-05-09 12:14:13 +00:00
|
|
|
)
|
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
type Opts struct {
|
2019-05-15 21:46:21 +00:00
|
|
|
APIURL string
|
2019-05-09 12:14:13 +00:00
|
|
|
Token string
|
|
|
|
SkipVerify bool
|
|
|
|
Oauth2ClientID string
|
|
|
|
Oauth2Secret string
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2020-02-11 14:54:53 +00:00
|
|
|
client *gitea.Client
|
|
|
|
oauth2HTTPClient *http.Client
|
|
|
|
APIURL string
|
|
|
|
oauth2ClientID string
|
|
|
|
oauth2Secret string
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fromCommitStatus converts a gitsource commit status to a gitea commit status
|
2019-10-24 08:58:57 +00:00
|
|
|
func fromCommitStatus(status gitsource.CommitStatus) gitea.StatusState {
|
2019-02-21 15:06:34 +00:00
|
|
|
switch status {
|
|
|
|
case gitsource.CommitStatusPending:
|
2019-10-24 08:58:57 +00:00
|
|
|
return gitea.StatusPending
|
2019-02-21 15:06:34 +00:00
|
|
|
case gitsource.CommitStatusSuccess:
|
2019-10-24 08:58:57 +00:00
|
|
|
return gitea.StatusSuccess
|
2019-05-15 09:26:50 +00:00
|
|
|
case gitsource.CommitStatusError:
|
2019-10-24 08:58:57 +00:00
|
|
|
return gitea.StatusError
|
2019-02-21 15:06:34 +00:00
|
|
|
case gitsource.CommitStatusFailed:
|
2019-10-24 08:58:57 +00:00
|
|
|
return gitea.StatusFailure
|
2019-02-21 15:06:34 +00:00
|
|
|
default:
|
2019-10-22 08:15:20 +00:00
|
|
|
panic(errors.Errorf("unknown commit status %q", status))
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func parseRepoPath(repopath string) (string, string, error) {
|
|
|
|
parts := strings.Split(repopath, "/")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", errors.Errorf("wrong gitea repo path: %q", repopath)
|
|
|
|
}
|
|
|
|
return parts[0], parts[1], nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
func New(opts Opts) (*Client, error) {
|
2019-04-29 09:24:03 +00:00
|
|
|
// 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},
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
2019-04-29 09:24:03 +00:00
|
|
|
httpClient := &http.Client{Transport: transport}
|
|
|
|
|
2019-05-15 21:46:21 +00:00
|
|
|
client := gitea.NewClient(opts.APIURL, opts.Token)
|
2019-04-29 09:24:03 +00:00
|
|
|
client.SetHTTPClient(httpClient)
|
|
|
|
|
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-05-09 12:14:13 +00:00
|
|
|
func (c *Client) oauth2Config(callbackURL string) *oauth2.Config {
|
|
|
|
return &oauth2.Config{
|
|
|
|
ClientID: c.oauth2ClientID,
|
|
|
|
ClientSecret: c.oauth2Secret,
|
|
|
|
Scopes: GiteaOauth2Scopes,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
2019-05-15 21:46:21 +00:00
|
|
|
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", c.APIURL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.APIURL),
|
2019-05-09 12:14:13 +00:00
|
|
|
},
|
|
|
|
RedirectURL: callbackURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) {
|
|
|
|
var config = c.oauth2Config(callbackURL)
|
|
|
|
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-05-09 12:14:13 +00:00
|
|
|
var config = c.oauth2Config(callbackURL)
|
2020-02-11 14:54:53 +00:00
|
|
|
token, err := config.Exchange(ctx, code)
|
2019-05-09 12:14:13 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return nil, errors.Errorf("cannot get oauth2 token: %w", err)
|
2019-05-09 12:14:13 +00:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-09 12:14:13 +00:00
|
|
|
var config = c.oauth2Config("")
|
|
|
|
token := &oauth2.Token{RefreshToken: refreshToken}
|
2020-02-11 14:54:53 +00:00
|
|
|
ts := config.TokenSource(ctx, token)
|
2019-05-09 12:14:13 +00:00
|
|
|
return ts.Token()
|
|
|
|
}
|
|
|
|
|
2019-04-30 10:13:12 +00:00
|
|
|
func (c *Client) LoginPassword(username, password, tokenName string) (string, error) {
|
2019-02-21 15:06:34 +00:00
|
|
|
// try to get agola access token if it already exists
|
2019-05-13 12:21:41 +00:00
|
|
|
// use custom http call since gitea api client doesn't provide an easy way to
|
|
|
|
// guess if the username/password login failed
|
2019-02-21 15:06:34 +00:00
|
|
|
var accessToken string
|
2019-05-13 12:21:41 +00:00
|
|
|
|
|
|
|
tokens := make([]*gitea.AccessToken, 0, 10)
|
2019-05-15 21:46:21 +00:00
|
|
|
req, err := http.NewRequest("GET", c.APIURL+"/api/v1"+fmt.Sprintf("/users/%s/tokens", username), nil)
|
2019-05-13 12:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
|
|
|
|
|
2020-02-11 14:54:53 +00:00
|
|
|
resp, err := c.oauth2HTTPClient.Do(req)
|
2019-05-13 12:21:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
|
|
return "", gitsource.ErrUnauthorized
|
|
|
|
}
|
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return "", errors.Errorf("gitea api status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err := dec.Decode(&tokens); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, token := range tokens {
|
|
|
|
if token.Name == tokenName {
|
2019-06-11 08:29:06 +00:00
|
|
|
accessToken = token.Token
|
2019-05-13 12:21:41 +00:00
|
|
|
break
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create access token
|
|
|
|
if accessToken == "" {
|
|
|
|
token, terr := c.client.CreateAccessToken(
|
|
|
|
username,
|
|
|
|
password,
|
2019-10-24 08:58:57 +00:00
|
|
|
gitea.CreateAccessTokenOption{Name: tokenName},
|
2019-02-21 15:06:34 +00:00
|
|
|
)
|
|
|
|
if terr != nil {
|
|
|
|
return "", terr
|
|
|
|
}
|
2019-06-11 08:29:06 +00:00
|
|
|
accessToken = token.Token
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return accessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) {
|
|
|
|
user, err := c.client.GetMyUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &gitsource.UserInfo{
|
|
|
|
ID: strconv.FormatInt(user.ID, 10),
|
|
|
|
LoginName: user.UserName,
|
|
|
|
Email: user.Email,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
rr, err := c.client.GetRepo(owner, reponame)
|
2019-04-03 09:07:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
return fromGiteaRepo(rr), nil
|
2019-04-03 09:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data, err := c.client.GetFile(owner, reponame, commit, file)
|
2019-02-21 15:06:34 +00:00
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:01:21 +00:00
|
|
|
func (c *Client) CreateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-24 08:58:57 +00:00
|
|
|
if _, err = c.client.CreateDeployKey(owner, reponame, gitea.CreateKeyOption{
|
2019-02-21 15:06:34 +00:00
|
|
|
Title: title,
|
|
|
|
Key: pubKey,
|
|
|
|
ReadOnly: readonly,
|
2019-05-23 09:23:14 +00:00
|
|
|
}); err != nil {
|
|
|
|
return errors.Errorf("error creating deploy key: %w", err)
|
|
|
|
}
|
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 {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-21 15:06:34 +00:00
|
|
|
// NOTE(sgotti) gitea has a bug where if we delete and remove the same key with
|
|
|
|
// the same value it is correctly readded and the admin must force a
|
2019-02-28 16:19:53 +00:00
|
|
|
// authorized_keys regeneration on the server. To avoid this we update it only
|
2019-02-21 15:06:34 +00:00
|
|
|
// when the public key value has changed
|
2019-04-03 13:01:21 +00:00
|
|
|
keys, err := c.client.ListDeployKeys(owner, reponame)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error retrieving existing deploy keys: %w", err)
|
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.DeleteDeployKey(owner, reponame, key.ID); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error removing existing deploy key: %w", err)
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:58:57 +00:00
|
|
|
if _, err := c.client.CreateDeployKey(owner, reponame, gitea.CreateKeyOption{
|
2019-02-21 15:06:34 +00:00
|
|
|
Title: title,
|
|
|
|
Key: pubKey,
|
|
|
|
ReadOnly: readonly,
|
|
|
|
}); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error creating deploy key: %w", err)
|
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 {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
keys, err := c.client.ListDeployKeys(owner, reponame)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error retrieving existing deploy keys: %w", err)
|
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.DeleteDeployKey(owner, reponame, key.ID); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error removing existing deploy key: %w", err)
|
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 {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:58:57 +00:00
|
|
|
opts := gitea.CreateHookOption{
|
2019-02-21 15:06:34 +00:00
|
|
|
Type: "gitea",
|
|
|
|
Config: map[string]string{
|
|
|
|
"url": url,
|
|
|
|
"content_type": "json",
|
|
|
|
"secret": secret,
|
|
|
|
},
|
|
|
|
Events: []string{"push", "pull_request"},
|
|
|
|
Active: true,
|
|
|
|
}
|
2019-04-03 13:01:21 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
if _, err = c.client.CreateRepoHook(owner, reponame, opts); err != nil {
|
|
|
|
return errors.Errorf("error creating repository webhook: %w", err)
|
|
|
|
}
|
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 {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hooks, err := c.client.ListRepoHooks(owner, reponame)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error retrieving repository webhooks: %w", err)
|
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.Config["url"] == u {
|
2019-04-03 13:01:21 +00:00
|
|
|
if err := c.client.DeleteRepoHook(owner, reponame, hook.ID); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("error deleting existing repository webhook: %w", err)
|
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 {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-24 08:58:57 +00:00
|
|
|
_, err = c.client.CreateStatus(owner, reponame, commitSHA, gitea.CreateStatusOption{
|
2019-04-29 15:35:07 +00:00
|
|
|
State: fromCommitStatus(status),
|
|
|
|
TargetURL: targetURL,
|
|
|
|
Description: description,
|
|
|
|
Context: context,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
|
|
|
|
func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {
|
|
|
|
remoteRepos, err := c.client.ListMyRepos()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repos := []*gitsource.RepoInfo{}
|
|
|
|
|
|
|
|
for _, rr := range remoteRepos {
|
2019-05-23 14:58:20 +00:00
|
|
|
// keep only repos with admin permissions
|
|
|
|
if !rr.Permissions.Admin {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-29 15:36:29 +00:00
|
|
|
repos = append(repos, fromGiteaRepo(rr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return repos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromGiteaRepo(rr *gitea.Repository) *gitsource.RepoInfo {
|
|
|
|
return &gitsource.RepoInfo{
|
|
|
|
ID: strconv.FormatInt(rr.ID, 10),
|
|
|
|
Path: path.Join(rr.Owner.UserName, rr.Name),
|
2019-06-11 09:07:12 +00:00
|
|
|
HTMLURL: rr.HTMLURL,
|
2019-04-29 15:36:29 +00:00
|
|
|
SSHCloneURL: rr.SSHURL,
|
|
|
|
HTTPCloneURL: rr.CloneURL,
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 09:07:39 +00:00
|
|
|
|
|
|
|
func (c *Client) GetRef(repopath, ref string) (*gitsource.Ref, error) {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-22 08:15:20 +00:00
|
|
|
remoteRefs, err := c.client.GetRepoRefs(owner, reponame, ref)
|
2019-06-11 09:07:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-22 08:15:20 +00:00
|
|
|
if len(remoteRefs) == 0 {
|
|
|
|
return nil, errors.Errorf("no ref %q for repository %q", ref, repopath)
|
|
|
|
}
|
|
|
|
if len(remoteRefs) != 1 {
|
|
|
|
return nil, errors.Errorf("no exact match found for ref %q for repository %q", ref, repopath)
|
|
|
|
}
|
2019-06-11 09:07:39 +00:00
|
|
|
|
2019-10-22 08:15:20 +00:00
|
|
|
return fromGiteaRef(remoteRefs[0])
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fromGiteaRef(remoteRef *gitea.Reference) (*gitsource.Ref, error) {
|
|
|
|
t := remoteRef.Object.Type
|
|
|
|
switch t {
|
|
|
|
case "commit":
|
|
|
|
default:
|
2019-10-22 08:15:20 +00:00
|
|
|
return nil, errors.Errorf("unsupported object type: %s", t)
|
2019-06-11 09:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &gitsource.Ref{
|
|
|
|
Ref: remoteRef.Ref,
|
|
|
|
CommitSHA: remoteRef.Object.SHA,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2019-10-22 08:15:20 +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) {
|
|
|
|
owner, reponame, err := parseRepoPath(repopath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := c.client.GetSingleCommit(owner, reponame, commitSHA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gitsource.Commit{
|
|
|
|
SHA: commit.SHA,
|
|
|
|
Message: commit.RepoCommit.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/src/branch/%s", repoInfo.HTMLURL, branch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) TagLink(repoInfo *gitsource.RepoInfo, tag string) string {
|
|
|
|
return fmt.Sprintf("%s/src/tag/%s", repoInfo.HTMLURL, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PullRequestLink(repoInfo *gitsource.RepoInfo, prID string) string {
|
|
|
|
return fmt.Sprintf("%s/pulls/%s", repoInfo.HTMLURL, prID)
|
|
|
|
}
|