[JIRA] [bitbucket-branch-source-plugin] (JENKINS-33507) Bitbucket Server webhooks and Bitbucket Cloud build status notifications

221 views
Skip to first unread message

amuniz@cloudbees.com (JIRA)

unread,
Mar 13, 2016, 7:49:01 AM3/13/16
to jenkinsc...@googlegroups.com
Antonio Muñiz created an issue
 
Jenkins / New Feature JENKINS-33507
Bitbucket Server webhooks and Bitbucket Cloud build status notifications
Issue Type: New Feature New Feature
Assignee: Antonio Muñiz
Components: bitbucket-branch-source-plugin
Created: 13/Mar/16 11:48 AM
Priority: Major Major
Reporter: Antonio Muñiz

Auto-register webhooks for Bitbucket Server and build status notifications for Bitbucket Cloud (only PR comments are sent currently).

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v6.4.2#64017-sha1:e244265)
Atlassian logo

nickbroon@gmail.com (JIRA)

unread,
May 9, 2016, 1:14:01 PM5/9/16
to jenkinsc...@googlegroups.com
Nicholas Brown commented on New Feature JENKINS-33507
 
Re: Bitbucket Server webhooks and Bitbucket Cloud build status notifications

I'm guessing that Bitbucket server auto-register webhook support is not yet implemented, despite the option being presented in the Branch source configuration?

https://github.com/jenkinsci/bitbucket-branch-source-plugin/blob/f50ce24f4afc75e6dae00c44ea20b8fa137bd72d/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookAutoRegisterListener.java#L187

If I enable this option when configuring Jenkins with my Bitbucket Server details and visit http:// [JENKINS_ROOT_URL]/bitbucket-scmsource-hook/notify I get the below error in my browser.

Until this implemented, could the help-autoRegisterHooks.jelly perhaps be updated to point out BitBucket Server is not supported.
I think this means that automatic branch re-indexing of branches will also not happen as this based off SCM change notification, and thus, 'Periodically if not otherwise run' Build Trigger will need to configured so that this happens.

java.lang.Exception: X-Event-Key HTTP header not found
	at org.kohsuke.stapler.HttpResponses.error(HttpResponses.java:83)
	at com.cloudbees.jenkins.plugins.bitbucket.hooks.BitbucketSCMSourcePushHookReceiver.doNotify(BitbucketSCMSourcePushHookReceiver.java:68)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)

amuniz@cloudbees.com (JIRA)

unread,
May 9, 2016, 2:04:03 PM5/9/16
to jenkinsc...@googlegroups.com

Yes, you are right. Changes need to be made where you pointed and here.

And yes, the help tip could be updated. Feel free to send a PR for that if you want.

bmiddleton@sdl.com (JIRA)

unread,
May 21, 2016, 12:34:03 PM5/21/16
to jenkinsc...@googlegroups.com

Is there any scope to modify the hook implemention to use https://github.com/Nerdwin15/stash-jenkins-postreceive-webhook - which works with the default Git plugin?

bmiddleton@sdl.com (JIRA)

unread,
May 21, 2016, 12:34:03 PM5/21/16
to jenkinsc...@googlegroups.com
Ben Middleton edited a comment on New Feature JENKINS-33507
Is there any scope to modify the hook  implemention  implementation  to use https://github.com/Nerdwin15/stash-jenkins-postreceive-webhook - which works with the default Git plugin?

konrad_w@gmx.de (JIRA)

unread,
May 24, 2016, 2:53:02 AM5/24/16
to jenkinsc...@googlegroups.com
Konrad Windszus edited a comment on New Feature JENKINS-33507
As far as I understand, Bitbucket Server has no API to configure generic web hooks (in contrast to Bitbucket Cloud). You can just enable an already installed web hook plugin remotely through https://developer.atlassian.com/static/rest/bitbucket-server/4.6.1/bitbucket-rest.html#idp2137840. Is the plan here to also create a dedicated web hook Bitbucket plugin or to leverage an existing one?

Maybe https://confluence.atlassian.com/bitbucketserver/post-service-webhook-for-bitbucket-server-776640367.html could be leveraged but it doesn't seem to provide an API to set that up automatically.

amuniz@cloudbees.com (JIRA)

unread,
May 24, 2016, 4:03:00 AM5/24/16
to jenkinsc...@googlegroups.com

The idea is to investigate if auto-registration is possible. If not just add the listener for the hook event (which does not exist currently, only Bitbucket Cloud hooks are attended).

konrad_w@gmx.de (JIRA)

unread,
May 24, 2016, 4:11:02 AM5/24/16
to jenkinsc...@googlegroups.com

