Update webhook and setup #1

Open
shekhar-cis wants to merge 5 commits from shekhar-cis/buildbot-transifex:transifex-webhook into master
Showing only changes of commit ab6d1083ec - Show all commits

View File

@ -1,14 +1,12 @@
"""Transifex webhook handler """
import os
import base64
import time
import json
import hmac
import hashlib
from subprocess import call
from base64 import b64encode
import hmac
import json
import os
import requests
import re
import time
from buildbot.process.properties import Properties
from buildbot.util import bytes2unicode, unicode2bytes
@ -17,37 +15,36 @@ from buildbot.www.hooks.base import BaseHookHandler
from twisted.internet import defer
from twisted.python import log
from dateutil.parser import parse as dateparse
_HEADER_USER_AGENT = 'User-Agent'
_HEADER_SIGNATURE = 'X-TX-Signature'
_HEADER_URL_PATH = 'X-TX-Url'
HTTP_DATE = 'date'
_HTTP_DATE = 'date'
_EVENT_KEY = 'event'
transifex_request_data = {}
author = 'buildbot-transifex'
class TransifexHandler(BaseHookHandler):
def __init__(self, master, secret, options, transifex_dict):
def __init__(self, master, secret, transifex_to_github_map, options=None):
if not options:
options = {}
self.secret = secret
self.master = master
self.options = options
self.transifex_dict = transifex_dict
self.transifex_to_github_map = transifex_to_github_map
def returnMessage(self, status = False, message = "Unimplemented"):
output = json.dumps({"status": "OK" if status else "FAIL", "message": message})
return [output, [('Content-type', 'application/json')]]
def verifyTransifexSignature(
def _verifyTransifexSignature(
self, request, content, signature, header_signature
):
http_verb = 'POST'
http_url_path = request.getHeader(_HEADER_URL_PATH)
http_gmt_date = request.getHeader(HTTP_DATE)
http_gmt_date = request.getHeader(_HTTP_DATE)
content_md5 = hashlib.md5(content).hexdigest()
msg = b'\n'.join([
http_verb, http_url_path, http_gmt_date, content_md5
@ -60,39 +57,25 @@ class TransifexHandler(BaseHookHandler):
).digest()
)
if tx_signature != header_signature:
return False
raise ValueError("Tx Signature mismatch")
if signature != request.getHeader(_HEADER_SIGNATURE):
return False
raise ValueError("Signature mismatch")
return True
def process_translation_completed(self, payload, transifex_dict, event_type, codebase):
def process_translation_completed(self, payload, codebase):
changes = []
transifex_response = self._transform_variables(payload, transifex_dict)
if 'pybitmessage-test' in transifex_response['project'] and 'messagespot' in transifex_response['resource']:
if 'translation_completed' in transifex_response['event']:
ts = int(time.time())
lang = transifex_response['language']
return
# if isinstance(self.options, dict):
# commits = commits[:1]
# # for commit in commits:
# # files = []
# # for kind in ('added', 'modified', 'removed'):
# # files.extend(commit.get(kind, []) or [])
translated_request = self._transform_variables(payload['project'], payload['resource'])
change = {
'author': 'buildbot-transifex,
'resource': transifex_response['resource'],
'branch': transifex_dict['branch'],
'project': transifex_response['project'],
'event': event_type,
'author': "buildbot-transifex",
'branch': translated_request["branch"],
'project': translated_request["project"],
'properties': {
'branch': branch,
'revision': revision,
'language': lang,
'resource': resource,
'project': project
"transifex_language": payload.get("language", "None"),
"transifex_event": payload.get("event", "None"),
"transifex_project": payload.get("project", "None"),
"transifex_resource": payload.get("resource", "None"),
"transifex_branch": "v0.6"
Review

this should be the randomly generated one, the branch of the PR

this should be the randomly generated one, the branch of the PR
}
}
if codebase is not None:
@ -100,18 +83,22 @@ class TransifexHandler(BaseHookHandler):
changes.insert(0, change)
return changes
def _transform_variables(self, payload, transifex_dict):
project = payload.get('project', 'None')
transform_values = {
project: {
"repository": "https://github.com/Bitmessage/PyBitmessage",
"branch": "v0.6"
}
def _transform_variables(self, transifex_project, transifex_resource):
if transifex_project is None:
raise ValueError("Unknown project %s from transifex".format(transifex_project))
key = "{}/{}".format(transifex_project, transifex_resource)
_map = self.map[key]
repository = _map["repository"]
project = re.sub(r'^.*/(.*?)(\.git)?$', r'\1', repository)
return{
'project': project,
'repository': repository,
'branch': _map["branch"],
}
return transform_values
@defer.inlineCallbacks
def getChanges(self, request):
change = {}
self.secret = None
if isinstance(self.options, dict):
self.secret = self.options.get('secret')
@ -126,36 +113,23 @@ class TransifexHandler(BaseHookHandler):
p = Properties()
p.master = self.master
option = self.options
self.rendered_secret = yield p.render(self.secret)
rendered_secret = yield p.render(self.secret)
signature = hmac.new(
unicode2bytes(self.rendered_secret),
unicode2bytes(rendered_secret),
unicode2bytes(content_text.strip()),
digestmod=hashlib.sha256)
header_signature = bytes2unicode(
request.getHeader(_HEADER_SIGNATURE))
self.verifyTransifexSignature(
request, content, self.rendered_secret,
signature, header_signature
)
self._verifyTransifexSignature(request, content, rendered_secret, signature, header_signature)
event_type = payload.get("event", "None")
language = payload.get("language", 'None')
project = payload.get("project", 'None')
resource = payload.get("resource", 'None')
transifex_request_data['branch'] = "v0.6"
transifex_request_data['revision'] = ""
transifex_request_data["properties"] = "langugage"
transifex_request_data["properties"] = "resource"
transifex_request_data["properties"] = "project"
mapped_request = self._transform_variables(payload['project'], payload['resource'])
transifex_request_data["properties"] = {
"branch": branch,
"revision": revision
}
transifex_request_data["changes"] = {
"author": "buildbot-transifex",
"repository": project,
change["changes"] = {
"author": author,
"repository": mapped_request["repository"],
"project": mapped_request["project"],
"branch": mapped_request["branch"]
}
log.msg("Received event '{}' from transifex".format(event_type))
@ -171,5 +145,4 @@ class TransifexHandler(BaseHookHandler):
return (changes, 'transifex')
transifex = TransifexHandler(transifex_dict)
transifex = TransifexHandler