stash-jenkins-postreceive-webhook icon indicating copy to clipboard operation
stash-jenkins-postreceive-webhook copied to clipboard

Programmatically configure the Stash webhook?

Open steve-jansen opened this issue 9 years ago • 8 comments

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

steve-jansen avatar Jul 17 '15 20:07 steve-jansen

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.

mschuett avatar Jul 20 '15 16:07 mschuett

Trying to do this via the rest API, you may be impacted by issue #139.

kbaltrinic avatar Jul 29 '15 12:07 kbaltrinic

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...

jeansergegagnon avatar Mar 09 '17 20:03 jeansergegagnon

@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

thrownullpointer avatar Jun 23 '17 00:06 thrownullpointer

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.

thrownullpointer avatar Jun 23 '17 00:06 thrownullpointer

Yes please @thrownullpointer - please share your script

jeansergegagnon avatar Jun 24 '17 14:06 jeansergegagnon

yes Please. can you share the script to me please @thrownullpointer @slcma

santhoshrao450 avatar Apr 24 '18 02:04 santhoshrao450

@thrownullpointer I'm interested in your script too.

tschlue avatar Apr 24 '18 07:04 tschlue