I don't see a possibility for auto-registration currently.

If not just add the listener for the hook event (which does not exist currently, only Bitbucket Cloud hooks are attended).

That option is not clear enough to me. Which hook exactly should be used within Bitbucket Cloud to trigger the listener?

konrad_w@gmx.de (JIRA)

unread,
May 24, 2016, 4:11:02 AM5/24/16
to jenkinsc...@googlegroups.com
Konrad Windszus edited a comment on New Feature JENKINS-33507
I don't see a possibility for auto-registration currently. 
bq. If not just add the listener for the hook event (which does not exist currently, only Bitbucket Cloud hooks are attended).
That option is not clear enough to me. Which hook exactly should be used within Bitbucket
 Cloud  Server  to trigger the listener?

amuniz@cloudbees.com (JIRA)

unread,
May 24, 2016, 4:26:03 AM5/24/16
to jenkinsc...@googlegroups.com

Which hook exactly should be used within Bitbucket Server to trigger the listener?

TBD. But probably this one https://marketplace.atlassian.com/plugins/com.nerdwin15.stash-stash-webhook-jenkins/support once proved that it work as announced.

konrad_w@gmx.de (JIRA)

unread,
May 24, 2016, 6:03:02 AM5/24/16
to jenkinsc...@googlegroups.com

konrad_w@gmx.de (JIRA)

unread,
May 25, 2016, 2:02:32 PM5/25/16
to jenkinsc...@googlegroups.com

As far as I understand, Bitbucket Server has no API to configure generic web hooks (in contrast to Bitbucket Cloud). You can just enable an already installed web hook plugin remotely through https://developer.atlassian.com/static/rest/bitbucket-server/4.6.1/bitbucket-rest.html#idp2137840. Is the plan here to also create a dedicated web hook Bitbucket plugin or to leverage an existing one?

Add Comment Add Comment
 

mkobit@gmail.com (JIRA)

unread,
Jun 2, 2016, 2:05:01 PM6/2/16
to jenkinsc...@googlegroups.com

The plugin that Antonio linked is the one we use, so it would be awesome if that was the supported one.

I have a feeling that Bitbucket Server is not going to have generic hooks anytime soon, as those have been open for multiple years.

mkobit@gmail.com (JIRA)

unread,
Jun 9, 2016, 5:05:01 PM6/9/16
to jenkinsc...@googlegroups.com
Mike Kobit updated an issue
 
Change By: Mike Kobit
Auto xAuto -register webhooks for Bitbucket Server and build status notifications for Bitbucket Cloud (only PR comments are sent currently).

amuniz@cloudbees.com (JIRA)

unread,
Jun 10, 2016, 5:00:03 AM6/10/16
to jenkinsc...@googlegroups.com
Antonio Muñiz edited a comment on New Feature JENKINS-33507
 
Re: Bitbucket Server webhooks and Bitbucket Cloud build status notifications
bq. Which hook exactly should be used within Bitbucket Server to trigger the listener?

TBD. But probably this one https://marketplace.atlassian.com/plugins/com.nerdwin15.stash-stash-webhook-jenkins/support once proved that it
 work  works  as announced.

amuniz@cloudbees.com (JIRA)

unread,
Jun 10, 2016, 5:01:02 AM6/10/16
to jenkinsc...@googlegroups.com
Antonio Muñiz updated an issue
 
Change By: Antonio Muñiz
xAuto Auto -register webhooks for Bitbucket Server and build status notifications for Bitbucket Cloud (only PR comments are sent currently).

dodoentertainment@gmail.com (JIRA)

unread,
Jun 13, 2016, 3:51:03 AM6/13/16
to jenkinsc...@googlegroups.com
Nenad Miksa commented on New Feature JENKINS-33507
 
Re: Bitbucket Server webhooks and Bitbucket Cloud build status notifications

Well, if webhook autoregistering is problematic, build status API implementation would be enough for us at first.

mkobit@gmail.com (JIRA)

unread,
Jun 13, 2016, 10:54:09 AM6/13/16
to jenkinsc...@googlegroups.com

