2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2021-03-29 11:44:47 +00:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
2018-03-10 19:09:14 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-10 19:09:14 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-10 19:09:14 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
2021-03-29 11:44:47 +00:00
|
|
|
//
|
2021-03-29 12:21:29 +00:00
|
|
|
// This file has been modified by Informatyka Boguslawski sp. z o.o. sp.k.
|
2018-02-19 22:24:10 +00:00
|
|
|
|
2017-05-01 10:33:06 +00:00
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
2021-09-27 06:11:11 +00:00
|
|
|
"context"
|
2017-05-01 10:33:06 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-07-05 17:33:02 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2017-05-01 10:33:06 +00:00
|
|
|
|
2017-05-02 01:09:36 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-10-12 07:25:13 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 16:25:51 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-09-27 06:11:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authorizeTokenURL = "%s/login/oauth/authorize"
|
|
|
|
accessTokenURL = "%s/login/oauth/access_token"
|
|
|
|
perPage = 50
|
2017-05-01 10:33:06 +00:00
|
|
|
)
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
type Gitea struct {
|
|
|
|
URL string
|
|
|
|
Context string
|
|
|
|
Machine string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
PrivateMode bool
|
|
|
|
SkipVerify bool
|
|
|
|
}
|
|
|
|
|
2017-05-01 10:33:06 +00:00
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
|
|
|
URL string // Gitea server url.
|
2018-03-10 19:09:14 +00:00
|
|
|
Context string // Context to display in status check
|
2021-06-17 07:02:44 +00:00
|
|
|
Client string // OAuth2 Client ID
|
|
|
|
Secret string // OAuth2 Client Secret
|
2017-05-01 10:33:06 +00:00
|
|
|
Username string // Optional machine account username.
|
|
|
|
Password string // Optional machine account password.
|
|
|
|
PrivateMode bool // Gitea is running in private mode.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
// New returns a Remote implementation that integrates with Gitea,
|
|
|
|
// an open source Git service written in Go. See https://gitea.io/
|
2017-05-01 10:33:06 +00:00
|
|
|
func New(opts Opts) (remote.Remote, error) {
|
2021-09-24 14:29:26 +00:00
|
|
|
u, err := url.Parse(opts.URL)
|
2017-05-01 10:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
host, _, err := net.SplitHostPort(u.Host)
|
2017-05-01 10:33:06 +00:00
|
|
|
if err == nil {
|
2021-09-24 14:29:26 +00:00
|
|
|
u.Host = host
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2021-09-27 06:11:11 +00:00
|
|
|
return &Gitea{
|
|
|
|
URL: opts.URL,
|
|
|
|
Context: opts.Context,
|
|
|
|
Machine: u.Host,
|
|
|
|
ClientID: opts.Client,
|
|
|
|
ClientSecret: opts.Secret,
|
|
|
|
Username: opts.Username,
|
|
|
|
Password: opts.Password,
|
|
|
|
PrivateMode: opts.PrivateMode,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
2017-05-01 10:33:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-02-15 08:39:59 +00:00
|
|
|
// Login authenticates an account with Gitea using basic authentication. The
|
2017-05-01 10:33:06 +00:00
|
|
|
// Gitea account details are returned when the user is successfully authenticated.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Login(ctx context.Context, w http.ResponseWriter, req *http.Request) (*model.User, error) {
|
2021-09-27 06:11:11 +00:00
|
|
|
config := &oauth2.Config{
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: fmt.Sprintf(authorizeTokenURL, c.URL),
|
|
|
|
TokenURL: fmt.Sprintf(accessTokenURL, c.URL),
|
|
|
|
},
|
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.Host),
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
// get the OAuth errors
|
|
|
|
if err := req.FormValue("error"); err != "" {
|
|
|
|
return nil, &remote.AuthError{
|
|
|
|
Err: err,
|
|
|
|
Description: req.FormValue("error_description"),
|
|
|
|
URI: req.FormValue("error_uri"),
|
|
|
|
}
|
2020-09-16 06:48:09 +00:00
|
|
|
}
|
2017-05-01 10:33:06 +00:00
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
// get the OAuth code
|
|
|
|
code := req.FormValue("code")
|
|
|
|
if len(code) == 0 {
|
2021-10-02 08:59:34 +00:00
|
|
|
http.Redirect(w, req, config.AuthCodeURL("woodpecker"), http.StatusSeeOther)
|
2021-09-27 06:11:11 +00:00
|
|
|
return nil, nil
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
token, err := config.Exchange(oauth2.NoContext, code)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
client, err := c.newClientToken(ctx, token.AccessToken)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-27 06:11:11 +00:00
|
|
|
account, _, err := client.GetMyUserInfo()
|
2017-05-01 10:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.User{
|
2021-09-27 06:11:11 +00:00
|
|
|
Token: token.AccessToken,
|
|
|
|
Secret: token.RefreshToken,
|
|
|
|
Expiry: token.Expiry.UTC().Unix(),
|
2017-05-01 10:33:06 +00:00
|
|
|
Login: account.UserName,
|
|
|
|
Email: account.Email,
|
|
|
|
Avatar: expandAvatar(c.URL, account.AvatarURL),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
// Auth uses the Gitea oauth2 access token and refresh token to authenticate
|
|
|
|
// a session and return the Gitea account login.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Auth(ctx context.Context, token, secret string) (string, error) {
|
|
|
|
client, err := c.newClientToken(ctx, token)
|
2021-09-27 06:11:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
user, _, err := client.GetMyUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return user.UserName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh refreshes the Gitea oauth2 access token. If the token is
|
|
|
|
// refreshed the user is updated and a true value is returned.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Refresh(ctx context.Context, user *model.User) (bool, error) {
|
2021-09-27 06:11:11 +00:00
|
|
|
config := &oauth2.Config{
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
ClientSecret: c.ClientSecret,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: fmt.Sprintf(authorizeTokenURL, c.URL),
|
|
|
|
TokenURL: fmt.Sprintf(accessTokenURL, c.URL),
|
|
|
|
},
|
|
|
|
}
|
2021-09-28 10:56:59 +00:00
|
|
|
source := config.TokenSource(ctx, &oauth2.Token{RefreshToken: user.Secret})
|
2021-09-27 06:11:11 +00:00
|
|
|
|
|
|
|
token, err := source.Token()
|
|
|
|
if err != nil || len(token.AccessToken) == 0 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Token = token.AccessToken
|
|
|
|
user.Secret = token.RefreshToken
|
|
|
|
user.Expiry = token.Expiry.UTC().Unix()
|
|
|
|
return true, nil
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 01:09:36 +00:00
|
|
|
// Teams is supported by the Gitea driver.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
|
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
teams := make([]*model.Team, 0, perPage)
|
2017-05-01 10:33:06 +00:00
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
page := 1
|
|
|
|
for {
|
|
|
|
orgs, _, err := client.ListMyOrgs(
|
|
|
|
gitea.ListOrgsOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: perPage,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, org := range orgs {
|
|
|
|
teams = append(teams, toTeam(org, c.URL))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orgs) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
page++
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2021-09-27 06:11:11 +00:00
|
|
|
|
2017-05-01 10:33:06 +00:00
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeamPerm is not supported by the Gitea driver.
|
2021-09-27 06:11:11 +00:00
|
|
|
func (c *Gitea) TeamPerm(u *model.User, org string) (*model.Perm, error) {
|
2017-05-01 10:33:06 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repo returns the named Gitea repository.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Repo(ctx context.Context, u *model.User, owner, name string) (*model.Repo, error) {
|
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, _, err := client.GetRepo(owner, name)
|
2017-05-01 10:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if c.PrivateMode {
|
|
|
|
repo.Private = true
|
|
|
|
}
|
2017-08-26 22:59:57 +00:00
|
|
|
return toRepo(repo, c.PrivateMode), nil
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Repos returns a list of all repositories for the Gitea account, including
|
|
|
|
// organization repositories.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
|
2021-09-27 06:11:11 +00:00
|
|
|
repos := make([]*model.Repo, 0, perPage)
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-29 11:44:47 +00:00
|
|
|
// Gitea SDK forces us to read repo list paginated.
|
2021-09-24 14:29:26 +00:00
|
|
|
var page = 1
|
2021-03-29 11:44:47 +00:00
|
|
|
for {
|
|
|
|
all, _, err := client.ListMyRepos(
|
|
|
|
gitea.ListReposOptions{
|
|
|
|
ListOptions: gitea.ListOptions{
|
|
|
|
Page: page,
|
2021-09-27 06:11:11 +00:00
|
|
|
PageSize: perPage,
|
2021-03-29 11:44:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-09-27 06:11:11 +00:00
|
|
|
return nil, err
|
2021-03-29 11:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range all {
|
|
|
|
repos = append(repos, toRepo(repo, c.PrivateMode))
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
if len(all) < perPage {
|
2021-03-29 11:44:47 +00:00
|
|
|
break
|
|
|
|
}
|
2021-09-27 06:11:11 +00:00
|
|
|
// Last page was not empty so more repos may be available - continue loop.
|
|
|
|
page++
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 11:44:47 +00:00
|
|
|
return repos, nil
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perm returns the user permissions for the named Gitea repository.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Perm(ctx context.Context, u *model.User, owner, name string) (*model.Perm, error) {
|
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, _, err := client.GetRepo(owner, name)
|
2017-05-01 10:33:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return toPerm(repo.Permissions), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// File fetches the file from the Gitea repository and returns its contents.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) File(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
|
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, _, err := client.GetFile(r.Owner, r.Name, b.Commit, f)
|
2017-05-01 10:33:06 +00:00
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, f string) ([]*remote.FileMeta, error) {
|
2021-07-05 17:33:02 +00:00
|
|
|
var configs []*remote.FileMeta
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2021-07-05 17:33:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// List files in repository. Path from root
|
|
|
|
tree, _, err := client.GetTrees(r.Owner, r.Name, b.Commit, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f = path.Clean(f) // We clean path and remove trailing slash
|
|
|
|
f += "/" + "*" // construct pattern for match i.e. file in subdir
|
|
|
|
for _, e := range tree.Entries {
|
|
|
|
// Filter path matching pattern and type file (blob)
|
|
|
|
if m, _ := filepath.Match(f, e.Path); m && e.Type == "blob" {
|
2021-09-28 10:56:59 +00:00
|
|
|
data, err := c.File(ctx, u, r, b, e.Path)
|
2021-07-05 17:33:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("multi-pipeline cannot get %s: %s", e.Path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configs = append(configs, &remote.FileMeta{
|
|
|
|
Name: e.Path,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
2019-06-03 07:16:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 01:09:36 +00:00
|
|
|
// Status is supported by the Gitea driver.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Build, link string, proc *model.Proc) error {
|
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-01 10:33:06 +00:00
|
|
|
|
|
|
|
status := getStatus(b.Status)
|
|
|
|
desc := getDesc(b.Status)
|
|
|
|
|
2020-09-16 06:48:09 +00:00
|
|
|
_, _, err = client.CreateStatus(
|
2017-05-01 10:33:06 +00:00
|
|
|
r.Owner,
|
|
|
|
r.Name,
|
|
|
|
b.Commit,
|
|
|
|
gitea.CreateStatusOption{
|
2017-05-24 02:02:11 +00:00
|
|
|
State: status,
|
|
|
|
TargetURL: link,
|
|
|
|
Description: desc,
|
2018-03-10 19:09:14 +00:00
|
|
|
Context: c.Context,
|
2017-05-01 10:33:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-05-01 12:29:57 +00:00
|
|
|
return err
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Netrc returns a netrc file capable of authenticating Gitea requests and
|
|
|
|
// cloning Gitea repositories. The netrc will use the global machine account
|
|
|
|
// when configured.
|
2021-09-27 06:11:11 +00:00
|
|
|
func (c *Gitea) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
2017-05-01 10:33:06 +00:00
|
|
|
if c.Password != "" {
|
|
|
|
return &model.Netrc{
|
|
|
|
Login: c.Username,
|
|
|
|
Password: c.Password,
|
|
|
|
Machine: c.Machine,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return &model.Netrc{
|
2017-10-12 12:33:25 +00:00
|
|
|
Login: u.Login,
|
|
|
|
Password: u.Token,
|
2017-05-01 10:33:06 +00:00
|
|
|
Machine: c.Machine,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activate activates the repository by registering post-commit hooks with
|
|
|
|
// the Gitea repository.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
2017-05-01 10:33:06 +00:00
|
|
|
config := map[string]string{
|
|
|
|
"url": link,
|
|
|
|
"secret": r.Hash,
|
|
|
|
"content_type": "json",
|
|
|
|
}
|
|
|
|
hook := gitea.CreateHookOption{
|
2021-09-27 06:11:11 +00:00
|
|
|
Type: gitea.HookTypeGitea,
|
2017-05-01 10:33:06 +00:00
|
|
|
Config: config,
|
|
|
|
Events: []string{"push", "create", "pull_request"},
|
|
|
|
Active: true,
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _, err = client.CreateRepoHook(r.Owner, r.Name, hook)
|
2017-05-01 10:33:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-24 02:02:11 +00:00
|
|
|
// Deactivate deactives the repository be removing repository push hooks from
|
|
|
|
// the Gitea repository.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
|
|
|
client, err := c.newClientToken(ctx, u.Token)
|
2020-09-16 06:48:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-24 02:02:11 +00:00
|
|
|
|
2020-09-16 06:48:09 +00:00
|
|
|
hooks, _, err := client.ListRepoHooks(r.Owner, r.Name, gitea.ListHooksOptions{})
|
2017-05-24 02:02:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hook := matchingHooks(hooks, link)
|
|
|
|
if hook != nil {
|
2020-09-16 06:48:09 +00:00
|
|
|
_, err := client.DeleteRepoHook(r.Owner, r.Name, hook.ID)
|
|
|
|
return err
|
2017-05-24 02:02:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 10:33:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hook parses the incoming Gitea hook and returns the Repository and Build
|
|
|
|
// details. If the hook is unsupported nil values are returned.
|
2021-09-27 06:11:11 +00:00
|
|
|
func (c *Gitea) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
2017-05-01 10:33:06 +00:00
|
|
|
return parseHook(r)
|
|
|
|
}
|
|
|
|
|
2021-06-05 08:55:48 +00:00
|
|
|
// helper function to return the Gitea client with Token
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *Gitea) newClientToken(ctx context.Context, token string) (*gitea.Client, error) {
|
2020-09-16 06:48:09 +00:00
|
|
|
httpClient := &http.Client{}
|
2017-05-01 10:33:06 +00:00
|
|
|
if c.SkipVerify {
|
|
|
|
httpClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 10:56:59 +00:00
|
|
|
return gitea.NewClient(c.URL, gitea.SetToken(token), gitea.SetHTTPClient(httpClient), gitea.SetContext(ctx))
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2017-05-24 02:02:11 +00:00
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
const (
|
|
|
|
DescPending = "the build is pending"
|
|
|
|
DescRunning = "the build is running"
|
|
|
|
DescSuccess = "the build was successful"
|
|
|
|
DescFailure = "the build failed"
|
|
|
|
DescCanceled = "the build canceled"
|
|
|
|
DescBlocked = "the build is pending approval"
|
|
|
|
DescDeclined = "the build was rejected"
|
|
|
|
)
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// getStatus is a helper function that converts a Woodpecker
|
2021-09-27 06:11:11 +00:00
|
|
|
// status to a Gitea status.
|
|
|
|
func getStatus(status string) gitea.StatusState {
|
|
|
|
switch status {
|
|
|
|
case model.StatusPending, model.StatusBlocked:
|
|
|
|
return gitea.StatusPending
|
|
|
|
case model.StatusRunning:
|
|
|
|
return gitea.StatusPending
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return gitea.StatusSuccess
|
|
|
|
case model.StatusFailure, model.StatusError:
|
|
|
|
return gitea.StatusFailure
|
|
|
|
case model.StatusKilled:
|
|
|
|
return gitea.StatusFailure
|
|
|
|
case model.StatusDeclined:
|
|
|
|
return gitea.StatusWarning
|
|
|
|
default:
|
|
|
|
return gitea.StatusFailure
|
2021-06-05 08:55:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:11:11 +00:00
|
|
|
// getDesc is a helper function that generates a description
|
|
|
|
// message for the build based on the status.
|
|
|
|
func getDesc(status string) string {
|
|
|
|
switch status {
|
|
|
|
case model.StatusPending:
|
|
|
|
return DescPending
|
|
|
|
case model.StatusRunning:
|
|
|
|
return DescRunning
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return DescSuccess
|
|
|
|
case model.StatusFailure, model.StatusError:
|
|
|
|
return DescFailure
|
|
|
|
case model.StatusKilled:
|
|
|
|
return DescCanceled
|
|
|
|
case model.StatusBlocked:
|
|
|
|
return DescBlocked
|
|
|
|
case model.StatusDeclined:
|
|
|
|
return DescDeclined
|
|
|
|
default:
|
|
|
|
return DescFailure
|
2017-05-24 02:02:11 +00:00
|
|
|
}
|
|
|
|
}
|