woodpecker/server/forge/bitbucket/parse_test.go
Joan Flotats 047eb19d42
Change Bitbucket PR hook to point the source branch, commit & ref (#3965)
## Description

This is the first fix for: https://github.com/woodpecker-ci/woodpecker/issues/3932

Change the Pull Request hook parser to return the source commit, branch, and ref instead of the destination. Right now, the workflow pulls the destination configuration and code. It should pull the source configuration and code to verify that the configuration and code work as expected before merging the changes.

In case of the close event, the hook parser returns the destination branch, ref and merge commit. Usually, the contributor automatically deletes the source branch after merging the changes to the destination branch. Using the source values will cause the workflow to fail.

After the changes, Woodpecker will correctly download the workflow from the source branch (Pull Request commit), but it will fail to clone the repository. This issue is related to the commit format returned by the Bitbucket webhook. This inconsistency has already been reported: https://jira.atlassian.com/browse/BCLOUD-21201. The webhook returns a short SHA. The problem is that the `git fetch` command requires the full SHA. 

A workaround for this issue is to use the ref to fetch the code:

```yaml
clone:
  git:
    image: woodpeckerci/plugin-git
    settings:
      ref: ${CI_COMMIT_REF}
```

This is not ideal, because the Pull Request head won't always match the workflow commit, but it solves 80% of the event use cases (e.g. trigger a pull request workflow on change). This workaround won't work when re-running a previous workflow pointing to another commit, it will pull the last commit, not the previous one.

## Solutions

The solution proposed by the community is to retrieve the full SHA from the Bitbucket API using the short one. This solution has drawbacks:
- The Bitbucket API rate limit is 1000 req/h. This solution will reduce the maximum number of workflow runs per hour.
- It requires a braking change in the forges interface because the ´Hook(...)´ method does not have an instance of the HTTP Client. 

We propose to allow the git plugin to fetch the source code from a URL. The Bitbucket returns a link pointing to the commit. 

This proposal only requires a small change to the git plugin:
- Add a new optional parameter (e.g. CommitLink)
- Add a clause to the following conditional: 7ac9615f40/plugin.go (L79C1-L88C3)
```go
if p.Pipeline.CommitLink != "" {...}
```
Git commands:
```shell
$ git fetch --no-tags --depth=1 --filter=tree:0 https://bitbucket.org/workspace/repo/commits/692972aabfec
$ git reset --hard -q 692972aabfec # It works with the short SHA
```
Woodpecker will set CommitLink to a blank string for the other forges, but Bitbuckket will use the one returned by the webhook.
2024-07-23 16:58:38 +02:00

139 lines
4.5 KiB
Go

// Copyright 2018 Drone.IO Inc.
//
// 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 bitbucket
import (
"bytes"
"net/http"
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/bitbucket/fixtures"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)
func Test_parser(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Bitbucket parser", func() {
g.It("should ignore unsupported hook", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, "issue:created")
r, b, err := parseHook(req)
g.Assert(r).IsNil()
g.Assert(b).IsNil()
assert.ErrorIs(t, err, &types.ErrIgnoreEvent{})
})
g.Describe("Given a pull-request hook payload", func() {
g.It("should return err when malformed", func() {
buf := bytes.NewBufferString("[]")
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullCreated)
_, _, err := parseHook(req)
g.Assert(err).IsNotNil()
})
g.It("should return pull-request details", func() {
buf := bytes.NewBufferString(fixtures.HookPull)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullCreated)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r.FullName).Equal("user_name/repo_name")
g.Assert(b.Event).Equal(model.EventPull)
g.Assert(b.Commit).Equal("d3022fc0ca3d")
})
g.It("should return pull-request details for a pull-request merged payload", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequestMerged)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullMerged)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r.FullName).Equal("anbraten/test-2")
g.Assert(b.Event).Equal(model.EventPullClosed)
g.Assert(b.Commit).Equal("006704dbeab2")
})
g.It("should return pull-request details for a pull-request closed payload", func() {
buf := bytes.NewBufferString(fixtures.HookPullRequestDeclined)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPullDeclined)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r.FullName).Equal("anbraten/test-2")
g.Assert(b.Event).Equal(model.EventPullClosed)
g.Assert(b.Commit).Equal("f90e18fc9d45")
})
})
g.Describe("Given a push hook payload", func() {
g.It("should return err when malformed", func() {
buf := bytes.NewBufferString("[]")
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
_, _, err := parseHook(req)
g.Assert(err).IsNotNil()
})
g.It("should return nil if missing commit sha", func() {
buf := bytes.NewBufferString(fixtures.HookPushEmptyHash)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
r, b, err := parseHook(req)
g.Assert(r).IsNil()
g.Assert(b).IsNil()
g.Assert(err).IsNil()
})
g.It("should return push details", func() {
buf := bytes.NewBufferString(fixtures.HookPush)
req, _ := http.NewRequest(http.MethodPost, "/hook", buf)
req.Header = http.Header{}
req.Header.Set(hookEvent, hookPush)
r, b, err := parseHook(req)
g.Assert(err).IsNil()
g.Assert(r.FullName).Equal("martinherren1984/publictestrepo")
g.Assert(r.SCMKind).Equal(model.RepoGit)
g.Assert(r.Clone).Equal("https://bitbucket.org/martinherren1984/publictestrepo")
g.Assert(b.Commit).Equal("c14c1bb05dfb1fdcdf06b31485fff61b0ea44277")
g.Assert(b.Message).Equal("a\n")
})
})
g.Describe("Given a tag hook payload", func() {
// TODO
})
})
}