Changing settings via REST API

API

Webhook to Jenkins hook settings can be updated via the Atlassian Bitbucket API via the following endpoint:

/rest/api/latest/projects/{0}/repos/{1}/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook/enabled

For more on the Bitbucket REST API for updating and changing hooks, read here.

We've created a sample python script to automate updating settings. It reads in a settings.json file which you can download here or below.

To change the webhook source, add jenkinsEndPointType to the json setting with one of the following values:

Bitbucket Branch Source Plugin

com.nerdwin15.stash.webhook.notifier.BitbucketSourceNotifier

Git Plugin

com.nerdwin15.stash.webhook.notifier.GitPluginNotifier

Direct Jobs Trigger

com.nerdwin15.stash.webhook.notifier.DirectJobsNotifier

Example scripts

configure-webhook-jenkins.py
#!/usr/bin/python import httplib import sys import json import argparse from urlparse import urlparse from base64 import b64encode parser = argparse.ArgumentParser(description='Sample script to update Webhook To Bitbucket settings examples.') parser.add_argument('--bitbucket_url', type=str, help='Url for Bitbucket instance.', default='http://localhost:7990/bitbucket') parser.add_argument('--username', type=str, help='Username for user with access to repository', default='admin') parser.add_argument('--password', type=str, help='Password for user with access to repository', default='admin') parser.add_argument('--project', type=str, help='Project key where repository is located', default='PROJECT_1') parser.add_argument('--repository', type=str, help='Repository key for the instance', default='rep_1') parser.add_argument('--settings', type=str, help='Path to the settings file. JSON formatted.', default='settings.json') args = parser.parse_args() bb_url = args.bitbucket_url username = args.username password = args.password project_key = args.project repository_slug = args.repository settings_path = args.settings end_point = "/rest/api/latest/projects/{0}/repos/{1}/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins" \ "%3AjenkinsPostReceiveHook/enabled".format(project_key, repository_slug) settings_file = open(settings_path, 'r') try: settings = settings_file.read() try: j = json.loads(settings) # just to validate except: print "Invalid JSON: %s" % settings sys.exit(1) finally: settings_file.close() url_obj = urlparse(bb_url) headers = { 'authorization': "Basic " + b64encode(username + ':' + password), 'Content-Type': 'application/json' } connection = httplib.HTTPConnection(url_obj.hostname, url_obj.port) try: connection.request("PUT", url_obj.path + end_point, body=settings, headers=headers) r1 = connection.getresponse() print r1.status, r1.reason, r1.getheaders() data1 = r1.read() print data1 finally: connection.close()
settings.json
{ "branchOptions": "whitelist", "branchOptionsBranches": "master, develop, feature-1", "cloneType": "custom", "cloneType2": "custom", "gitRepoUrl": "http://172.17.0.1:7990/bitbucket/scm/project_1/rep_1.git", "gitRepoUrl2": "http://172.17.0.1:7990/bitbucket/scm/project_1/rep_1.git", "ignoreCommitters": "admin, user", "jenkinsBase": "http://localhost:8080", "jenkinsBase2": "https://localhost:8083", "ignoreCerts": false, "omitHashCode": false, "omitBranchName": false, "omitTriggerBuildButton": false }

 

Parameter details

disabledEvents is a comma-separated list of class names of events to ignore. Supported class names are listed below, with links to the corresponding Bitbucket documentation explaining when each event is triggered:

Â