woodpecker/remote/gogs/helper.go

226 lines
5.7 KiB
Go
Raw Normal View History

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.
package gogs
import (
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
"time"
"github.com/gogits/go-gogs-client"
"github.com/woodpecker-ci/woodpecker/model"
)
// helper function that converts a Gogs repository to a Drone repository.
2017-08-26 22:59:57 +00:00
func toRepo(from *gogs.Repository, privateMode bool) *model.Repo {
name := strings.Split(from.FullName, "/")[1]
avatar := expandAvatar(
from.HtmlUrl,
from.Owner.AvatarUrl,
)
2017-08-26 22:59:57 +00:00
private := from.Private
if privateMode {
private = true
}
return &model.Repo{
Kind: model.RepoGit,
Name: name,
Owner: from.Owner.UserName,
FullName: from.FullName,
Avatar: avatar,
Link: from.HtmlUrl,
2017-08-26 22:59:57 +00:00
IsPrivate: private,
Clone: from.CloneUrl,
Branch: "master",
}
}
// helper function that converts a Gogs permission to a Drone permission.
func toPerm(from gogs.Permission) *model.Perm {
return &model.Perm{
Pull: from.Pull,
Push: from.Push,
Admin: from.Admin,
}
}
2016-05-11 19:32:18 +00: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),
}
}
// helper function that extracts the Build data from a Gogs push hook
func buildFromPush(hook *pushHook) *model.Build {
avatar := expandAvatar(
hook.Repo.URL,
fixMalformedAvatar(hook.Sender.Avatar),
)
2016-11-25 12:06:07 +00:00
author := hook.Sender.Login
if author == "" {
author = hook.Sender.Username
2016-05-06 20:14:19 +00:00
}
2017-03-18 08:49:27 +00:00
sender := hook.Sender.Username
if sender == "" {
sender = hook.Sender.Login
}
2016-05-06 20:14:19 +00:00
return &model.Build{
2016-11-25 12:06:07 +00:00
Event: model.EventPush,
Commit: hook.After,
Ref: hook.Ref,
Link: hook.Compare,
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
2016-11-25 12:06:07 +00:00
Message: hook.Commits[0].Message,
Avatar: avatar,
Author: author,
Email: hook.Sender.Email,
2016-11-25 12:06:07 +00:00
Timestamp: time.Now().UTC().Unix(),
2017-03-18 08:49:27 +00:00
Sender: sender,
2016-11-25 12:06:07 +00: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 08:49:27 +00:00
sender := hook.Sender.Username
if sender == "" {
sender = hook.Sender.Login
}
2016-11-25 12:06:07 +00: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),
Avatar: avatar,
2016-11-25 12:06:07 +00:00
Author: author,
2017-03-18 08:49:27 +00:00
Sender: sender,
Timestamp: time.Now().UTC().Unix(),
}
}
// 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 08:49:27 +00:00
sender := hook.Sender.Username
if sender == "" {
sender = hook.Sender.Login
}
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 15:20:09 +00:00
Branch: hook.PullRequest.BaseBranch,
Message: hook.PullRequest.Title,
Author: hook.PullRequest.User.Username,
Avatar: avatar,
2017-03-18 08:49:27 +00:00
Sender: sender,
Title: hook.PullRequest.Title,
Refspec: fmt.Sprintf("%s:%s",
2017-03-18 15:20:09 +00:00
hook.PullRequest.HeadBranch,
hook.PullRequest.BaseBranch,
),
}
return build
}
// helper function that extracts the Repository data from a Gogs push hook
func repoFromPush(hook *pushHook) *model.Repo {
return &model.Repo{
Name: hook.Repo.Name,
Owner: hook.Repo.Owner.Username,
2016-11-25 12:06:07 +00:00
FullName: hook.Repo.FullName,
Link: hook.Repo.URL,
}
}
// 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,
}
}
// helper function that parses a push hook from a read closer.
func parsePush(r io.Reader) (*pushHook, error) {
push := new(pushHook)
err := json.NewDecoder(r).Decode(push)
return push, err
}
func parsePullRequest(r io.Reader) (*pullRequestHook, error) {
pr := new(pullRequestHook)
err := json.NewDecoder(r).Decode(pr)
return pr, err
}
// fixMalformedAvatar is a helper function that fixes an avatar url if malformed
// (currently a known bug with gogs)
func fixMalformedAvatar(url string) string {
index := strings.Index(url, "///")
if index != -1 {
return url[index+1:]
}
index = strings.Index(url, "//avatars/")
if index != -1 {
return strings.Replace(url, "//avatars/", "/avatars/", -1)
}
return url
}
// expandAvatar is a helper function that converts a relative avatar URL to the
// absolute url.
func expandAvatar(repo, rawurl string) string {
aurl, err := url.Parse(rawurl)
if err != nil {
return rawurl
}
if aurl.IsAbs() {
// Url is already absolute
return aurl.String()
}
// Resolve to base
burl, err := url.Parse(repo)
if err != nil {
return rawurl
}
aurl = burl.ResolveReference(aurl)
return aurl.String()
}