2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +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-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +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.
|
|
|
|
|
2015-10-22 23:36:43 +00:00
|
|
|
package gogs
|
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2016-03-22 10:34:33 +00:00
|
|
|
"crypto/tls"
|
2015-10-22 23:36:43 +00:00
|
|
|
"fmt"
|
2015-12-24 15:09:18 +00:00
|
|
|
"net"
|
2015-10-22 23:36:43 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-12-22 10:54:53 +00:00
|
|
|
"strings"
|
2015-10-22 23:36:43 +00:00
|
|
|
|
2019-06-03 06:49:11 +00:00
|
|
|
"github.com/gogits/go-gogs-client"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
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"
|
2022-02-26 01:54:15 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/common"
|
2015-10-22 23:36:43 +00:00
|
|
|
)
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
|
|
|
URL string // Gogs server url.
|
|
|
|
Username string // Optional machine account username.
|
|
|
|
Password string // Optional machine account password.
|
|
|
|
PrivateMode bool // Gogs is running in private mode.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
2015-10-22 23:36:43 +00:00
|
|
|
URL string
|
2016-05-01 23:30:00 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
2015-10-22 23:36:43 +00:00
|
|
|
PrivateMode bool
|
|
|
|
SkipVerify bool
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// New returns a Remote implementation that integrates with Gogs, an open
|
|
|
|
// source Git service written in Go. See https://gogs.io/
|
2016-05-01 23:30:00 +00:00
|
|
|
func New(opts Opts) (remote.Remote, error) {
|
2021-09-24 14:29:26 +00:00
|
|
|
u, err := url.Parse(opts.URL)
|
2016-05-01 23:30:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
2021-09-24 14:29:26 +00:00
|
|
|
host, _, err := net.SplitHostPort(u.Host)
|
2016-05-01 23:30:00 +00:00
|
|
|
if err == nil {
|
2021-09-24 14:29:26 +00:00
|
|
|
u.Host = host
|
2016-05-01 23:30:00 +00:00
|
|
|
}
|
|
|
|
return &client{
|
|
|
|
URL: opts.URL,
|
|
|
|
Username: opts.Username,
|
|
|
|
Password: opts.Password,
|
|
|
|
PrivateMode: opts.PrivateMode,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
|
|
|
}, nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 18:14:01 +00:00
|
|
|
// Name returns the string name of this driver
|
|
|
|
func (c *client) Name() string {
|
|
|
|
return "gogs"
|
|
|
|
}
|
|
|
|
|
2018-02-15 08:39:59 +00:00
|
|
|
// Login authenticates an account with Gogs using basic authentication. The
|
2016-05-01 23:30:00 +00:00
|
|
|
// Gogs account details are returned when the user is successfully authenticated.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Login(ctx context.Context, res http.ResponseWriter, req *http.Request) (*model.User, error) {
|
2015-10-22 23:36:43 +00:00
|
|
|
var (
|
|
|
|
username = req.FormValue("username")
|
|
|
|
password = req.FormValue("password")
|
|
|
|
)
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// if the username or password is empty we re-direct to the login screen.
|
2015-10-22 23:36:43 +00:00
|
|
|
if len(username) == 0 || len(password) == 0 {
|
|
|
|
http.Redirect(res, req, "/login/form", http.StatusSeeOther)
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
client := c.newClient()
|
2015-10-22 23:36:43 +00:00
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// try to fetch woodpecker token if it exists
|
2015-10-22 23:36:43 +00:00
|
|
|
var accessToken string
|
|
|
|
tokens, err := client.ListAccessTokens(username, password)
|
2016-05-01 23:30:00 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, token := range tokens {
|
2021-10-02 08:59:34 +00:00
|
|
|
if token.Name == "woodpecker" {
|
2016-05-01 23:30:00 +00:00
|
|
|
accessToken = token.Sha1
|
|
|
|
break
|
|
|
|
}
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// if woodpecker token not found, create it
|
2015-10-22 23:36:43 +00:00
|
|
|
if accessToken == "" {
|
2016-05-01 23:30:00 +00:00
|
|
|
token, terr := client.CreateAccessToken(
|
|
|
|
username,
|
|
|
|
password,
|
2021-10-02 08:59:34 +00:00
|
|
|
gogs.CreateAccessTokenOption{Name: "woodpecker"},
|
2016-05-01 23:30:00 +00:00
|
|
|
)
|
|
|
|
if terr != nil {
|
|
|
|
return nil, terr
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
accessToken = token.Sha1
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
client = c.newClientToken(accessToken)
|
|
|
|
account, err := client.GetUserInfo(username)
|
2015-10-22 23:36:43 +00:00
|
|
|
if err != nil {
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, err
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return &model.User{
|
|
|
|
Token: accessToken,
|
|
|
|
Login: account.UserName,
|
|
|
|
Email: account.Email,
|
|
|
|
Avatar: expandAvatar(c.URL, account.AvatarUrl),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth is not supported by the Gogs driver.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Auth(ctx context.Context, token, secret string) (string, error) {
|
2016-05-01 23:30:00 +00:00
|
|
|
return "", fmt.Errorf("Not Implemented")
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Teams is not supported by the Gogs driver.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
|
2016-05-11 19:32:18 +00:00
|
|
|
client := c.newClientToken(u.Token)
|
|
|
|
orgs, err := client.ListMyOrgs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var teams []*model.Team
|
|
|
|
for _, org := range orgs {
|
|
|
|
teams = append(teams, toTeam(org, c.URL))
|
|
|
|
}
|
|
|
|
return teams, nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Repo returns the named Gogs repository.
|
2022-09-05 15:08:51 +00:00
|
|
|
func (c *client) Repo(ctx context.Context, u *model.User, _ model.RemoteID, owner, name string) (*model.Repo, error) {
|
2016-05-01 23:30:00 +00:00
|
|
|
client := c.newClientToken(u.Token)
|
|
|
|
repo, err := client.GetRepo(owner, name)
|
2015-10-22 23:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-26 22:59:57 +00:00
|
|
|
return toRepo(repo, c.PrivateMode), nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Repos returns a list of all repositories for the Gogs account, including
|
|
|
|
// organization repositories.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
|
2022-09-05 15:08:51 +00:00
|
|
|
var repos []*model.Repo
|
2015-10-22 23:36:43 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
client := c.newClientToken(u.Token)
|
|
|
|
all, err := client.ListMyRepos()
|
2015-10-22 23:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
for _, repo := range all {
|
2017-08-26 22:59:57 +00:00
|
|
|
repos = append(repos, toRepo(repo, c.PrivateMode))
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Perm returns the user permissions for the named Gogs repository.
|
2022-01-03 14:33:36 +00:00
|
|
|
func (c *client) Perm(ctx context.Context, u *model.User, r *model.Repo) (*model.Perm, error) {
|
2016-05-01 23:30:00 +00:00
|
|
|
client := c.newClientToken(u.Token)
|
2022-01-03 14:33:36 +00:00
|
|
|
repo, err := client.GetRepo(r.Owner, r.Name)
|
2015-10-22 23:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
return toPerm(repo.Permissions), nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// File fetches the file from the Gogs repository and returns its contents.
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) File(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, f string) ([]byte, error) {
|
2016-05-01 23:30:00 +00:00
|
|
|
client := c.newClientToken(u.Token)
|
2017-03-18 15:20:09 +00:00
|
|
|
ref := b.Commit
|
|
|
|
|
|
|
|
// TODO gogs does not yet return a sha with the pull request
|
|
|
|
// so unfortunately we need to use the pull request branch.
|
|
|
|
if b.Event == model.EventPull {
|
|
|
|
ref = b.Branch
|
|
|
|
}
|
|
|
|
if ref == "" {
|
2016-12-22 08:54:54 +00:00
|
|
|
// Remove refs/tags or refs/heads, Gogs needs a short ref
|
2017-03-18 15:20:09 +00:00
|
|
|
ref = strings.TrimPrefix(
|
2016-12-22 13:24:05 +00:00
|
|
|
strings.TrimPrefix(
|
|
|
|
b.Ref,
|
|
|
|
"refs/heads/",
|
|
|
|
),
|
|
|
|
"refs/tags/",
|
|
|
|
)
|
2016-05-06 20:14:19 +00:00
|
|
|
}
|
2017-03-18 15:20:09 +00:00
|
|
|
cfg, err := client.GetFile(r.Owner, r.Name, ref, f)
|
2016-03-22 10:34:33 +00:00
|
|
|
return cfg, err
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, f string) ([]*remote.FileMeta, error) {
|
2019-06-03 07:16:15 +00:00
|
|
|
return nil, fmt.Errorf("Not implemented")
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Status is not supported by the Gogs driver.
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, proc *model.Proc) error {
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Netrc returns a netrc file capable of authenticating Gogs requests and
|
|
|
|
// cloning Gogs repositories. The netrc will use the global machine account
|
|
|
|
// when configured.
|
|
|
|
func (c *client) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
2022-02-26 01:54:15 +00:00
|
|
|
host, err := common.ExtractHostFromCloneURL(r.Clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
if c.Password != "" {
|
|
|
|
return &model.Netrc{
|
|
|
|
Login: c.Username,
|
|
|
|
Password: c.Password,
|
2022-02-26 01:54:15 +00:00
|
|
|
Machine: host,
|
2016-05-01 23:30:00 +00:00
|
|
|
}, nil
|
2015-12-24 15:09:18 +00:00
|
|
|
}
|
2015-10-22 23:36:43 +00:00
|
|
|
return &model.Netrc{
|
|
|
|
Login: u.Token,
|
|
|
|
Password: "x-oauth-basic",
|
2022-02-26 01:54:15 +00:00
|
|
|
Machine: host,
|
2015-10-22 23:36:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Activate activates the repository by registering post-commit hooks with
|
|
|
|
// the Gogs repository.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
2015-10-22 23:36:43 +00:00
|
|
|
config := map[string]string{
|
|
|
|
"url": link,
|
|
|
|
"secret": r.Hash,
|
|
|
|
"content_type": "json",
|
|
|
|
}
|
|
|
|
hook := gogs.CreateHookOption{
|
|
|
|
Type: "gogs",
|
|
|
|
Config: config,
|
2017-01-03 19:31:45 +00:00
|
|
|
Events: []string{"push", "create", "pull_request"},
|
2015-10-22 23:36:43 +00:00
|
|
|
Active: true,
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
client := c.newClientToken(u.Token)
|
2015-10-22 23:36:43 +00:00
|
|
|
_, err := client.CreateRepoHook(r.Owner, r.Name, hook)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Deactivate is not supported by the Gogs driver.
|
2021-09-28 10:56:59 +00:00
|
|
|
func (c *client) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:47:55 +00:00
|
|
|
// Branches returns the names of all branches for the named repository.
|
|
|
|
func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
|
2022-02-26 16:36:00 +00:00
|
|
|
token := ""
|
|
|
|
if u != nil {
|
|
|
|
token = u.Token
|
|
|
|
}
|
|
|
|
client := c.newClientToken(token)
|
|
|
|
gogsBranches, err := client.ListRepoBranches(r.Owner, r.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
branches := make([]string, 0)
|
|
|
|
for _, branch := range gogsBranches {
|
|
|
|
branches = append(branches, branch.Name)
|
|
|
|
}
|
|
|
|
return branches, nil
|
2021-10-27 00:47:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 22:36:32 +00:00
|
|
|
// BranchHead returns sha of commit on top of the specified branch
|
|
|
|
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
|
|
|
|
token := ""
|
|
|
|
if u != nil {
|
|
|
|
token = u.Token
|
|
|
|
}
|
|
|
|
b, err := c.newClientToken(token).GetRepoBranch(r.Owner, r.Name, branch)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.Commit.ID, nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// Hook parses the incoming Gogs hook and returns the Repository and Pipeline
|
2016-05-01 23:30:00 +00:00
|
|
|
// details. If the hook is unsupported nil values are returned.
|
2022-10-18 01:24:12 +00:00
|
|
|
func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Pipeline, error) {
|
2022-09-07 17:16:40 +00:00
|
|
|
return parseHook(r, c.PrivateMode)
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
2015-10-27 23:48:05 +00:00
|
|
|
|
2022-07-25 01:09:35 +00:00
|
|
|
// OrgMembership returns if user is member of organization and if user
|
|
|
|
// is admin/owner in this organization.
|
|
|
|
func (c *client) OrgMembership(ctx context.Context, u *model.User, owner string) (*model.OrgPerm, error) {
|
|
|
|
client := c.newClientToken(u.Token)
|
|
|
|
|
|
|
|
orgs, err := client.ListMyOrgs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, org := range orgs {
|
|
|
|
if org.UserName == owner {
|
|
|
|
// TODO: API does not support checking if user is admin/owner of org
|
|
|
|
return &model.OrgPerm{Member: true}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &model.OrgPerm{}, nil
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// helper function to return the Gogs client
|
|
|
|
func (c *client) newClient() *gogs.Client {
|
|
|
|
return c.newClientToken("")
|
|
|
|
}
|
2016-03-22 10:34:33 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// helper function to return the Gogs client
|
|
|
|
func (c *client) newClientToken(token string) *gogs.Client {
|
|
|
|
client := gogs.NewClient(c.URL, token)
|
|
|
|
if c.SkipVerify {
|
|
|
|
httpClient := &http.Client{}
|
|
|
|
httpClient.Transport = &http.Transport{
|
2016-03-22 10:34:33 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
client.SetHTTPClient(httpClient)
|
2016-03-22 10:34:33 +00:00
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
return client
|
2015-10-27 23:48:05 +00:00
|
|
|
}
|