gitness icon indicating copy to clipboard operation
gitness copied to clipboard

Add Pull Request support for Bitbucket

Open lazy404 opened this issue 9 years ago • 26 comments

Bitbucket has support for PR hooks

https://confluence.atlassian.com/display/BITBUCKET/Pull+Request+POST+hook+management;jsessionid=3CF795068ED8DB1530108DC91C83ACB0.node2

lazy404 avatar Oct 19 '14 19:10 lazy404

Are there docs for cloning a pull request?

bradrydzewski avatar Oct 19 '14 19:10 bradrydzewski

So far I have found this

https://confluence.atlassian.com/display/BITBUCKET/Pull+Request+POST+hook+management;jsessionid=3CF795068ED8DB1530108DC91C83ACB0.node2

git remote add pr https://pr_source/pr/pr_repo git fetch pr git merge pr_hash_from_json

also there is also a link to diff

https://bitbucket.org/api/2.0/repositories/lazy404/drone_test/pullrequests/5/diff

I have found this https://bitbucket.org/site/master/issue/5814/reify-pull-requests-by-making-them-a-ref so it's only avaiable by api

lazy404 avatar Oct 19 '14 20:10 lazy404

We had someone investigate this about 6 months ago. The details are a bit fuzzy, but I thought the issue was that you need to clone from the fork and merge. This was problematic for private repositories because they use an SSH key to clone, and that SSH key does not grant authorization to the fork.

Again, this is from memory and that was 6 months ago, so it would need more research.

bradrydzewski avatar Oct 20 '14 04:10 bradrydzewski

One can add drone deploy key manually to the PR source repo.

will the following be ok for checking pr request from [email protected]:other/drone_test.git with PR_COMMIT hash ?

git clone --depth=50 --recursive --branch=master [email protected]:lazy404/drone_test.git git remote add PR [email protected]:other/drone_test.git git fetch PR git merge PR_COMMIT

lazy404 avatar Oct 20 '14 18:10 lazy404

@lazy404 working on pull request support for Bitbucket and GitLab. Would you be willing to weigh-in on the proposed design: #1066

bradrydzewski avatar Jun 19 '15 17:06 bradrydzewski

Is there any progress with this issue?

kushtrimjunuzi avatar Nov 02 '16 18:11 kushtrimjunuzi

I'm not aware of anyone working on this at the moment. The issue has been labeled as help wanted, which means it is not something I'm planning on implementing, but would accept a community contribution.

bradrydzewski avatar Nov 02 '16 18:11 bradrydzewski

I just merged https://github.com/drone/drone/pull/1864 which stores the remote repository for the pull request as well as a refspec value. With these values we can do the following:

# clone base
git clone https://bitbucket.org/atlassianlabs/ac-koa-hipchat
cd ac-koa-hipchat

# clone head from remote repo, refspec version/0.2.20:master
git pull https://bitbucket.org/andrew_sokolov/ac-koa-hipchat version/0.2.20:master

There are still some additional changes that will be required, for which I'm definitely willing to accept pull requests. Note that I'm not planning to work on this, so for those reading, we definitely need your help here.

git clone plugin

We need to alter the git plugin to handle pull requests that don't use merge refs. This would essentially look like the above shell script. Note that I realize it isn't perfect, but it is better than what we have now which is nothing :)

bitbucket hook parser

We need to alter the bitbucket hook parser to enable external pull requests. Right now the bitbucket hook parser only accepts intra-repository pull requests.

bradrydzewski avatar Nov 19 '16 23:11 bradrydzewski

I will try to look into this. Would be great to get this working in order to have a workflow were the pull_request is temporarily merged in the build and then tested. (It could then lead to writing a bitbucket plugin which automatically approves and merges a pull_request, when it succeeds. A step closer to automation for CI ;-) )

patrickjahns avatar Nov 25 '16 21:11 patrickjahns

Hi there. Is there any tips or tricks to make PR build at the moment, before drone support it? Any jenkins integration or call to drone cli... or something? Just wondering if I forget something :)

zaggash avatar Apr 16 '17 21:04 zaggash

Note that this issue is frozen until Bitbucket solves the following issue: https://bitbucket.org/site/master/issues/5814/repository-refs-for-pull-requests

They recently (in 2018) posted that they are working on this issue, and will rollout a series of changes in phases. Once they have a fix in place we can quickly implement in Drone.

bradrydzewski avatar Nov 04 '18 01:11 bradrydzewski

Hi @bradrydzewski https://bitbucket.org/site/master/issues/5814/repository-refs-for-pull-requests has been opened 7 years ago, we may wait 7 more until they implement this functionality. Could you please advise if is somehow possible to trigger the drone build on PR event? We could then just merge develop with the feature branch and run tests on it. Is there any way to do that? Thanks in advance.

