2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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.
|
|
|
|
|
2017-05-01 12:33:06 +02:00
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-05-01 15:14:57 +02:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-10-12 09:25:13 +02:00
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2022-01-18 00:46:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
2017-05-01 12:33:06 +02:00
|
|
|
)
|
|
|
|
|
2021-10-02 10:59:34 +02:00
|
|
|
// helper function that converts a Gitea repository to a Woodpecker repository.
|
2022-01-31 16:38:00 +02:00
|
|
|
func toRepo(from *gitea.Repository) *model.Repo {
|
2017-05-01 12:33:06 +02:00
|
|
|
name := strings.Split(from.FullName, "/")[1]
|
|
|
|
avatar := expandAvatar(
|
|
|
|
from.HTMLURL,
|
|
|
|
from.Owner.AvatarURL,
|
|
|
|
)
|
|
|
|
return &model.Repo{
|
2021-11-22 13:55:13 +02:00
|
|
|
SCMKind: model.RepoGit,
|
|
|
|
Name: name,
|
|
|
|
Owner: from.Owner.UserName,
|
|
|
|
FullName: from.FullName,
|
|
|
|
Avatar: avatar,
|
|
|
|
Link: from.HTMLURL,
|
2022-01-31 16:38:00 +02:00
|
|
|
IsSCMPrivate: from.Private,
|
2021-11-22 13:55:13 +02:00
|
|
|
Clone: from.CloneURL,
|
|
|
|
Branch: from.DefaultBranch,
|
2017-05-01 12:33:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 10:59:34 +02:00
|
|
|
// helper function that converts a Gitea permission to a Woodpecker permission.
|
2017-05-01 12:33:06 +02:00
|
|
|
func toPerm(from *gitea.Permission) *model.Perm {
|
|
|
|
return &model.Perm{
|
|
|
|
Pull: from.Pull,
|
|
|
|
Push: from.Push,
|
|
|
|
Admin: from.Admin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 10:59:34 +02:00
|
|
|
// helper function that converts a Gitea team to a Woodpecker team.
|
2017-05-01 12:33:06 +02:00
|
|
|
func toTeam(from *gitea.Organization, link string) *model.Team {
|
|
|
|
return &model.Team{
|
|
|
|
Login: from.UserName,
|
|
|
|
Avatar: expandAvatar(link, from.AvatarURL),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:08:57 +02:00
|
|
|
// helper function that extracts the Build data from a Gitea push hook
|
2017-05-01 12:33:06 +02:00
|
|
|
func buildFromPush(hook *pushHook) *model.Build {
|
|
|
|
avatar := expandAvatar(
|
|
|
|
hook.Repo.URL,
|
|
|
|
fixMalformedAvatar(hook.Sender.Avatar),
|
|
|
|
)
|
|
|
|
author := hook.Sender.Login
|
|
|
|
if author == "" {
|
|
|
|
author = hook.Sender.Username
|
|
|
|
}
|
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
|
|
|
|
2018-06-11 10:47:25 +02:00
|
|
|
message := ""
|
2022-01-08 23:06:00 +02:00
|
|
|
link := hook.Compare
|
2018-06-11 10:47:25 +02:00
|
|
|
if len(hook.Commits) > 0 {
|
|
|
|
message = hook.Commits[0].Message
|
|
|
|
}
|
|
|
|
|
2022-01-08 23:06:00 +02:00
|
|
|
if len(hook.Commits) == 1 {
|
|
|
|
link = hook.Commits[0].URL
|
|
|
|
}
|
|
|
|
|
2017-05-01 12:33:06 +02:00
|
|
|
return &model.Build{
|
2021-07-04 20:38:59 +02:00
|
|
|
Event: model.EventPush,
|
|
|
|
Commit: hook.After,
|
|
|
|
Ref: hook.Ref,
|
2022-01-08 23:06:00 +02:00
|
|
|
Link: link,
|
2021-07-04 20:38:59 +02:00
|
|
|
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
|
|
|
|
Message: message,
|
|
|
|
Avatar: avatar,
|
|
|
|
Author: author,
|
|
|
|
Email: hook.Sender.Email,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
Sender: sender,
|
|
|
|
ChangedFiles: getChangedFilesFromPushHook(hook),
|
2017-05-01 12:33:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:38:59 +02:00
|
|
|
func getChangedFilesFromPushHook(hook *pushHook) []string {
|
2022-01-18 00:46:59 +02:00
|
|
|
// assume a capacity of 4 changed files per commit
|
|
|
|
files := make([]string, 0, len(hook.Commits)*4)
|
2022-01-08 23:16:07 +02:00
|
|
|
for _, c := range hook.Commits {
|
|
|
|
files = append(files, c.Added...)
|
|
|
|
files = append(files, c.Removed...)
|
|
|
|
files = append(files, c.Modified...)
|
2021-07-04 20:38:59 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 00:46:59 +02:00
|
|
|
return utils.DedupStrings(files)
|
2021-07-04 20:38:59 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:08:57 +02:00
|
|
|
// helper function that extracts the Build data from a Gitea tag hook
|
2017-05-01 12:33:06 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Build{
|
|
|
|
Event: model.EventTag,
|
2017-11-04 17:06:37 +02:00
|
|
|
Commit: hook.Sha,
|
2017-05-01 12:33:06 +02:00
|
|
|
Ref: fmt.Sprintf("refs/tags/%s", hook.Ref),
|
2017-11-04 17:06:37 +02:00
|
|
|
Link: fmt.Sprintf("%s/src/tag/%s", hook.Repo.URL, hook.Ref),
|
2017-05-01 12:33:06 +02:00
|
|
|
Branch: fmt.Sprintf("refs/tags/%s", hook.Ref),
|
|
|
|
Message: fmt.Sprintf("created tag %s", hook.Ref),
|
|
|
|
Avatar: avatar,
|
|
|
|
Author: author,
|
|
|
|
Sender: sender,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:08:57 +02:00
|
|
|
// helper function that extracts the Build data from a Gitea pull_request hook
|
2017-05-01 12:33:06 +02:00
|
|
|
func buildFromPullRequest(hook *pullRequestHook) *model.Build {
|
|
|
|
avatar := expandAvatar(
|
|
|
|
hook.Repo.URL,
|
|
|
|
fixMalformedAvatar(hook.PullRequest.User.Avatar),
|
|
|
|
)
|
|
|
|
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),
|
2018-01-29 14:20:02 +02:00
|
|
|
Branch: hook.PullRequest.Base.Ref,
|
2017-05-01 12:33:06 +02:00
|
|
|
Message: hook.PullRequest.Title,
|
|
|
|
Author: hook.PullRequest.User.Username,
|
|
|
|
Avatar: avatar,
|
|
|
|
Sender: sender,
|
|
|
|
Title: hook.PullRequest.Title,
|
|
|
|
Refspec: fmt.Sprintf("%s:%s",
|
2018-01-29 14:20:02 +02:00
|
|
|
hook.PullRequest.Head.Ref,
|
|
|
|
hook.PullRequest.Base.Ref,
|
2017-05-01 12:33:06 +02:00
|
|
|
),
|
|
|
|
}
|
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:08:57 +02:00
|
|
|
// helper function that extracts the Repository data from a Gitea push hook
|
2017-05-01 12:33:06 +02:00
|
|
|
func repoFromPush(hook *pushHook) *model.Repo {
|
|
|
|
return &model.Repo{
|
|
|
|
Name: hook.Repo.Name,
|
|
|
|
Owner: hook.Repo.Owner.Username,
|
|
|
|
FullName: hook.Repo.FullName,
|
|
|
|
Link: hook.Repo.URL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:08:57 +02:00
|
|
|
// helper function that extracts the Repository data from a Gitea pull_request hook
|
2017-05-01 12:33:06 +02:00
|
|
|
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
|
2017-05-01 16:43:23 +02:00
|
|
|
// (currently a known bug with gitea)
|
2017-05-01 12:33:06 +02:00
|
|
|
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()
|
|
|
|
}
|
2021-09-27 08:11:11 +02:00
|
|
|
|
|
|
|
// helper function to return matching hooks.
|
|
|
|
func matchingHooks(hooks []*gitea.Hook, rawurl string) *gitea.Hook {
|
|
|
|
link, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if val, ok := hook.Config["url"]; ok {
|
|
|
|
hookurl, err := url.Parse(val)
|
|
|
|
if err == nil && hookurl.Host == link.Host {
|
|
|
|
return hook
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|