Skip to content

Instantly share code, notes, and snippets.

@NicolasGeraud
Created March 3, 2015 22:24
Show Gist options
  • Save NicolasGeraud/57bfe6396591730f29bc to your computer and use it in GitHub Desktop.
Save NicolasGeraud/57bfe6396591730f29bc to your computer and use it in GitHub Desktop.
BITBUCKET - How to add POST hook
import urllib
import urllib2
import base64
import json
username = "mail"
password = "passwd"
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
url = 'https://bitbucket.org/api/2.0/repositories/bekom/'
repositories = []
while True:
req = urllib2.Request(url)
req.add_header("Authorization", "Basic %s" % base64string)
response = json.load(urllib2.urlopen(req))
values = response['values']
currentPage = response['page']
maxPage = response['pagelen']
print "process %s/%s" % (currentPage, maxPage)
for v in values:
repositories.append(v['name'])
if currentPage == maxPage:
break
else:
url = response['next']
for repo in repositories:
url = "https://bitbucket.org/api/1.0/repositories/bekom/%s/services" % repo
values = {
'type': 'POST',
'URL': 'http://jenkins.com/bitbucket-hook/'
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
req.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(req)
result = response.read()
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment