2015-10-23 01:36:43 +02:00
|
|
|
package gogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-10-27 19:32:14 +02:00
|
|
|
"net/url"
|
2015-10-23 01:36:43 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/gogits/go-gogs-client"
|
|
|
|
)
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// helper function that converts a Gogs repository to a Drone repository.
|
2015-10-23 01:36:43 +02:00
|
|
|
func toRepo(from *gogs.Repository) *model.Repo {
|
|
|
|
name := strings.Split(from.FullName, "/")[1]
|
2015-10-27 19:32:14 +02:00
|
|
|
avatar := expandAvatar(
|
|
|
|
from.HtmlUrl,
|
|
|
|
from.Owner.AvatarUrl,
|
|
|
|
)
|
2015-10-23 01:36:43 +02:00
|
|
|
return &model.Repo{
|
2015-10-27 22:03:37 +02:00
|
|
|
Kind: model.RepoGit,
|
2015-10-23 01:36:43 +02:00
|
|
|
Name: name,
|
|
|
|
Owner: from.Owner.UserName,
|
|
|
|
FullName: from.FullName,
|
2015-10-27 19:32:14 +02:00
|
|
|
Avatar: avatar,
|
2015-10-23 01:36:43 +02:00
|
|
|
Link: from.HtmlUrl,
|
|
|
|
IsPrivate: from.Private,
|
|
|
|
Clone: from.CloneUrl,
|
|
|
|
Branch: "master",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// helper function that converts a Gogs permission to a Drone permission.
|
2015-10-23 01:36:43 +02:00
|
|
|
func toPerm(from gogs.Permission) *model.Perm {
|
|
|
|
return &model.Perm{
|
|
|
|
Pull: from.Pull,
|
|
|
|
Push: from.Push,
|
|
|
|
Admin: from.Admin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:32:18 +02:00
|
|
|
// helper function that converts a Gogs team to a Drone team.
|
|
|
|
func toTeam(from *gogs.Organization, link string) *model.Team {
|
|
|
|
return &model.Team{
|
|
|
|
Login: from.UserName,
|
|
|
|
Avatar: expandAvatar(link, from.AvatarUrl),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// helper function that extracts the Build data from a Gogs push hook
|
|
|
|
func buildFromPush(hook *pushHook) *model.Build {
|
2015-10-27 19:32:14 +02:00
|
|
|
avatar := expandAvatar(
|
2016-05-02 01:30:00 +02:00
|
|
|
hook.Repo.URL,
|
2015-10-27 19:32:14 +02:00
|
|
|
fixMalformedAvatar(hook.Sender.Avatar),
|
|
|
|
)
|
2016-11-25 14:06:07 +02:00
|
|
|
author := hook.Sender.Login
|
|
|
|
if author == "" {
|
|
|
|
author = hook.Sender.Username
|
2016-05-06 22:14:19 +02:00
|
|
|
}
|
2017-03-18 10:49:27 +02:00
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2016-05-06 22:14:19 +02:00
|
|
|
|
2015-10-23 01:36:43 +02:00
|
|
|
return &model.Build{
|
2016-11-25 14:06:07 +02:00
|
|
|
Event: model.EventPush,
|
2015-10-23 01:36:43 +02:00
|
|
|
Commit: hook.After,
|
|
|
|
Ref: hook.Ref,
|
|
|
|
Link: hook.Compare,
|
|
|
|
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
|
2016-11-25 14:06:07 +02:00
|
|
|
Message: hook.Commits[0].Message,
|
|
|
|
Avatar: avatar,
|
|
|
|
Author: author,
|
2017-06-21 06:53:47 +02:00
|
|
|
Email: hook.Pusher.Email,
|
2016-11-25 14:06:07 +02:00
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
2017-03-18 10:49:27 +02:00
|
|
|
Sender: sender,
|
2016-11-25 14:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function that extracts the Build data from a Gogs tag hook
|
|
|
|
func buildFromTag(hook *pushHook) *model.Build {
|
|
|
|
avatar := expandAvatar(
|
|
|
|
hook.Repo.URL,
|
|
|
|
fixMalformedAvatar(hook.Sender.Avatar),
|
|
|
|
)
|
|
|
|
author := hook.Sender.Login
|
|
|
|
if author == "" {
|
|
|
|
author = hook.Sender.Username
|
|
|
|
}
|
2017-03-18 10:49:27 +02:00
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2016-11-25 14:06:07 +02:00
|
|
|
|
|
|
|
return &model.Build{
|
|
|
|
Event: model.EventTag,
|
|
|
|
Commit: hook.After,
|
|
|
|
Ref: fmt.Sprintf("refs/tags/%s", hook.Ref),
|
|
|
|
Link: fmt.Sprintf("%s/src/%s", hook.Repo.URL, hook.Ref),
|
|
|
|
Branch: fmt.Sprintf("refs/tags/%s", hook.Ref),
|
|
|
|
Message: fmt.Sprintf("created tag %s", hook.Ref),
|
2015-10-27 19:32:14 +02:00
|
|
|
Avatar: avatar,
|
2016-11-25 14:06:07 +02:00
|
|
|
Author: author,
|
2017-03-18 10:49:27 +02:00
|
|
|
Sender: sender,
|
2015-10-23 01:36:43 +02:00
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:38:05 +02:00
|
|
|
// helper function that extracts the Build data from a Gogs pull_request hook
|
|
|
|
func buildFromPullRequest(hook *pullRequestHook) *model.Build {
|
|
|
|
avatar := expandAvatar(
|
|
|
|
hook.Repo.URL,
|
|
|
|
fixMalformedAvatar(hook.PullRequest.User.Avatar),
|
|
|
|
)
|
2017-03-18 10:49:27 +02:00
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2017-01-03 10:38:05 +02:00
|
|
|
build := &model.Build{
|
|
|
|
Event: model.EventPull,
|
|
|
|
Commit: hook.PullRequest.Head.Sha,
|
|
|
|
Link: hook.PullRequest.URL,
|
|
|
|
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
|
2017-03-18 17:20:09 +02:00
|
|
|
Branch: hook.PullRequest.BaseBranch,
|
2017-01-03 10:38:05 +02:00
|
|
|
Message: hook.PullRequest.Title,
|
|
|
|
Author: hook.PullRequest.User.Username,
|
|
|
|
Avatar: avatar,
|
2017-03-18 10:49:27 +02:00
|
|
|
Sender: sender,
|
2017-01-03 10:38:05 +02:00
|
|
|
Title: hook.PullRequest.Title,
|
|
|
|
Refspec: fmt.Sprintf("%s:%s",
|
2017-03-18 17:20:09 +02:00
|
|
|
hook.PullRequest.HeadBranch,
|
|
|
|
hook.PullRequest.BaseBranch,
|
2017-01-03 10:38:05 +02:00
|
|
|
),
|
|
|
|
}
|
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// helper function that extracts the Repository data from a Gogs push hook
|
|
|
|
func repoFromPush(hook *pushHook) *model.Repo {
|
2015-10-23 01:36:43 +02:00
|
|
|
return &model.Repo{
|
|
|
|
Name: hook.Repo.Name,
|
|
|
|
Owner: hook.Repo.Owner.Username,
|
2016-11-25 14:06:07 +02:00
|
|
|
FullName: hook.Repo.FullName,
|
2016-05-02 01:30:00 +02:00
|
|
|
Link: hook.Repo.URL,
|
2015-10-23 01:36:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:38:05 +02:00
|
|
|
// helper function that extracts the Repository data from a Gogs pull_request hook
|
|
|
|
func repoFromPullRequest(hook *pullRequestHook) *model.Repo {
|
|
|
|
return &model.Repo{
|
|
|
|
Name: hook.Repo.Name,
|
|
|
|
Owner: hook.Repo.Owner.Username,
|
|
|
|
FullName: hook.Repo.FullName,
|
|
|
|
Link: hook.Repo.URL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// helper function that parses a push hook from a read closer.
|
|
|
|
func parsePush(r io.Reader) (*pushHook, error) {
|
|
|
|
push := new(pushHook)
|
2015-10-23 01:36:43 +02:00
|
|
|
err := json.NewDecoder(r).Decode(push)
|
|
|
|
return push, err
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:38:05 +02:00
|
|
|
func parsePullRequest(r io.Reader) (*pullRequestHook, error) {
|
|
|
|
pr := new(pullRequestHook)
|
|
|
|
err := json.NewDecoder(r).Decode(pr)
|
|
|
|
return pr, err
|
|
|
|
}
|
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// fixMalformedAvatar is a helper function that fixes an avatar url if malformed
|
|
|
|
// (currently a known bug with gogs)
|
2015-10-23 01:36:43 +02:00
|
|
|
func fixMalformedAvatar(url string) string {
|
|
|
|
index := strings.Index(url, "///")
|
|
|
|
if index != -1 {
|
|
|
|
return url[index+1:]
|
|
|
|
}
|
2015-10-28 03:44:41 +02:00
|
|
|
index = strings.Index(url, "//avatars/")
|
|
|
|
if index != -1 {
|
|
|
|
return strings.Replace(url, "//avatars/", "/avatars/", -1)
|
|
|
|
}
|
2015-10-23 01:36:43 +02:00
|
|
|
return url
|
|
|
|
}
|
2015-10-27 19:32:14 +02:00
|
|
|
|
2016-05-02 01:30:00 +02:00
|
|
|
// expandAvatar is a helper function that converts a relative avatar URL to the
|
2016-07-06 15:58:31 +02:00
|
|
|
// absolute url.
|
2015-10-27 19:32:14 +02:00
|
|
|
func expandAvatar(repo, rawurl string) string {
|
2016-07-06 15:58:31 +02:00
|
|
|
aurl, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
2015-10-27 19:32:14 +02:00
|
|
|
return rawurl
|
|
|
|
}
|
2016-07-06 15:58:31 +02:00
|
|
|
if aurl.IsAbs() {
|
|
|
|
// Url is already absolute
|
|
|
|
return aurl.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve to base
|
|
|
|
burl, err := url.Parse(repo)
|
2015-10-27 19:32:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return rawurl
|
|
|
|
}
|
2016-07-06 15:58:31 +02:00
|
|
|
aurl = burl.ResolveReference(aurl)
|
|
|
|
|
|
|
|
return aurl.String()
|
2015-10-27 19:32:14 +02:00
|
|
|
}
|