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-08-22 19:06:56 +00:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2015-08-22 19:06:56 +00:00
|
|
|
"net/http"
|
2021-10-03 12:42:47 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2015-08-22 19:06:56 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
"github.com/franela/goblin"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitlab/testdata"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2015-08-22 19:06:56 +00:00
|
|
|
)
|
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
func load(config string) *GitLab {
|
|
|
|
_url, _ := url.Parse(config)
|
2021-12-01 13:22:06 +00:00
|
|
|
params := _url.Query()
|
|
|
|
_url.RawQuery = ""
|
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
|
|
|
gitlab := GitLab{}
|
2023-05-31 16:30:41 +00:00
|
|
|
gitlab.url = _url.String()
|
2021-10-03 12:42:47 +00:00
|
|
|
gitlab.ClientID = params.Get("client_id")
|
|
|
|
gitlab.ClientSecret = params.Get("client_secret")
|
|
|
|
gitlab.SkipVerify, _ = strconv.ParseBool(params.Get("skip_verify"))
|
|
|
|
gitlab.HideArchives, _ = strconv.ParseBool(params.Get("hide_archives"))
|
|
|
|
|
|
|
|
// this is a temp workaround
|
|
|
|
gitlab.Search, _ = strconv.ParseBool(params.Get("search"))
|
|
|
|
|
|
|
|
return &gitlab
|
|
|
|
}
|
|
|
|
|
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 Test_GitLab(t *testing.T) {
|
2022-01-17 22:46:59 +00:00
|
|
|
// setup a dummy gitlab server
|
2022-01-05 20:50:23 +00:00
|
|
|
server := testdata.NewServer(t)
|
2015-08-22 19:06:56 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2016-04-12 20:22:23 +00:00
|
|
|
env := server.URL + "?client_id=test&client_secret=test"
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
client := load(env)
|
2015-08-22 19:06:56 +00:00
|
|
|
|
2022-01-05 20:50:23 +00:00
|
|
|
user := model.User{
|
2015-08-22 19:06:56 +00:00
|
|
|
Login: "test_user",
|
|
|
|
Token: "e3b0c44298fc1c149afbf4c8996fb",
|
|
|
|
}
|
|
|
|
|
2022-01-05 20:50:23 +00:00
|
|
|
repo := model.Repo{
|
2015-08-22 19:06:56 +00:00
|
|
|
Name: "diaspora-client",
|
|
|
|
Owner: "diaspora",
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
ctx := context.Background()
|
2015-08-22 19:06:56 +00:00
|
|
|
g := goblin.Goblin(t)
|
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
|
|
|
g.Describe("GitLab Plugin", func() {
|
2016-02-16 11:35:20 +00:00
|
|
|
// Test projects method
|
|
|
|
g.Describe("AllProjects", func() {
|
|
|
|
g.It("Should return only non-archived projects is hidden", func() {
|
2021-10-03 12:42:47 +00:00
|
|
|
client.HideArchives = true
|
|
|
|
_projects, err := client.Repos(ctx, &user)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, _projects, 1)
|
2016-02-16 11:35:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should return all the projects", func() {
|
2021-10-03 12:42:47 +00:00
|
|
|
client.HideArchives = false
|
|
|
|
_projects, err := client.Repos(ctx, &user)
|
2016-02-16 11:35:20 +00:00
|
|
|
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
2016-02-16 11:35:20 +00:00
|
|
|
g.Assert(len(_projects)).Equal(2)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-08-22 19:06:56 +00:00
|
|
|
// Test repository method
|
|
|
|
g.Describe("Repo", func() {
|
|
|
|
g.It("Should return valid repo", func() {
|
2022-09-05 15:08:51 +00:00
|
|
|
_repo, err := client.Repo(ctx, &user, "0", "diaspora", "diaspora-client")
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "diaspora-client", _repo.Name)
|
|
|
|
assert.Equal(t, "diaspora", _repo.Owner)
|
2021-11-22 11:55:13 +00:00
|
|
|
assert.True(t, _repo.IsSCMPrivate)
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should return error, when repo not exist", func() {
|
2022-09-05 15:08:51 +00:00
|
|
|
_, err := client.Repo(ctx, &user, "0", "not-existed", "not-existed")
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.Error(t, err)
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test activate method
|
|
|
|
g.Describe("Activate", func() {
|
|
|
|
g.It("Should be success", func() {
|
2023-10-07 14:41:25 +00:00
|
|
|
err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook?access_token=token")
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.NoError(t, err)
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should be failed, when token not given", func() {
|
2023-10-07 14:41:25 +00:00
|
|
|
err := client.Activate(ctx, &user, &repo, "http://example.com/api/hook")
|
2015-08-22 19:06:56 +00:00
|
|
|
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNotNil()
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test deactivate method
|
|
|
|
g.Describe("Deactivate", func() {
|
|
|
|
g.It("Should be success", func() {
|
2023-10-07 14:41:25 +00:00
|
|
|
err := client.Deactivate(ctx, &user, &repo, "http://example.com/api/hook?access_token=token")
|
2015-08-22 19:06:56 +00:00
|
|
|
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test hook method
|
|
|
|
g.Describe("Hook", func() {
|
2016-02-22 20:43:54 +00:00
|
|
|
g.Describe("Push hook", func() {
|
2021-10-08 16:35:56 +00:00
|
|
|
g.It("Should parse actual push hook", func() {
|
2016-02-22 20:43:54 +00:00
|
|
|
req, _ := http.NewRequest(
|
2021-10-03 12:42:47 +00:00
|
|
|
testdata.ServiceHookMethod,
|
|
|
|
testdata.ServiceHookURL.String(),
|
2023-12-26 18:22:52 +00:00
|
|
|
bytes.NewReader(testdata.HookPush),
|
2016-02-22 20:43:54 +00:00
|
|
|
)
|
2021-10-03 12:42:47 +00:00
|
|
|
req.Header = testdata.ServiceHookHeaders
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
hookRepo, pipeline, err := client.Hook(ctx, req)
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-18 01:24:12 +00:00
|
|
|
if assert.NotNil(t, hookRepo) && assert.NotNil(t, pipeline) {
|
|
|
|
assert.Equal(t, pipeline.Event, model.EventPush)
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.Equal(t, "test", hookRepo.Owner)
|
|
|
|
assert.Equal(t, "woodpecker", hookRepo.Name)
|
|
|
|
assert.Equal(t, "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg", hookRepo.Avatar)
|
|
|
|
assert.Equal(t, "develop", hookRepo.Branch)
|
2023-07-31 03:47:23 +00:00
|
|
|
assert.Equal(t, "refs/heads/main", pipeline.Ref)
|
2022-10-18 01:24:12 +00:00
|
|
|
assert.Equal(t, []string{"cmd/cli/main.go"}, pipeline.ChangedFiles)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
2016-02-22 20:43:54 +00:00
|
|
|
})
|
|
|
|
})
|
2015-08-22 19:06:56 +00:00
|
|
|
|
2016-02-22 20:43:54 +00:00
|
|
|
g.Describe("Tag push hook", func() {
|
|
|
|
g.It("Should parse tag push hook", func() {
|
|
|
|
req, _ := http.NewRequest(
|
2021-10-03 12:42:47 +00:00
|
|
|
testdata.ServiceHookMethod,
|
|
|
|
testdata.ServiceHookURL.String(),
|
2023-12-26 18:22:52 +00:00
|
|
|
bytes.NewReader(testdata.HookTag),
|
2016-02-22 20:43:54 +00:00
|
|
|
)
|
2021-10-03 12:42:47 +00:00
|
|
|
req.Header = testdata.ServiceHookHeaders
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
hookRepo, pipeline, err := client.Hook(ctx, req)
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-18 01:24:12 +00:00
|
|
|
if assert.NotNil(t, hookRepo) && assert.NotNil(t, pipeline) {
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.Equal(t, "test", hookRepo.Owner)
|
|
|
|
assert.Equal(t, "woodpecker", hookRepo.Name)
|
|
|
|
assert.Equal(t, "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg", hookRepo.Avatar)
|
|
|
|
assert.Equal(t, "develop", hookRepo.Branch)
|
2022-10-18 01:24:12 +00:00
|
|
|
assert.Equal(t, "refs/tags/v22", pipeline.Ref)
|
|
|
|
assert.Len(t, pipeline.ChangedFiles, 0)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
2016-02-22 20:43:54 +00:00
|
|
|
})
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
|
2016-02-28 17:11:50 +00:00
|
|
|
g.Describe("Merge request hook", func() {
|
|
|
|
g.It("Should parse merge request hook", func() {
|
|
|
|
req, _ := http.NewRequest(
|
2021-10-03 12:42:47 +00:00
|
|
|
testdata.ServiceHookMethod,
|
|
|
|
testdata.ServiceHookURL.String(),
|
2023-12-26 18:22:52 +00:00
|
|
|
bytes.NewReader(testdata.HookPullRequest),
|
2016-02-28 17:11:50 +00:00
|
|
|
)
|
2021-10-03 12:42:47 +00:00
|
|
|
req.Header = testdata.ServiceHookHeaders
|
|
|
|
|
2022-01-17 22:46:59 +00:00
|
|
|
// TODO: insert fake store into context to retrieve user & repo, this will activate fetching of ChangedFiles
|
2022-10-18 01:24:12 +00:00
|
|
|
hookRepo, pipeline, err := client.Hook(ctx, req)
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-18 01:24:12 +00:00
|
|
|
if assert.NotNil(t, hookRepo) && assert.NotNil(t, pipeline) {
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.Equal(t, "http://example.com/uploads/project/avatar/555/Outh-20-Logo.jpg", hookRepo.Avatar)
|
2022-01-17 22:46:59 +00:00
|
|
|
assert.Equal(t, "main", hookRepo.Branch)
|
|
|
|
assert.Equal(t, "anbraten", hookRepo.Owner)
|
2021-10-03 12:42:47 +00:00
|
|
|
assert.Equal(t, "woodpecker", hookRepo.Name)
|
2022-10-18 01:24:12 +00:00
|
|
|
assert.Equal(t, "Update client.go 🎉", pipeline.Title)
|
|
|
|
assert.Len(t, pipeline.ChangedFiles, 0) // see L217
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
2016-02-28 17:11:50 +00:00
|
|
|
})
|
2023-12-26 18:22:52 +00:00
|
|
|
|
|
|
|
g.It("Should parse merge request hook when MR closed", func() {
|
|
|
|
req, _ := http.NewRequest(
|
|
|
|
testdata.ServiceHookMethod,
|
|
|
|
testdata.ServiceHookURL.String(),
|
|
|
|
bytes.NewReader(testdata.HookPullRequestClosed),
|
|
|
|
)
|
|
|
|
req.Header = testdata.ServiceHookHeaders
|
|
|
|
|
|
|
|
// TODO: insert fake store into context to retrieve user & repo, this will activate fetching of ChangedFiles
|
|
|
|
hookRepo, pipeline, err := client.Hook(ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NotNil(t, hookRepo) && assert.NotNil(t, pipeline) {
|
|
|
|
assert.Equal(t, "main", hookRepo.Branch)
|
|
|
|
assert.Equal(t, "anbraten", hookRepo.Owner)
|
|
|
|
assert.Equal(t, "woodpecker-test", hookRepo.Name)
|
|
|
|
assert.Equal(t, "Add new file", pipeline.Title)
|
|
|
|
assert.Len(t, pipeline.ChangedFiles, 0) // see L217
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should parse merge request hook when merged", func() {
|
|
|
|
req, _ := http.NewRequest(
|
|
|
|
testdata.ServiceHookMethod,
|
|
|
|
testdata.ServiceHookURL.String(),
|
|
|
|
bytes.NewReader(testdata.HookPullRequestMerged),
|
|
|
|
)
|
|
|
|
req.Header = testdata.ServiceHookHeaders
|
|
|
|
|
|
|
|
// TODO: insert fake store into context to retrieve user & repo, this will activate fetching of ChangedFiles
|
|
|
|
hookRepo, pipeline, err := client.Hook(ctx, req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NotNil(t, hookRepo) && assert.NotNil(t, pipeline) {
|
|
|
|
assert.Equal(t, "main", hookRepo.Branch)
|
|
|
|
assert.Equal(t, "anbraten", hookRepo.Owner)
|
|
|
|
assert.Equal(t, "woodpecker-test", hookRepo.Name)
|
|
|
|
assert.Equal(t, "Add new file", pipeline.Title)
|
|
|
|
assert.Len(t, pipeline.ChangedFiles, 0) // see L217
|
|
|
|
}
|
|
|
|
})
|
2015-08-22 19:06:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|