bitbucket-bulk-add-hook
bitbucket-bulk-add-hook copied to clipboard
Using script to modify exsiting repos
Is there an easy way to add to/change this script to alter web-hooks that exist already through a PUT request?
I think (I'd need confirmation from @simonrjones) that the only way to do this is first do a GET request to find the list of webhooks (for each repository), then process these to decide how they should change, then update them on a hook by hook basis.
I feel the BitBucket docs lack in this department and it may end up being easier to DELETE old hooks and recreate.
I.e. you could search for all hooks where the URL matches slack.com then replace those (and only those) with the hook you desire?
So what if I run this script twice, would it add the webhook again?
It would make sense that once a slack webhook is added, it doesn't get added twice. And once the the URL is updated (or removed), it is possible to change/delete it.
When you run the script it looks to see if there is an EXACT match to the hook URL you are providing, for example:
https://example.com/web/hook/123
Will only be added if there's not an exact match. So running the script a second time will only add the web-hook to repositories which don't have the web-hook.
If you look at lines 108 to 116 you can see the 'checking' code:
$hooks = $api->getRepositoryWebHooks($account, $url);
$slackIntegrated = false;
if (!empty($hooks)) {
foreach ($hooks->values as $hook) {
if ($hook->url == $hookUrl) {
$slackIntegrated = true;
}
}
}
The $slackIntegrated value is then used to decide if we should POST a new hook. This section could be replaced with a $webhookStatus variable that stored something like 'hook present', 'requires update' or 'no hook'.
You can then adjust your flow to update/add when required.
That would be nice indeed. Perhaps it could also be nice to create a 'cleanup' command, for when you stop using Slack.