2015-10-22 23:36:43 +00:00
|
|
|
package gogs
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2016-04-29 19:39:56 +00:00
|
|
|
"github.com/drone/drone/remote"
|
2015-10-22 23:36:43 +00:00
|
|
|
"github.com/gogits/go-gogs-client"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
Machine string
|
|
|
|
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) {
|
|
|
|
url, err := url.Parse(opts.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
host, _, err := net.SplitHostPort(url.Host)
|
|
|
|
if err == nil {
|
|
|
|
url.Host = host
|
|
|
|
}
|
|
|
|
return &client{
|
|
|
|
URL: opts.URL,
|
|
|
|
Machine: url.Host,
|
|
|
|
Username: opts.Username,
|
|
|
|
Password: opts.Password,
|
|
|
|
PrivateMode: opts.PrivateMode,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
|
|
|
}, nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Login authenticates an account with Gogs using basic authenticaiton. The
|
|
|
|
// Gogs account details are returned when the user is successfully authenticated.
|
|
|
|
func (c *client) Login(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
|
|
|
|
|
|
|
// try to fetch drone token if it exists
|
|
|
|
var accessToken string
|
|
|
|
tokens, err := client.ListAccessTokens(username, password)
|
2016-05-01 23:30:00 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, token := range tokens {
|
|
|
|
if token.Name == "drone" {
|
|
|
|
accessToken = token.Sha1
|
|
|
|
break
|
|
|
|
}
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if drone token not found, create it
|
|
|
|
if accessToken == "" {
|
2016-05-01 23:30:00 +00:00
|
|
|
token, terr := client.CreateAccessToken(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
gogs.CreateAccessTokenOption{Name: "drone"},
|
|
|
|
)
|
|
|
|
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.
|
|
|
|
func (c *client) Auth(token, secret string) (string, error) {
|
|
|
|
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.
|
|
|
|
func (c *client) Teams(u *model.User) ([]*model.Team, error) {
|
|
|
|
var empty []*model.Team
|
|
|
|
return empty, nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Repo returns the named Gogs repository.
|
|
|
|
func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) {
|
|
|
|
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
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
return toRepo(repo), 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.
|
|
|
|
func (c *client) Repos(u *model.User) ([]*model.RepoLite, error) {
|
2015-10-22 23:36:43 +00:00
|
|
|
repos := []*model.RepoLite{}
|
|
|
|
|
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 {
|
2015-10-22 23:36:43 +00:00
|
|
|
repos = append(repos, toRepoLite(repo))
|
|
|
|
}
|
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Perm returns the user permissions for the named Gogs repository.
|
|
|
|
func (c *client) Perm(u *model.User, owner, name string) (*model.Perm, error) {
|
|
|
|
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
|
|
|
|
}
|
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.
|
|
|
|
func (c *client) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
|
|
|
|
client := c.newClientToken(u.Token)
|
2016-03-22 10:34:33 +00:00
|
|
|
cfg, err := client.GetFile(r.Owner, r.Name, b.Commit, f)
|
|
|
|
return cfg, err
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Status is not supported by the Gogs driver.
|
|
|
|
func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link string) error {
|
|
|
|
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) {
|
|
|
|
if c.Password != "" {
|
|
|
|
return &model.Netrc{
|
|
|
|
Login: c.Username,
|
|
|
|
Password: c.Password,
|
|
|
|
Machine: c.Machine,
|
|
|
|
}, 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",
|
2016-05-01 23:30:00 +00:00
|
|
|
Machine: c.Machine,
|
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.
|
|
|
|
func (c *client) Activate(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,
|
|
|
|
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.
|
|
|
|
func (c *client) Deactivate(u *model.User, r *model.Repo, link string) error {
|
|
|
|
return nil
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// Hook parses the incoming Gogs hook and returns the Repository and Build
|
|
|
|
// details. If the hook is unsupported nil values are returned.
|
|
|
|
func (c *client) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
|
2015-10-22 23:36:43 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
repo *model.Repo
|
|
|
|
build *model.Build
|
|
|
|
)
|
|
|
|
|
|
|
|
switch r.Header.Get("X-Gogs-Event") {
|
|
|
|
case "push":
|
2016-05-01 23:30:00 +00:00
|
|
|
var push *pushHook
|
2015-10-22 23:36:43 +00:00
|
|
|
push, err = parsePush(r.Body)
|
|
|
|
if err == nil {
|
|
|
|
repo = repoFromPush(push)
|
|
|
|
build = buildFromPush(push)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return repo, build, err
|
|
|
|
}
|
2015-10-27 23:48:05 +00:00
|
|
|
|
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
|
|
|
}
|