mohamicorp / stash-jenkins-postreceive-webhook

Webhook used to notify Jenkins when commits are made to Stash
Other
138 stars 98 forks source link

Forked workflow support: send the internal pull request branch name #142

Open martinda opened 9 years ago

martinda commented 9 years ago

In a forked workflow, there is usually one upstream repository, and multiple forks. To simplify administration, this plugin is installed only on the upstream repository. So the repository configured in this plugin is that of the upstream. But when this plugin sends the branch name, it uses the branch name in the fork. When the Git Plugin in Jenkins tries to find the fork branch name in the upstream repository, it fails and the build does not happen.

This plugin needs to send the internal Stash pull request branch name, not the name of the branch being pulled from the fork.

Would you welcome a pull request for this feature, or are you staying away from using Git Stash internal names?

Details

When the notifyCommit?url=SCM_URL&branches=BRANCH_NAME notification hits Jenkins, the Jenkins Git Plugin first matches the SCM_URL against the list of Git Plugin repositories. If there is a match, it prefixes the BRANCH_NAME with the matching repository name alias (e.g. upstream, origin, etc.), and it proceeds to match the branch name against the list of "branches to build" that are configured by the Git Plugin. If a match is found, Jenkins proceeds to polling the git repository at the SCM_URL to find the branch. And here is the issue: the branch name is the name of the branch in the forked repository, and this branch does not exist in the central repo. So Jenkins does not find it, and the build never takes place.

In other words, if the Jenkins Git Plugin is configured like this: Repository: url_to_upstream Name: origin (or empty) Branches to build: origin/feature/*

And the notifyCommit is like this: notifyCommit?url=url_to_upstream&branches=feature/f1

Then Jenkins will attempt to find origin/feature/f1 in url_to_upstream and it will fail.

martinda commented 9 years ago

A preliminary experiment with local modifications shows that a few changes produce the expected outcome. You can see the code in the pull request. Obviously this is not mergeable, I have to make changes to maintain backward compatibility, and write some tests.

Jenkins Git Plugin configuration Repository: ssh://git@localhost:7999/project/hello-world.git Name: upstream Refspec: +refs/pull-requests/*:refs/remotes/upstream/pull-requests/* Branches to build: upstream/pull-requests/*/from

Stash Webhook to Jenkins configuration Repo Clone URL: ssh://git@localhost:7999/project/hello-world.git Skip SSL: checked Omit SHA1: checked Omit branch: not checked

Create a pull request, click the Trigger Build button.

The HTTP GET to Jenkins: GET /git/notifyCommit?url=ssh://git@localhost:7999/project/hello-world.git&branches=pull-requests/7/from

Jenkins builds this PR.

shinstudio commented 9 years ago

I had the same issue and was able to resolve it by adding this

+refs/pull-requests/*:refs/remotes/origin/* +refs/heads/*:refs/remotes/origin/*

to refspec field in Git SCM.

And then leave branches field to \ for the refspec to take over which branch to pick.