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.
|
|
|
|
|
2017-05-01 10:33:06 +00:00
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-05-01 13:14:57 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
2017-05-01 10:33:06 +00:00
|
|
|
)
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// toRepo converts a Gitea repository to a Woodpecker repository.
|
2022-01-31 14:38:00 +00:00
|
|
|
func toRepo(from *gitea.Repository) *model.Repo {
|
2017-05-01 10:33:06 +00:00
|
|
|
name := strings.Split(from.FullName, "/")[1]
|
|
|
|
avatar := expandAvatar(
|
|
|
|
from.HTMLURL,
|
|
|
|
from.Owner.AvatarURL,
|
|
|
|
)
|
|
|
|
return &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
|
|
|
|
SCMKind: model.RepoGit,
|
|
|
|
Name: name,
|
|
|
|
Owner: from.Owner.UserName,
|
|
|
|
FullName: from.FullName,
|
|
|
|
Avatar: avatar,
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: from.HTMLURL,
|
2022-11-15 14:01:23 +00:00
|
|
|
IsSCMPrivate: from.Private || from.Owner.Visibility != gitea.VisibleTypePublic,
|
|
|
|
Clone: from.CloneURL,
|
2023-08-12 15:39:13 +00:00
|
|
|
CloneSSH: from.SSHURL,
|
2022-11-15 14:01:23 +00:00
|
|
|
Branch: from.DefaultBranch,
|
2023-03-21 22:01:59 +00:00
|
|
|
Perm: toPerm(from.Permissions),
|
2023-12-24 09:04:18 +00:00
|
|
|
PREnabled: from.HasPullRequests,
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// toPerm converts a Gitea permission to a Woodpecker permission.
|
2017-05-01 10:33:06 +00:00
|
|
|
func toPerm(from *gitea.Permission) *model.Perm {
|
|
|
|
return &model.Perm{
|
|
|
|
Pull: from.Pull,
|
|
|
|
Push: from.Push,
|
|
|
|
Admin: from.Admin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// toTeam converts a Gitea team to a Woodpecker team.
|
2017-05-01 10:33:06 +00:00
|
|
|
func toTeam(from *gitea.Organization, link string) *model.Team {
|
|
|
|
return &model.Team{
|
|
|
|
Login: from.UserName,
|
|
|
|
Avatar: expandAvatar(link, from.AvatarURL),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// pipelineFromPush extracts the Pipeline data from a Gitea push hook.
|
2022-10-18 01:24:12 +00:00
|
|
|
func pipelineFromPush(hook *pushHook) *model.Pipeline {
|
2017-05-01 10:33:06 +00:00
|
|
|
avatar := expandAvatar(
|
2022-09-07 17:16:40 +00:00
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.Sender.AvatarURL),
|
2017-05-01 10:33:06 +00:00
|
|
|
)
|
|
|
|
|
2024-01-09 20:35:37 +00:00
|
|
|
var message string
|
2022-01-08 21:06:00 +00:00
|
|
|
link := hook.Compare
|
2018-06-11 08:47:25 +00:00
|
|
|
if len(hook.Commits) > 0 {
|
|
|
|
message = hook.Commits[0].Message
|
2024-02-22 07:02:19 +00:00
|
|
|
if len(hook.Commits) == 1 {
|
|
|
|
link = hook.Commits[0].URL
|
|
|
|
}
|
2022-09-02 14:41:51 +00:00
|
|
|
} else {
|
|
|
|
message = hook.HeadCommit.Message
|
2024-02-22 07:02:19 +00:00
|
|
|
link = hook.HeadCommit.URL
|
2022-01-08 21:06:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return &model.Pipeline{
|
2021-07-04 18:38:59 +00:00
|
|
|
Event: model.EventPush,
|
|
|
|
Commit: hook.After,
|
|
|
|
Ref: hook.Ref,
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: link,
|
2021-07-04 18:38:59 +00:00
|
|
|
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
|
|
|
|
Message: message,
|
|
|
|
Avatar: avatar,
|
2022-09-07 17:16:40 +00:00
|
|
|
Author: hook.Sender.UserName,
|
2021-07-04 18:38:59 +00:00
|
|
|
Email: hook.Sender.Email,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
2022-09-07 17:16:40 +00:00
|
|
|
Sender: hook.Sender.UserName,
|
2021-07-04 18:38:59 +00:00
|
|
|
ChangedFiles: getChangedFilesFromPushHook(hook),
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 18:38:59 +00:00
|
|
|
func getChangedFilesFromPushHook(hook *pushHook) []string {
|
2022-01-17 22:46:59 +00:00
|
|
|
// assume a capacity of 4 changed files per commit
|
|
|
|
files := make([]string, 0, len(hook.Commits)*4)
|
2022-01-08 21:16:07 +00:00
|
|
|
for _, c := range hook.Commits {
|
|
|
|
files = append(files, c.Added...)
|
|
|
|
files = append(files, c.Removed...)
|
|
|
|
files = append(files, c.Modified...)
|
2021-07-04 18:38:59 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:41:51 +00:00
|
|
|
files = append(files, hook.HeadCommit.Added...)
|
|
|
|
files = append(files, hook.HeadCommit.Removed...)
|
|
|
|
files = append(files, hook.HeadCommit.Modified...)
|
|
|
|
|
2024-04-09 07:04:53 +00:00
|
|
|
return utils.DeduplicateStrings(files)
|
2021-07-04 18:38:59 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// pipelineFromTag extracts the Pipeline data from a Gitea tag hook.
|
2022-10-18 01:24:12 +00:00
|
|
|
func pipelineFromTag(hook *pushHook) *model.Pipeline {
|
2017-05-01 10:33:06 +00:00
|
|
|
avatar := expandAvatar(
|
2022-09-07 17:16:40 +00:00
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.Sender.AvatarURL),
|
2017-05-01 10:33:06 +00:00
|
|
|
)
|
2024-04-30 07:59:03 +00:00
|
|
|
ref := strings.TrimPrefix(hook.Ref, "refs/tags/")
|
2017-05-01 10:33:06 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return &model.Pipeline{
|
2017-05-01 10:33:06 +00:00
|
|
|
Event: model.EventTag,
|
2017-11-04 15:06:37 +00:00
|
|
|
Commit: hook.Sha,
|
2024-04-30 07:59:03 +00:00
|
|
|
Ref: fmt.Sprintf("refs/tags/%s", ref),
|
|
|
|
ForgeURL: fmt.Sprintf("%s/src/tag/%s", hook.Repo.HTMLURL, ref),
|
|
|
|
Message: fmt.Sprintf("created tag %s", ref),
|
2017-05-01 10:33:06 +00:00
|
|
|
Avatar: avatar,
|
2022-09-07 17:16:40 +00:00
|
|
|
Author: hook.Sender.UserName,
|
|
|
|
Sender: hook.Sender.UserName,
|
2024-02-22 07:02:19 +00:00
|
|
|
Email: hook.Sender.Email,
|
2017-05-01 10:33:06 +00:00
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// pipelineFromPullRequest extracts the Pipeline data from a Gitea pull_request hook.
|
2022-10-18 01:24:12 +00:00
|
|
|
func pipelineFromPullRequest(hook *pullRequestHook) *model.Pipeline {
|
2017-05-01 10:33:06 +00:00
|
|
|
avatar := expandAvatar(
|
2022-09-07 17:16:40 +00:00
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.PullRequest.Poster.AvatarURL),
|
2017-05-01 10:33:06 +00:00
|
|
|
)
|
2023-12-26 18:22:52 +00:00
|
|
|
|
|
|
|
event := model.EventPull
|
|
|
|
if hook.Action == actionClose {
|
|
|
|
event = model.EventPullClosed
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2023-12-26 18:22:52 +00:00
|
|
|
Event: event,
|
2023-11-14 16:12:12 +00:00
|
|
|
Commit: hook.PullRequest.Head.Sha,
|
2024-02-22 07:02:19 +00:00
|
|
|
ForgeURL: hook.PullRequest.HTMLURL,
|
2023-11-14 16:12:12 +00:00
|
|
|
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
|
|
|
|
Branch: hook.PullRequest.Base.Ref,
|
|
|
|
Message: hook.PullRequest.Title,
|
|
|
|
Author: hook.PullRequest.Poster.UserName,
|
|
|
|
Avatar: avatar,
|
|
|
|
Sender: hook.Sender.UserName,
|
2024-02-22 07:02:19 +00:00
|
|
|
Email: hook.Sender.Email,
|
2023-11-14 16:12:12 +00:00
|
|
|
Title: hook.PullRequest.Title,
|
2017-05-01 10:33:06 +00:00
|
|
|
Refspec: fmt.Sprintf("%s:%s",
|
2018-01-29 12:20:02 +00:00
|
|
|
hook.PullRequest.Head.Ref,
|
|
|
|
hook.PullRequest.Base.Ref,
|
2017-05-01 10:33:06 +00:00
|
|
|
),
|
2023-03-17 02:43:04 +00:00
|
|
|
PullRequestLabels: convertLabels(hook.PullRequest.Labels),
|
2024-11-18 13:49:12 +00:00
|
|
|
FromFork: hook.PullRequest.Head.RepoID != hook.PullRequest.Base.RepoID,
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2023-12-26 18:22:52 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 16:39:00 +00:00
|
|
|
func pipelineFromRelease(hook *releaseHook) *model.Pipeline {
|
|
|
|
avatar := expandAvatar(
|
|
|
|
hook.Repo.HTMLURL,
|
|
|
|
fixMalformedAvatar(hook.Sender.AvatarURL),
|
|
|
|
)
|
|
|
|
|
|
|
|
return &model.Pipeline{
|
|
|
|
Event: model.EventRelease,
|
|
|
|
Ref: fmt.Sprintf("refs/tags/%s", hook.Release.TagName),
|
|
|
|
ForgeURL: hook.Release.HTMLURL,
|
|
|
|
Branch: hook.Release.Target,
|
|
|
|
Message: fmt.Sprintf("created release %s", hook.Release.Title),
|
|
|
|
Avatar: avatar,
|
|
|
|
Author: hook.Sender.UserName,
|
|
|
|
Sender: hook.Sender.UserName,
|
2024-02-22 07:02:19 +00:00
|
|
|
Email: hook.Sender.Email,
|
2024-01-30 16:39:00 +00:00
|
|
|
IsPrerelease: hook.Release.IsPrerelease,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// parsePush parses a push hook from a read closer.
|
2017-05-01 10:33:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-30 16:39:00 +00:00
|
|
|
func parseRelease(r io.Reader) (*releaseHook, error) {
|
|
|
|
pr := new(releaseHook)
|
|
|
|
err := json.NewDecoder(r).Decode(pr)
|
|
|
|
return pr, err
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// fixMalformedAvatar fixes an avatar url if malformed (currently a known bug with gitea).
|
2017-05-01 10:33:06 +00: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 {
|
2024-01-10 14:34:44 +00:00
|
|
|
return strings.ReplaceAll(url, "//avatars/", "/avatars/")
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// expandAvatar converts a relative avatar URL to the absolute url.
|
2024-06-04 06:30:54 +00:00
|
|
|
func expandAvatar(repo, rawURL string) string {
|
|
|
|
aURL, err := url.Parse(rawURL)
|
2017-05-01 10:33:06 +00:00
|
|
|
if err != nil {
|
2024-06-04 06:30:54 +00:00
|
|
|
return rawURL
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2024-06-04 06:30:54 +00:00
|
|
|
if aURL.IsAbs() {
|
2017-05-01 10:33:06 +00:00
|
|
|
// Url is already absolute
|
2024-06-04 06:30:54 +00:00
|
|
|
return aURL.String()
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve to base
|
|
|
|
burl, err := url.Parse(repo)
|
|
|
|
if err != nil {
|
2024-06-04 06:30:54 +00:00
|
|
|
return rawURL
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2024-06-04 06:30:54 +00:00
|
|
|
aURL = burl.ResolveReference(aURL)
|
2017-05-01 10:33:06 +00:00
|
|
|
|
2024-06-04 06:30:54 +00:00
|
|
|
return aURL.String()
|
2017-05-01 10:33:06 +00:00
|
|
|
}
|
2021-09-27 06:11:11 +00:00
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// matchingHooks return matching hooks.
|
2024-06-04 06:30:54 +00:00
|
|
|
func matchingHooks(hooks []*gitea.Hook, rawURL string) *gitea.Hook {
|
|
|
|
link, err := url.Parse(rawURL)
|
2021-09-27 06:11:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if val, ok := hook.Config["url"]; ok {
|
2024-06-04 06:30:54 +00:00
|
|
|
hookURL, err := url.Parse(val)
|
|
|
|
if err == nil && hookURL.Host == link.Host {
|
2021-09-27 06:11:11 +00:00
|
|
|
return hook
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-17 02:43:04 +00:00
|
|
|
|
|
|
|
func convertLabels(from []*gitea.Label) []string {
|
|
|
|
labels := make([]string, len(from))
|
|
|
|
for i, label := range from {
|
|
|
|
labels[i] = label.Name
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|