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 (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-10-27 17:32:14 +00:00
|
|
|
"net/url"
|
2015-10-22 23:36:43 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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"
|
2015-10-22 23:36:43 +00:00
|
|
|
)
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// helper function that converts a Gogs repository to a Woodpecker repository.
|
2017-08-26 22:59:57 +00:00
|
|
|
func toRepo(from *gogs.Repository, privateMode bool) *model.Repo {
|
2015-10-22 23:36:43 +00:00
|
|
|
name := strings.Split(from.FullName, "/")[1]
|
2015-10-27 17:32:14 +00:00
|
|
|
avatar := expandAvatar(
|
2021-10-28 08:09:27 +00:00
|
|
|
from.HTMLURL,
|
2015-10-27 17:32:14 +00:00
|
|
|
from.Owner.AvatarUrl,
|
|
|
|
)
|
2015-10-22 23:36:43 +00:00
|
|
|
return &model.Repo{
|
2022-09-05 15:08:51 +00:00
|
|
|
RemoteID: model.RemoteID(fmt.Sprint(from.ID)),
|
2021-11-22 11:55:13 +00:00
|
|
|
SCMKind: model.RepoGit,
|
|
|
|
Name: name,
|
|
|
|
Owner: from.Owner.UserName,
|
|
|
|
FullName: from.FullName,
|
|
|
|
Avatar: avatar,
|
|
|
|
Link: from.HTMLURL,
|
2022-09-07 17:16:40 +00:00
|
|
|
IsSCMPrivate: from.Private || privateMode,
|
2021-11-22 11:55:13 +00:00
|
|
|
Clone: from.CloneURL,
|
|
|
|
Branch: from.DefaultBranch,
|
2015-10-22 23:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// helper function that converts a Gogs permission to a Woodpecker permission.
|
2021-10-28 08:09:27 +00:00
|
|
|
func toPerm(from *gogs.Permission) *model.Perm {
|
2015-10-22 23:36:43 +00:00
|
|
|
return &model.Perm{
|
|
|
|
Pull: from.Pull,
|
|
|
|
Push: from.Push,
|
|
|
|
Admin: from.Admin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// helper function that converts a Gogs team to a Woodpecker team.
|
2016-05-11 19:32:18 +00:00
|
|
|
func toTeam(from *gogs.Organization, link string) *model.Team {
|
|
|
|
return &model.Team{
|
|
|
|
Login: from.UserName,
|
|
|
|
Avatar: expandAvatar(link, from.AvatarUrl),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// helper function that extracts the Pipeline data from a Gogs push hook
|
|
|
|
func pipelineFromPush(hook *pushHook) *model.Pipeline {
|
2015-10-27 17:32:14 +00:00
|
|
|
avatar := expandAvatar(
|
2022-09-07 17:16:40 +00:00
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.Sender.AvatarUrl),
|
2015-10-27 17:32:14 +00:00
|
|
|
)
|
2016-11-25 12:06:07 +00:00
|
|
|
author := hook.Sender.Login
|
|
|
|
if author == "" {
|
2022-09-07 17:16:40 +00:00
|
|
|
author = hook.Sender.UserName
|
2016-05-06 20:14:19 +00:00
|
|
|
}
|
2022-09-07 17:16:40 +00:00
|
|
|
sender := hook.Sender.UserName
|
2017-03-18 08:49:27 +00:00
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2016-05-06 20:14:19 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return &model.Pipeline{
|
2016-11-25 12:06:07 +00:00
|
|
|
Event: model.EventPush,
|
2015-10-22 23:36:43 +00:00
|
|
|
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,
|
2018-01-25 08:05:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// helper function that extracts the pipeline data from a Gogs tag hook
|
|
|
|
func pipelineFromTag(hook *pushHook) *model.Pipeline {
|
2016-11-25 12:06:07 +00:00
|
|
|
avatar := expandAvatar(
|
2022-09-07 17:16:40 +00:00
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.Sender.AvatarUrl),
|
2016-11-25 12:06:07 +00:00
|
|
|
)
|
|
|
|
author := hook.Sender.Login
|
|
|
|
if author == "" {
|
2022-09-07 17:16:40 +00:00
|
|
|
author = hook.Sender.UserName
|
2016-11-25 12:06:07 +00:00
|
|
|
}
|
2022-09-07 17:16:40 +00:00
|
|
|
sender := hook.Sender.UserName
|
2017-03-18 08:49:27 +00:00
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2016-11-25 12:06:07 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return &model.Pipeline{
|
2016-11-25 12:06:07 +00:00
|
|
|
Event: model.EventTag,
|
|
|
|
Commit: hook.After,
|
|
|
|
Ref: fmt.Sprintf("refs/tags/%s", hook.Ref),
|
2022-09-07 17:16:40 +00:00
|
|
|
Link: fmt.Sprintf("%s/src/%s", hook.Repo.HTMLURL, hook.Ref),
|
2016-11-25 12:06:07 +00:00
|
|
|
Branch: fmt.Sprintf("refs/tags/%s", hook.Ref),
|
|
|
|
Message: fmt.Sprintf("created tag %s", hook.Ref),
|
2015-10-27 17:32:14 +00:00
|
|
|
Avatar: avatar,
|
2016-11-25 12:06:07 +00:00
|
|
|
Author: author,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: sender,
|
2015-10-22 23:36:43 +00:00
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// helper function that extracts the Pipeline data from a Gogs pull_request hook
|
|
|
|
func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
|
2017-01-03 08:38:05 +00:00
|
|
|
avatar := expandAvatar(
|
2022-09-07 17:16:40 +00:00
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.PullRequest.User.AvatarUrl),
|
2017-01-03 08:38:05 +00:00
|
|
|
)
|
2022-09-07 17:16:40 +00:00
|
|
|
sender := hook.Sender.UserName
|
2017-03-18 08:49:27 +00:00
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2017-01-03 08:38:05 +00:00
|
|
|
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,
|
2017-01-03 08:38:05 +00:00
|
|
|
Message: hook.PullRequest.Title,
|
2022-09-07 17:16:40 +00:00
|
|
|
Author: hook.PullRequest.User.UserName,
|
2017-01-03 08:38:05 +00:00
|
|
|
Avatar: avatar,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: sender,
|
2017-01-03 08:38:05 +00:00
|
|
|
Title: hook.PullRequest.Title,
|
|
|
|
Refspec: fmt.Sprintf("%s:%s",
|
2017-03-18 15:20:09 +00:00
|
|
|
hook.PullRequest.HeadBranch,
|
|
|
|
hook.PullRequest.BaseBranch,
|
2017-01-03 08:38:05 +00:00
|
|
|
),
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline
|
2017-01-03 08:38:05 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// helper function that parses a push hook from a read closer.
|
|
|
|
func parsePush(r io.Reader) (*pushHook, error) {
|
|
|
|
push := new(pushHook)
|
2015-10-22 23:36:43 +00:00
|
|
|
err := json.NewDecoder(r).Decode(push)
|
|
|
|
return push, err
|
|
|
|
}
|
|
|
|
|
2017-01-03 08:38:05 +00:00
|
|
|
func parsePullRequest(r io.Reader) (*pullRequestHook, error) {
|
|
|
|
pr := new(pullRequestHook)
|
|
|
|
err := json.NewDecoder(r).Decode(pr)
|
|
|
|
return pr, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// fixMalformedAvatar is a helper function that fixes an avatar url if malformed
|
|
|
|
// (currently a known bug with gogs)
|
2015-10-22 23:36:43 +00:00
|
|
|
func fixMalformedAvatar(url string) string {
|
|
|
|
index := strings.Index(url, "///")
|
|
|
|
if index != -1 {
|
|
|
|
return url[index+1:]
|
|
|
|
}
|
2015-10-28 01:44:41 +00:00
|
|
|
index = strings.Index(url, "//avatars/")
|
|
|
|
if index != -1 {
|
|
|
|
return strings.Replace(url, "//avatars/", "/avatars/", -1)
|
|
|
|
}
|
2015-10-22 23:36:43 +00:00
|
|
|
return url
|
|
|
|
}
|
2015-10-27 17:32:14 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
// expandAvatar is a helper function that converts a relative avatar URL to the
|
2016-07-06 13:58:31 +00:00
|
|
|
// absolute url.
|
2015-10-27 17:32:14 +00:00
|
|
|
func expandAvatar(repo, rawurl string) string {
|
2016-07-06 13:58:31 +00:00
|
|
|
aurl, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
2015-10-27 17:32:14 +00:00
|
|
|
return rawurl
|
|
|
|
}
|
2016-07-06 13:58:31 +00:00
|
|
|
if aurl.IsAbs() {
|
|
|
|
// Url is already absolute
|
|
|
|
return aurl.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve to base
|
|
|
|
burl, err := url.Parse(repo)
|
2015-10-27 17:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return rawurl
|
|
|
|
}
|
2016-07-06 13:58:31 +00:00
|
|
|
aurl = burl.ResolveReference(aurl)
|
|
|
|
|
|
|
|
return aurl.String()
|
2015-10-27 17:32:14 +00:00
|
|
|
}
|