I filed an issue to the previously mentioned Bitbucket Server hook [https://github.com/Nerdwin15/stash-jenkins-postreceive-webhook/issues/171|here]. Unfortunately, there doesn't appear to be a large amount of activity on this repository (last commit in January).

It looks like the notification URL is hardcoded to a specific URL (I'm guessing from the Git plugin, but I'm not familiar enough with Jenkins internals):

From https://github.com/Nerdwin15/stash-jenkins-postreceive-webhook/blob/master/src/main/java/com/nerdwin15/stash/webhook/Notifier.java#L94

private static final String BASE_URL = "%s/git/notifyCommit?url=%s";

.

For us, we want to use the Bitbucket Branch Source Plugin for building branches and pull requests. Right now we are using Multibranch and we have to do build notifications ourselves as well as we have to do additional jobs to support pull request events.

Auto-registration would be awesome (mostly what this issue is for), but I first need the Bitbucket Server notifications to trigger Jenkins branch and PR generation. How have others done this? Is there a better plugin than the previously mentioned one for handling these events?

bmiddleton@sdl.com (JIRA)

unread,
Jun 13, 2016, 10:59:05 AM6/13/16
to jenkinsc...@googlegroups.com

I'm not sure there are any other options right now. Unfortunately this is a show-stopper for us, which is preventing us moving to Jenkins Pipelines.

amuniz@cloudbees.com (JIRA)

unread,
Jun 13, 2016, 11:11:02 AM6/13/16
to jenkinsc...@googlegroups.com

private static final String BASE_URL = "%s/git/notifyCommit?url=%s";

Oh, that looks like a blocker. Bitbucket Branch Source uses another URL.

dodoentertainment@gmail.com (JIRA)

unread,
Jun 13, 2016, 11:36:01 AM6/13/16
to jenkinsc...@googlegroups.com

We actually have solved this way: we created a webhook on BitBucket which every time there is a push to repository or pull request is created or updated, it calls this URL: http://jenkins.server/job/MyPipelineJob/build

MyPipelineJob is multibranch pipeline job which only enumerates changes in pull requests and feature branches and triggers builds if necessary. However, we are missing Build status badges from BitBucket BuildStatus API.

mark.ottaviani@ssa.gov (JIRA)

unread,
Jun 13, 2016, 2:18:02 PM6/13/16
to jenkinsc...@googlegroups.com

dodoentertainment@gmail.com (JIRA)

unread,
Jun 19, 2016, 3:28:02 PM6/19/16
to jenkinsc...@googlegroups.com

For BitBucket status API notifications, we have the following workaround:


def obtainBitbucketToken() {
    try {
        echo "Obtaining BitBucket Access Token"
        sh "curl -s -X POST https://bitbucket.org/site/oauth2/access_token -u \"${getBitbucketOAuthKey()}:${getBitbucketOAuthSecret()}\" -d grant_type=client_credentials | jsawk 'return this.access_token' | tr -d \"\\n\" > accessToken.txt"

        def accessToken = readFile 'accessToken.txt'

        echo "BitBucket Token request response: ${accessToken}"

        return accessToken
    } catch (error) {
        echo "Failed to obtain BitBucket access token!"
        return null
    }
}

def notifyCommit(String accessToken, String commitHash, String repository, String buildKey, boolean inProgress, boolean runTests) {
    echo "Notifying commit"
    if (accessToken == null) {
        echo "Failed to notify commit with null access token!"
        return
    }
    try {
        def state = null
        def description = null
        if (inProgress) {
            state = 'INPROGRESS'
            description = "Build in progress. Tests will run: ${runTests}"
        } else {
            if (currentBuild.result == 'SUCCESS') {
                state = 'SUCCESSFUL'
                if (runTests) {
                    description = 'Build has succeeded and all tests have passed!'
                } else {
                    description = 'Build has succeeded but tests were not run!'
                }
            } else {
                state = 'FAILED'
                if (currentBuild.result == 'UNSTABLE') {
                    description = 'Build has succeeded, but some tests have failed!'
                } else {
                    description = "Build has failed. Jenkins result is: ${currentBuild.result}"
                }
            }
        }
        def payload = "{\"state\": \"${state}\", \"key\": \"Jenkins ${buildKey}\", \"name\": \"Jenkins ${buildKey}\", \"url\": \"${env.BUILD_URL}\", \"description\": \"${description}\"}"
        sh "curl -s -X POST https://api.bitbucket.org/2.0/repositories/microblink/${repository}/commit/${commitHash}/statuses/build -H \"Content-Type: application/json\" -H \"Authorization: Bearer ${accessToken}\" -d '${payload}'"
    } catch(error) {
        echo "There has been an error with notifying BitBucket!. Error: ${error}"
    }
}

So, at the beginning of my pipeline script, after checkout and resolving of git commit, I call it this way:

def accessToken = obtainBitbucketToken()
notifyCommit(accessToken, commitHash, repository, keyPullRequestBuild, true, runTests) // runTests is variable in my pipeline telling me whether tests should be run

After the build finishes, I just need to call:

def accessToken = obtainBitbucketToken()
notifyCommit(accessToken, commitHash, repository, keyPullRequestBuild, false, runTests)

This code needs to be run on unix node which has curl and jsawk tools installed (I tried JsonSlurper which should be built into the Groovy, but it kept crashing with serialization exceptions, even when used only from functions marked with @NonCPS).

This message was sent by Atlassian JIRA (v7.1.7#71011-sha1:2526d7c)
Atlassian logo

dodoentertainment@gmail.com (JIRA)

unread,
Jun 19, 2016, 3:46:02 PM6/19/16
to jenkinsc...@googlegroups.com

I forgot to mention - you need to define functions getBitbucketOAuthKey() and getBitbucketOAuthSecret() which return OAuth key and secret for OAuth consumer.

oldenkamperwin@gmail.com (JIRA)

unread,
Jun 19, 2016, 6:48:03 PM6/19/16
to jenkinsc...@googlegroups.com

As of just now I filled a PR for this plugin and a plugin for bitbucket.
When both are put together the whole workflow works as intended.
See the PR : https://github.com/jenkinsci/bitbucket-branch-source-plugin/pull/5
See the Bitbucket plugin: https://github.com/topicusfinan/bitbucket-webhooks-plugin

oldenkamperwin@gmail.com (JIRA)

unread,
Jun 19, 2016, 6:49:02 PM6/19/16
to jenkinsc...@googlegroups.com
Erwin Oldenkamp updated an issue
 
Change By: Erwin Oldenkamp
Comment:
As of just now I filled a PR for this plugin and a plugin for bitbucket.
When both are put together the whole workflow works as intended.
See the PR : [https://github.com/jenkinsci/bitbucket-branch-source-plugin/pull/5]
See the Bitbucket plugin: [https://github.com/topicusfinan/bitbucket-webhooks-plugin]

amuniz@cloudbees.com (JIRA)

unread,
Jul 2, 2016, 2:11:03 PM7/2/16
to jenkinsc...@googlegroups.com
Antonio Muñiz commented on New Feature JENKINS-33507
 
Re: Bitbucket Server webhooks and Bitbucket Cloud build status notifications

Another alternative for webhooks in BB Server. And the author has sent a PR to support it. I could not test it yet.

Maybe this plugin could expose an API to register the webhook in the repository.

oldenkamperwin@gmail.com (JIRA)

unread,
Jul 2, 2016, 6:49:04 PM7/2/16
to jenkinsc...@googlegroups.com

I'm currently creating this api. Its almost done, just need some more testing and re-approval by Atlassian when its done.

aaron@trouter.co.uk (JIRA)

unread,
Aug 1, 2016, 6:14:04 AM8/1/16
to jenkinsc...@googlegroups.com
Aaron Trout updated an issue
 
Change By: Aaron Trout
Attachment: Screen Shot 2016-08-01 at 11.12.23.png

aaron@trouter.co.uk (JIRA)

unread,
Aug 1, 2016, 6:14:04 AM8/1/16
to jenkinsc...@googlegroups.com
Aaron Trout commented on New Feature JENKINS-33507
 
Re: Bitbucket Server webhooks and Bitbucket Cloud build status notifications

+1 for webhooks on Bitbucket Server. Could it just enable the proper Jenkins hook plugin?

amuniz@cloudbees.com (JIRA)

unread,
Sep 17, 2016, 5:25:05 PM9/17/16
to jenkinsc...@googlegroups.com
Antonio Muñiz closed an issue as Fixed
 
Change By: Antonio Muñiz
Status: Open Closed
Resolution: Fixed

amuniz@cloudbees.com (JIRA)

unread,
Sep 17, 2016, 5:27:03 PM9/17/16
to jenkinsc...@googlegroups.com
Antonio Muñiz commented on New Feature JENKINS-33507
 
Re: Bitbucket Server webhooks and Bitbucket Cloud build status notifications

Webhooks support for BB Server merged into master (not released yet).

For the records: the plugin required at BB Server side is https://marketplace.atlassian.com/plugins/nl.topicus.bitbucket.bitbucket-webhooks/server/overview

itaisanders@gmail.com (JIRA)

unread,
Sep 18, 2016, 1:29:06 AM9/18/16
to jenkinsc...@googlegroups.com

does this mean we also have the build status notification fixed?

mihai.marinescu@bksv.com (JIRA)

unread,
Sep 29, 2016, 2:22:03 AM9/29/16
to jenkinsc...@googlegroups.com

Antonio Muñiz When do you plan to release this ? Is there a way i can test this now ?

Reply all
Reply to author
Forward
0 new messages