woodpecker/server
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
..
api Fix panic if forge is unreachable (#3944) 2024-07-20 11:46:51 +02:00
badges Spellcheck "server/*" (#3753) 2024-06-04 08:30:54 +02:00
cache Add godot linter to harmonitze toplevel comments (#3650) 2024-05-13 22:58:21 +02:00
ccmenu Update go module path for major version 2 (#2905) 2023-12-08 08:15:08 +01:00
cron Gracefully shutdown server (#3896) 2024-07-14 01:46:01 +02:00
forge Change Bitbucket PR hook to point the source branch, commit & ref (#3965) 2024-07-23 16:58:38 +02:00
grpc Add timestamp for last work done by agent (#3844) 2024-07-01 10:34:47 -07:00
logging Add godot linter to harmonitze toplevel comments (#3650) 2024-05-13 22:58:21 +02:00
model Fix deploy task env (#3878) 2024-07-07 13:43:07 +02:00
pipeline Remove deprecated pipeline settings (#3916) 2024-07-23 13:06:47 +02:00
pubsub Fix linter (#3354) 2024-02-08 22:49:07 +01:00
queue Gracefully shutdown server (#3896) 2024-07-14 01:46:01 +02:00
router server: remove old unused routes (#3845) 2024-07-22 01:05:05 +02:00
services Migrate to github.com/urfave/cli/v3 (#2951) 2024-07-18 01:26:35 +02:00
store Improve cron list (#3947) 2024-07-20 18:15:38 +02:00
web Add api for forges (#3733) 2024-06-20 13:08:54 +02:00
config.go Use proper oauth state (#3847) 2024-06-27 16:52:09 +02:00