dmytro-tolkodubov avatar Apr 08 '19 14:04 dmytro-tolkodubov

Without paying bitbucket customers complaining about it I guess it will never beeing implemented.

tboerger avatar Apr 08 '19 14:04 tboerger

@tboerger Hi Thomas, thanks for your reply. That's indeed very sad they can't implement that. Please could you advise if that is ever possible to trigger the build on PR event? Yes, we can't checkout the PR by ref spec, but at least we could do some other manual steps, ie merge feature branch with develop and run tests on it... Currently drone CI seems to ignore PR events from Bitbucket cloud.

dmytro-tolkodubov avatar Apr 08 '19 14:04 dmytro-tolkodubov

I don't know which webhooks are getting sent by Bitbucket.

tboerger avatar Apr 08 '19 14:04 tboerger

Thomas, they do have this documented here https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html and here https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/hooks

Is it something that could help?

dmytro-tolkodubov avatar Apr 08 '19 14:04 dmytro-tolkodubov

Hooks are not the problem. Without pull request refs we would have to change our clone process, and even with this change there is no guarantee cloning pull requests from private forks would work properly. It is undesirable to change our clone process just for Bitbucket, given that GitHub, GitLab, Gogs, Gitea and even Atlassian Stash all support pull request refs.

bradrydzewski avatar Apr 08 '19 14:04 bradrydzewski

@bradrydzewski Brad, thank you for your reply. That's a fair point regarding the cloning process. But what if you could just enable steps triggering on PR event, event without cloning PR/merge ref it is going to be a huge benefit for BitBucket clould users as we would be able to execute the steps triggered by this event. Is that possible without changing the clone logic?

dmytro-tolkodubov avatar Apr 09 '19 07:04 dmytro-tolkodubov

@dmytro-tolkodubov it is possible, however, the result is that every Bitbucket pull request would fail by default. Drone would be perceived as broken and the resulting support burden would probably decrease my quality of life. I'm not sure I want to take such actions at this time ... although I do appreciate the suggestion.

bradrydzewski avatar Apr 13 '19 20:04 bradrydzewski

Recently I implemented a workaround and it is working for my workflow, so basically for every PR there is a webhook request and cloning the target branch from upstream as usual but there is a step in pipeline were git rebase with PR, this is private repo and private fork. I dont know if this might help. The contributor has to be in drone users list in order to pull and rebase his/her PR.

pipeline:

  dev_git_pull_pr:
    image: plugins/git
    commands:
      - "git pull --rebase --no-tags https://bitbucket.org/${DRONE_COMMIT_AUTHOR}/${DRONE_REPO_NAME} ${DRONE_COMMIT_REFSPEC}"
    when:
      branch: [master]
      event: [pull_request]

kushtrimjunuzi avatar Apr 13 '19 20:04 kushtrimjunuzi

@bradrydzewski, thanks for replying. @kushtrimjunuzi, are you running drone 1.0? I think pull_requst event handling is not supported for BB cloud. Brad stated this as well above. Also DRONE_COMMIT_REFSPEC does not seems to be available in 1.0 release.

dmytro-tolkodubov avatar Apr 14 '19 19:04 dmytro-tolkodubov

@dmytro-tolkodubov I'm running drone 0.8

kushtrimjunuzi avatar Apr 14 '19 19:04 kushtrimjunuzi

I was able to hack and enable this pull_request flow in drone 1, I know its not the best way and while waiting for bitbucket.org to support pull request. Take a look a fork here https://github.com/kushtrimjunuzi/drone and here https://github.com/kushtrimjunuzi/go-scm. Btw in drone 0.8 for some reason pull requests are not parsed correctly, DRONE_COMMIT_AUTHOR is returning empty, I'm guessing this is because of api changes from bitbucket.org, for this reason I was forced to just move to latest drone version and do this hack.

pipeline:
  dev_git_pull_pr:
    image: plugins/git
    commands:
         - "git pull --no-tags https://bitbucket.org/${DRONE_COMMIT_AUTHOR}/${DRONE_REPO_NAME} ${DRONE_SOURCE_BRANCH}:master"
    when:
      branch: [master]
      event: [pull_request]

kushtrimjunuzi avatar Jun 03 '19 19:06 kushtrimjunuzi

Nice one! But we decided to move to GitHub and no regrets :)

dmytro-tolkodubov avatar Jun 04 '19 07:06 dmytro-tolkodubov

@bradrydzewski Everyone who stumble upon this issue would be glad if there is undocumented flag on the yaml to enable pull request Bitbucket trigger, so they don't have to fork the repo just to enable the feature like @kushtrimjunuzi just did. :D Custom clone step is just perfect for this issue, but it won't happen because the pull request itself cannot trigger the pipeline.

puzzloholic avatar Sep 28 '19 14:09 puzzloholic