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.
|
|
|
|
|
2016-05-03 20:01:16 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
2022-01-17 22:46:59 +00:00
|
|
|
"sort"
|
2024-01-30 16:39:00 +00:00
|
|
|
"strings"
|
2016-05-03 20:01:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-09-14 12:21:16 +00:00
|
|
|
"github.com/franela/goblin"
|
2023-07-14 00:03:54 +00:00
|
|
|
"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/github/fixtures"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2016-05-03 20:01:16 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 22:46:59 +00:00
|
|
|
const (
|
2024-01-30 16:39:00 +00:00
|
|
|
hookEvent = "X-GitHub-Event"
|
|
|
|
hookDeploy = "deployment"
|
|
|
|
hookPush = "push"
|
|
|
|
hookPull = "pull_request"
|
|
|
|
hookRelease = "release"
|
2022-01-17 22:46:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testHookRequest(payload []byte, event string) *http.Request {
|
|
|
|
buf := bytes.NewBuffer(payload)
|
2024-06-06 09:16:45 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
|
2022-01-17 22:46:59 +00:00
|
|
|
req.Header = http.Header{}
|
|
|
|
req.Header.Set(hookEvent, event)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2016-05-03 20:01:16 +00:00
|
|
|
func Test_parser(t *testing.T) {
|
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("GitHub parser", func() {
|
|
|
|
g.It("should ignore unsupported hook events", func() {
|
2022-01-17 22:46:59 +00:00
|
|
|
req := testHookRequest([]byte(fixtures.HookPullRequest), "issues")
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(r).IsNil()
|
|
|
|
g.Assert(b).IsNil()
|
2022-01-17 22:46:59 +00:00
|
|
|
g.Assert(p).IsNil()
|
2023-07-14 00:03:54 +00:00
|
|
|
assert.ErrorIs(t, err, &types.ErrIgnoreEvent{})
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
g.Describe("given a push hook", func() {
|
|
|
|
g.It("should skip when action is deleted", func() {
|
2022-01-17 22:46:59 +00:00
|
|
|
req := testHookRequest([]byte(fixtures.HookPushDeleted), hookPush)
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(r).IsNil()
|
|
|
|
g.Assert(b).IsNil()
|
|
|
|
g.Assert(err).IsNil()
|
2022-01-17 22:46:59 +00:00
|
|
|
g.Assert(p).IsNil()
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
2022-10-18 01:24:12 +00:00
|
|
|
g.It("should extract repository and pipeline details", func() {
|
2022-01-17 22:46:59 +00:00
|
|
|
req := testHookRequest([]byte(fixtures.HookPush), hookPush)
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
2022-01-17 22:46:59 +00:00
|
|
|
g.Assert(p).IsNil()
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(r).IsNotNil()
|
|
|
|
g.Assert(b).IsNotNil()
|
2016-05-03 20:01:16 +00:00
|
|
|
g.Assert(b.Event).Equal(model.EventPush)
|
2022-01-17 22:46:59 +00:00
|
|
|
sort.Strings(b.ChangedFiles)
|
|
|
|
g.Assert(b.ChangedFiles).Equal([]string{"pipeline/shared/replace_secrets.go", "pipeline/shared/replace_secrets_test.go"})
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Describe("given a pull request hook", func() {
|
2023-12-26 18:22:52 +00:00
|
|
|
g.It("should handle a PR hook when PR got opened or pushed to", func() {
|
|
|
|
req := testHookRequest([]byte(fixtures.HookPullRequest), hookPull)
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
2023-12-26 18:22:52 +00:00
|
|
|
g.Assert(r).IsNotNil()
|
|
|
|
g.Assert(b).IsNotNil()
|
|
|
|
g.Assert(p).IsNotNil()
|
|
|
|
g.Assert(b.Event).Equal(model.EventPull)
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
2023-12-26 18:22:52 +00:00
|
|
|
g.It("should handle a PR closed hook when PR got closed", func() {
|
|
|
|
req := testHookRequest([]byte(fixtures.HookPullRequestClosed), hookPull)
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
2023-12-26 18:22:52 +00:00
|
|
|
g.Assert(r).IsNotNil()
|
|
|
|
g.Assert(b).IsNotNil()
|
|
|
|
g.Assert(p).IsNotNil()
|
|
|
|
g.Assert(b.Event).Equal(model.EventPullClosed)
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
2023-12-26 18:22:52 +00:00
|
|
|
g.It("should handle a PR closed hook when PR got merged", func() {
|
|
|
|
req := testHookRequest([]byte(fixtures.HookPullRequestMerged), hookPull)
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
g.Assert(r).IsNotNil()
|
|
|
|
g.Assert(b).IsNotNil()
|
2022-01-17 22:46:59 +00:00
|
|
|
g.Assert(p).IsNotNil()
|
2023-12-26 18:22:52 +00:00
|
|
|
g.Assert(b.Event).Equal(model.EventPullClosed)
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Describe("given a deployment hook", func() {
|
2022-10-18 01:24:12 +00:00
|
|
|
g.It("should extract repository and pipeline details", func() {
|
2022-01-17 22:46:59 +00:00
|
|
|
req := testHookRequest([]byte(fixtures.HookDeploy), hookDeploy)
|
2022-01-31 14:38:00 +00:00
|
|
|
p, r, b, err := parseHook(req, false)
|
2021-11-04 13:42:25 +00:00
|
|
|
g.Assert(err).IsNil()
|
|
|
|
g.Assert(r).IsNotNil()
|
|
|
|
g.Assert(b).IsNotNil()
|
2022-01-17 22:46:59 +00:00
|
|
|
g.Assert(p).IsNil()
|
2016-05-03 20:01:16 +00:00
|
|
|
g.Assert(b.Event).Equal(model.EventDeploy)
|
2024-07-07 11:43:07 +00:00
|
|
|
g.Assert(b.DeployTo).Equal("production")
|
2024-05-02 16:56:19 +00:00
|
|
|
g.Assert(b.DeployTask).Equal("deploy")
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
|
|
|
})
|
2024-01-30 16:39:00 +00:00
|
|
|
|
|
|
|
g.Describe("given a release hook", func() {
|
|
|
|
g.It("should extract repository and build details", func() {
|
|
|
|
req := testHookRequest([]byte(fixtures.HookRelease), hookRelease)
|
|
|
|
p, r, b, err := parseHook(req, false)
|
|
|
|
g.Assert(err).IsNil()
|
|
|
|
g.Assert(r).IsNotNil()
|
|
|
|
g.Assert(b).IsNotNil()
|
|
|
|
g.Assert(p).IsNil()
|
|
|
|
g.Assert(b.Event).Equal(model.EventRelease)
|
|
|
|
g.Assert(len(strings.Split(b.Ref, "/")) == 3).IsTrue()
|
|
|
|
g.Assert(strings.HasPrefix(b.Ref, "refs/tags/")).IsTrue()
|
|
|
|
})
|
|
|
|
})
|
2016-05-03 20:01:16 +00:00
|
|
|
})
|
|
|
|
}
|