2021-10-03 12:42:47 +00:00
|
|
|
// Copyright 2021 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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 gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/xanzy/go-gitlab"
|
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"
|
2022-01-17 22:46:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mergeRefs = "refs/merge-requests/%d/head" // merge request merged with base
|
2021-10-03 12:42:47 +00:00
|
|
|
)
|
|
|
|
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) convertGitLabRepo(_repo *gitlab.Project) (*model.Repo, error) {
|
2021-12-01 13:22:06 +00:00
|
|
|
parts := strings.Split(_repo.PathWithNamespace, "/")
|
2022-01-05 20:50:23 +00:00
|
|
|
owner := strings.Join(parts[:len(parts)-1], "/")
|
|
|
|
name := parts[len(parts)-1]
|
2021-10-03 12:42:47 +00:00
|
|
|
repo := &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(_repo.ID)),
|
|
|
|
Owner: owner,
|
|
|
|
Name: name,
|
|
|
|
FullName: _repo.PathWithNamespace,
|
|
|
|
Avatar: _repo.AvatarURL,
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: _repo.WebURL,
|
2022-11-15 14:01:23 +00:00
|
|
|
Clone: _repo.HTTPURLToRepo,
|
2023-08-12 15:39:13 +00:00
|
|
|
CloneSSH: _repo.SSHURLToRepo,
|
2022-11-15 14:01:23 +00:00
|
|
|
Branch: _repo.DefaultBranch,
|
2023-04-30 12:01:11 +00:00
|
|
|
Visibility: model.RepoVisibility(_repo.Visibility),
|
2022-11-15 14:01:23 +00:00
|
|
|
IsSCMPrivate: !_repo.Public,
|
2023-03-21 22:01:59 +00:00
|
|
|
Perm: &model.Perm{
|
|
|
|
Pull: isRead(_repo),
|
|
|
|
Push: isWrite(_repo),
|
|
|
|
Admin: isAdmin(_repo),
|
|
|
|
},
|
2023-12-24 09:04:18 +00:00
|
|
|
PREnabled: _repo.MergeRequestsEnabled,
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(repo.Avatar) != 0 && !strings.HasPrefix(repo.Avatar, "http") {
|
2023-05-31 16:30:41 +00:00
|
|
|
repo.Avatar = fmt.Sprintf("%s/%s", g.url, repo.Avatar)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return repo, nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, *model.Repo, *model.Pipeline, error) {
|
2021-10-03 12:42:47 +00:00
|
|
|
repo := &model.Repo{}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
target := hook.ObjectAttributes.Target
|
|
|
|
source := hook.ObjectAttributes.Source
|
|
|
|
obj := hook.ObjectAttributes
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
switch {
|
|
|
|
case target == nil && source == nil:
|
2022-01-17 22:46:59 +00:00
|
|
|
return 0, nil, nil, fmt.Errorf("target and source keys expected in merge request hook")
|
2024-01-10 14:34:44 +00:00
|
|
|
case target == nil:
|
2022-01-17 22:46:59 +00:00
|
|
|
return 0, nil, nil, fmt.Errorf("target key expected in merge request hook")
|
2024-01-10 14:34:44 +00:00
|
|
|
case source == nil:
|
2022-01-17 22:46:59 +00:00
|
|
|
return 0, nil, nil, fmt.Errorf("source key expected in merge request hook")
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if target.PathWithNamespace != "" {
|
|
|
|
var err error
|
|
|
|
if repo.Owner, repo.Name, err = extractFromPath(target.PathWithNamespace); err != nil {
|
2022-01-17 22:46:59 +00:00
|
|
|
return 0, nil, nil, err
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
repo.FullName = target.PathWithNamespace
|
|
|
|
} else {
|
|
|
|
repo.Owner = req.FormValue("owner")
|
|
|
|
repo.Name = req.FormValue("name")
|
|
|
|
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
repo.ForgeRemoteID = model.ForgeRemoteID(fmt.Sprint(obj.TargetProjectID))
|
2023-11-14 16:12:12 +00:00
|
|
|
repo.ForgeURL = target.WebURL
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
if target.GitHTTPURL != "" {
|
|
|
|
repo.Clone = target.GitHTTPURL
|
|
|
|
} else {
|
|
|
|
repo.Clone = target.HTTPURL
|
|
|
|
}
|
2023-08-12 15:39:13 +00:00
|
|
|
if target.GitSSHURL != "" {
|
|
|
|
repo.CloneSSH = target.GitSSHURL
|
|
|
|
} else {
|
|
|
|
repo.CloneSSH = target.SSHURL
|
|
|
|
}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2023-07-30 16:28:52 +00:00
|
|
|
repo.Branch = target.DefaultBranch
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
if target.AvatarURL != "" {
|
|
|
|
repo.Avatar = target.AvatarURL
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventPull
|
2023-12-26 18:22:52 +00:00
|
|
|
if obj.State == "closed" {
|
|
|
|
pipeline.Event = model.EventPullClosed
|
|
|
|
}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
lastCommit := obj.LastCommit
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Message = lastCommit.Message
|
|
|
|
pipeline.Commit = lastCommit.ID
|
2022-11-04 23:35:06 +00:00
|
|
|
pipeline.CloneURL = obj.Source.HTTPURL
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Ref = fmt.Sprintf(mergeRefs, obj.IID)
|
|
|
|
pipeline.Branch = obj.SourceBranch
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
author := lastCommit.Author
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Author = author.Name
|
|
|
|
pipeline.Email = author.Email
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if len(pipeline.Email) != 0 {
|
|
|
|
pipeline.Avatar = getUserAvatar(pipeline.Email)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Title = obj.Title
|
2023-11-14 16:12:12 +00:00
|
|
|
pipeline.ForgeURL = obj.URL
|
2023-03-17 02:43:04 +00:00
|
|
|
pipeline.PullRequestLabels = convertLabels(hook.Labels)
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return obj.IID, repo, pipeline, nil
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func convertPushHook(hook *gitlab.PushEvent) (*model.Repo, *model.Pipeline, error) {
|
2021-10-03 12:42:47 +00:00
|
|
|
repo := &model.Repo{}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if repo.Owner, repo.Name, err = extractFromPath(hook.Project.PathWithNamespace); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
repo.ForgeRemoteID = model.ForgeRemoteID(fmt.Sprint(hook.ProjectID))
|
2021-10-03 12:42:47 +00:00
|
|
|
repo.Avatar = hook.Project.AvatarURL
|
2023-11-14 16:12:12 +00:00
|
|
|
repo.ForgeURL = hook.Project.WebURL
|
2021-10-03 12:42:47 +00:00
|
|
|
repo.Clone = hook.Project.GitHTTPURL
|
2023-08-12 15:39:13 +00:00
|
|
|
repo.CloneSSH = hook.Project.GitSSHURL
|
2021-10-03 12:42:47 +00:00
|
|
|
repo.FullName = hook.Project.PathWithNamespace
|
|
|
|
repo.Branch = hook.Project.DefaultBranch
|
|
|
|
|
|
|
|
switch hook.Project.Visibility {
|
|
|
|
case gitlab.PrivateVisibility:
|
2021-11-22 11:55:13 +00:00
|
|
|
repo.IsSCMPrivate = true
|
2021-10-03 12:42:47 +00:00
|
|
|
case gitlab.InternalVisibility:
|
2021-11-22 11:55:13 +00:00
|
|
|
repo.IsSCMPrivate = true
|
2021-10-03 12:42:47 +00:00
|
|
|
case gitlab.PublicVisibility:
|
2021-11-22 11:55:13 +00:00
|
|
|
repo.IsSCMPrivate = false
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventPush
|
|
|
|
pipeline.Commit = hook.After
|
|
|
|
pipeline.Branch = strings.TrimPrefix(hook.Ref, "refs/heads/")
|
|
|
|
pipeline.Ref = hook.Ref
|
2021-10-03 12:42:47 +00:00
|
|
|
|
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)
|
2021-10-03 12:42:47 +00:00
|
|
|
for _, cm := range hook.Commits {
|
|
|
|
if hook.After == cm.ID {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Author = cm.Author.Name
|
|
|
|
pipeline.Email = cm.Author.Email
|
|
|
|
pipeline.Message = cm.Message
|
|
|
|
pipeline.Timestamp = cm.Timestamp.Unix()
|
|
|
|
if len(pipeline.Email) != 0 {
|
|
|
|
pipeline.Avatar = getUserAvatar(pipeline.Email)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
|
|
|
|
files = append(files, cm.Added...)
|
|
|
|
files = append(files, cm.Removed...)
|
|
|
|
files = append(files, cm.Modified...)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.ChangedFiles = utils.DedupStrings(files)
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return repo, pipeline, nil
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func convertTagHook(hook *gitlab.TagEvent) (*model.Repo, *model.Pipeline, error) {
|
2021-10-03 12:42:47 +00:00
|
|
|
repo := &model.Repo{}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if repo.Owner, repo.Name, err = extractFromPath(hook.Project.PathWithNamespace); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
repo.ForgeRemoteID = model.ForgeRemoteID(fmt.Sprint(hook.ProjectID))
|
2021-10-03 12:42:47 +00:00
|
|
|
repo.Avatar = hook.Project.AvatarURL
|
2023-11-14 16:12:12 +00:00
|
|
|
repo.ForgeURL = hook.Project.WebURL
|
2021-10-03 12:42:47 +00:00
|
|
|
repo.Clone = hook.Project.GitHTTPURL
|
2023-08-12 15:39:13 +00:00
|
|
|
repo.CloneSSH = hook.Project.GitSSHURL
|
2021-10-03 12:42:47 +00:00
|
|
|
repo.FullName = hook.Project.PathWithNamespace
|
|
|
|
repo.Branch = hook.Project.DefaultBranch
|
|
|
|
|
|
|
|
switch hook.Project.Visibility {
|
|
|
|
case gitlab.PrivateVisibility:
|
2021-11-22 11:55:13 +00:00
|
|
|
repo.IsSCMPrivate = true
|
2021-10-03 12:42:47 +00:00
|
|
|
case gitlab.InternalVisibility:
|
2021-11-22 11:55:13 +00:00
|
|
|
repo.IsSCMPrivate = true
|
2021-10-03 12:42:47 +00:00
|
|
|
case gitlab.PublicVisibility:
|
2021-11-22 11:55:13 +00:00
|
|
|
repo.IsSCMPrivate = false
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventTag
|
|
|
|
pipeline.Commit = hook.After
|
|
|
|
pipeline.Branch = strings.TrimPrefix(hook.Ref, "refs/heads/")
|
|
|
|
pipeline.Ref = hook.Ref
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
for _, cm := range hook.Commits {
|
|
|
|
if hook.After == cm.ID {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Author = cm.Author.Name
|
|
|
|
pipeline.Email = cm.Author.Email
|
|
|
|
pipeline.Message = cm.Message
|
|
|
|
pipeline.Timestamp = cm.Timestamp.Unix()
|
|
|
|
if len(pipeline.Email) != 0 {
|
|
|
|
pipeline.Avatar = getUserAvatar(pipeline.Email)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return repo, pipeline, nil
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUserAvatar(email string) string {
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(email))
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s/%v.jpg?s=%s",
|
|
|
|
gravatarBase,
|
|
|
|
hex.EncodeToString(hasher.Sum(nil)),
|
|
|
|
"128",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractFromPath(str string) (string, string, error) {
|
|
|
|
s := strings.Split(str, "/")
|
|
|
|
if len(s) < 2 {
|
2024-01-10 21:56:42 +00:00
|
|
|
return "", "", fmt.Errorf("minimum match not found")
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
return s[0], s[1], nil
|
|
|
|
}
|
2023-03-17 02:43:04 +00:00
|
|
|
|
2023-03-20 23:48:15 +00:00
|
|
|
func convertLabels(from []*gitlab.EventLabel) []string {
|
2023-03-17 02:43:04 +00:00
|
|
|
labels := make([]string, len(from))
|
|
|
|
for i, label := range from {
|
2023-03-20 23:48:15 +00:00
|
|
|
labels[i] = label.Title
|
2023-03-17 02:43:04 +00:00
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|