stash-jenkins-postreceive-webhook
stash-jenkins-postreceive-webhook copied to clipboard
Programmatically configure the Stash webhook?
Hi Michael,
First, thank you for creating and maintaining a key piece of functionality. I've been cooking up CD at a client recently, and it was my first experience with Stash. It's a bit wacky that Atlassian doesn't support webhooks in a commercial dev tool in the year 2015. Neverthless, you're awesome for helping fill that gap.
I normally have my Jenkins pipelines call an API on GitHub/GitHubEn/GitLab to programmatically register the SCM webhook for the repo of the job.
Does your plugin (or Stash core) support programmatic config of your plugin?
Cheers, Steve
All Stash plugins can be enabled and configured via its REST API.
Here is some Python sample code to configure and enable the plugin in a single step:
def activate_jenkins_hook(repo):
headers_json = {
'Content-type': 'application/json',
'Accept': 'application/json'
}
hook_settings = {
"jenkinsBase": jenkins_baseurl,
"gitRepoUrl": repo['cloneUrl'],
"cloneType": "ssh",
"ignoreCommitters": "",
"branchOptions": "",
"branchOptionsBranches": ""
}
api_url = "http://%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook/enabled" % (servername, repo['project']['key'], repo['slug'])
r = requests.put(api_url, data=json.dumps(hook_settings), headers=headers_json)
Note: edited to add the cloneType
parameter, thanks to kbaltrinic for the hint.
Trying to do this via the rest API, you may be impacted by issue #139.
How do I find the other methods available? I'm trying to turn of (disable) the hook after having configured it by mistake.
For example, we have over 100 repos of which, say, 70 need to have the jenkins hook enabled for CI & CD jobs. I created a script with goes through all 100 repos and enables the hook... now, we realized those 30 needed to have the hook off...
So, I used POST to https://$server/rest/api/1.0/projects/$project/repos/$repo/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook/enabled with the json file such as { "jenkinsBase": "http://$jenkins", "gitRepoUrl": "https://$server/scm/$project/$repo.git", "ignoreCommitters": "", "cloneType":"http", "omitHashCode":false, "omitBranchName":false, "branchOptions": "", "branchOptionsBranches": "" } which works great to enable it... but I can't disable it after...
I tried a GET, PUT and POST to addresses like this: https://$server/rest/api/1.0/projects/$project/repos/$repo/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook/disabled https://$server/rest/api/1.0/projects/$project/repos/$repo/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook/off https://$server/rest/api/1.0/projects/$project/repos/$repo/settings/hooks/com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook/disable
but none of that works...
@slcma you need to call DELETE to disable, I have used this before with success.
https://developer.atlassian.com/static/rest/bitbucket-server/5.1.0/bitbucket-rest.html#idm45588159111264
Today I programmatically enable, configure and disable the hook using a simple python script (it accesses the bitbucket server apis). I think we can close this issue, I can share my scripts if anyone is interested.
Yes please @thrownullpointer - please share your script
yes Please. can you share the script to me please @thrownullpointer @slcma
@thrownullpointer I'm interested in your script too.