2023-01-06 16:08:43 +01:00
|
|
|
"""Transifex webhook handler """
|
2023-01-04 16:21:06 +01:00
|
|
|
import os
|
2022-12-28 09:04:44 +01:00
|
|
|
import base64
|
2023-01-04 16:21:06 +01:00
|
|
|
|
|
|
|
import time
|
2018-09-04 12:40:36 +02:00
|
|
|
import json
|
2021-03-09 21:54:06 +01:00
|
|
|
import hmac
|
|
|
|
import hashlib
|
2023-01-04 16:21:06 +01:00
|
|
|
from subprocess import call
|
|
|
|
from base64 import b64encode
|
2023-01-06 16:08:43 +01:00
|
|
|
import requests
|
2023-01-04 16:21:06 +01:00
|
|
|
|
2021-04-16 03:55:32 +02:00
|
|
|
from buildbot.process.properties import Properties
|
|
|
|
from buildbot.util import bytes2unicode, unicode2bytes
|
2018-09-04 12:40:36 +02:00
|
|
|
from buildbot.www.hooks.base import BaseHookHandler
|
|
|
|
|
2021-04-16 03:55:32 +02:00
|
|
|
from twisted.internet import defer
|
2018-09-04 12:40:36 +02:00
|
|
|
from twisted.python import log
|
2021-04-16 03:55:32 +02:00
|
|
|
|
2018-09-04 12:58:05 +02:00
|
|
|
from dateutil.parser import parse as dateparse
|
2018-09-04 12:40:36 +02:00
|
|
|
|
2022-12-28 09:04:44 +01:00
|
|
|
_HEADER_USER_AGENT = 'User-Agent'
|
|
|
|
_HEADER_SIGNATURE = 'X-TX-Signature'
|
2023-01-06 16:08:43 +01:00
|
|
|
_HEADER_URL_PATH = 'X-TX-Url'
|
|
|
|
HTTP_DATE = 'date'
|
2022-12-28 09:04:44 +01:00
|
|
|
_EVENT_KEY = 'event'
|
2023-01-06 16:08:43 +01:00
|
|
|
transifex_request_data = {}
|
2023-01-04 16:21:06 +01:00
|
|
|
|
2018-09-04 12:40:36 +02:00
|
|
|
|
2022-12-28 09:04:44 +01:00
|
|
|
class TransifexHandler(BaseHookHandler):
|
2018-09-04 12:40:36 +02:00
|
|
|
|
2023-01-06 16:08:43 +01:00
|
|
|
def __init__(self, master, secret, options, transifex_dict):
|
|
|
|
if not options:
|
|
|
|
options = {}
|
2023-01-04 16:21:06 +01:00
|
|
|
self.secret = secret
|
|
|
|
self.master = master
|
2023-01-06 16:08:43 +01:00
|
|
|
self.options = options
|
2023-01-04 16:21:06 +01:00
|
|
|
self.transifex_dict = transifex_dict
|
|
|
|
|
|
|
|
|
|
|
|
def returnMessage(self, status = False, message = "Unimplemented"):
|
|
|
|
output = json.dumps({"status": "OK" if status else "FAIL", "message": message})
|
2023-01-06 16:08:43 +01:00
|
|
|
return [output, [('Content-type', 'application/json')]]
|
2023-01-04 16:21:06 +01:00
|
|
|
|
|
|
|
def verifyTransifexSignature(
|
2023-01-06 16:08:43 +01:00
|
|
|
self, request, content, signature, header_signature
|
2023-01-04 16:21:06 +01:00
|
|
|
):
|
|
|
|
http_verb = 'POST'
|
2023-01-06 16:08:43 +01:00
|
|
|
http_url_path = request.getHeader(_HEADER_URL_PATH)
|
|
|
|
http_gmt_date = request.getHeader(HTTP_DATE)
|
2023-01-04 16:21:06 +01:00
|
|
|
content_md5 = hashlib.md5(content).hexdigest()
|
|
|
|
msg = b'\n'.join([
|
|
|
|
http_verb, http_url_path, http_gmt_date, content_md5
|
|
|
|
])
|
|
|
|
tx_signature = base64.b64encode(
|
|
|
|
hmac.new(
|
2023-01-06 16:08:43 +01:00
|
|
|
key=self.rendered_secret,
|
2023-01-04 16:21:06 +01:00
|
|
|
msg=msg,
|
|
|
|
digestmod=hashlib.sha256
|
|
|
|
).digest()
|
|
|
|
)
|
2023-01-06 16:08:43 +01:00
|
|
|
if tx_signature != header_signature:
|
2023-01-04 16:21:06 +01:00
|
|
|
return False
|
|
|
|
|
2023-01-06 16:08:43 +01:00
|
|
|
if signature != request.getHeader(_HEADER_SIGNATURE):
|
|
|
|
return False
|
2023-01-04 16:21:06 +01:00
|
|
|
|
|
|
|
def process_translation_completed(self, payload, transifex_dict, event_type, codebase):
|
2018-09-04 12:40:36 +02:00
|
|
|
changes = []
|
2023-01-04 16:21:06 +01:00
|
|
|
transifex_response = self._transform_variables(payload, transifex_dict)
|
|
|
|
if 'pybitmessage-test' in transifex_response['project'] and 'messagespot' in transifex_response['resource']:
|
2023-01-06 16:08:43 +01:00
|
|
|
if 'translation_completed' in transifex_response['event']:
|
2023-01-04 16:21:06 +01:00
|
|
|
ts = int(time.time())
|
|
|
|
lang = transifex_response['language']
|
2023-01-06 16:08:43 +01:00
|
|
|
return
|
2023-01-04 16:21:06 +01:00
|
|
|
|
2023-01-06 16:08:43 +01:00
|
|
|
# if isinstance(self.options, dict):
|
2023-01-04 16:21:06 +01:00
|
|
|
# commits = commits[:1]
|
|
|
|
|
2023-01-06 16:08:43 +01:00
|
|
|
# # for commit in commits:
|
|
|
|
# # files = []
|
|
|
|
# # for kind in ('added', 'modified', 'removed'):
|
|
|
|
# # files.extend(commit.get(kind, []) or [])
|
|
|
|
change = {
|
|
|
|
'author': 'buildbot-transifex,
|
|
|
|
'resource': transifex_response['resource'],
|
|
|
|
'branch': transifex_dict['branch'],
|
|
|
|
'project': transifex_response['project'],
|
|
|
|
'event': event_type,
|
|
|
|
'properties': {
|
|
|
|
'branch': branch,
|
|
|
|
'revision': revision,
|
|
|
|
'language': lang,
|
|
|
|
'resource': resource,
|
|
|
|
'project': project
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if codebase is not None:
|
|
|
|
change['codebase'] = codebase
|
|
|
|
changes.insert(0, change)
|
2018-09-04 12:40:36 +02:00
|
|
|
return changes
|
|
|
|
|
2023-01-04 16:21:06 +01:00
|
|
|
def _transform_variables(self, payload, transifex_dict):
|
2023-01-06 16:08:43 +01:00
|
|
|
project = payload.get('project', 'None')
|
|
|
|
transform_values = {
|
|
|
|
project: {
|
|
|
|
"repository": "https://github.com/Bitmessage/PyBitmessage",
|
|
|
|
"branch": "v0.6"
|
|
|
|
}
|
2022-12-28 09:04:44 +01:00
|
|
|
}
|
2023-01-06 16:08:43 +01:00
|
|
|
return transform_values
|
2022-12-28 09:04:44 +01:00
|
|
|
|
2021-04-16 03:55:32 +02:00
|
|
|
@defer.inlineCallbacks
|
2018-09-04 12:40:36 +02:00
|
|
|
def getChanges(self, request):
|
2023-01-04 16:21:06 +01:00
|
|
|
self.secret = None
|
2018-09-04 15:52:49 +02:00
|
|
|
if isinstance(self.options, dict):
|
2023-01-04 16:21:06 +01:00
|
|
|
self.secret = self.options.get('secret')
|
2018-09-04 12:40:36 +02:00
|
|
|
try:
|
|
|
|
content = request.content.read()
|
2021-03-09 21:54:06 +01:00
|
|
|
content_text = bytes2unicode(content)
|
|
|
|
payload = json.loads(content_text)
|
2018-09-27 13:21:10 +02:00
|
|
|
except Exception as exception:
|
|
|
|
raise ValueError('Error loading JSON: ' + str(exception))
|
2021-03-09 21:54:06 +01:00
|
|
|
|
2023-01-04 16:21:06 +01:00
|
|
|
if self.secret is not None:
|
2021-04-16 03:55:32 +02:00
|
|
|
p = Properties()
|
|
|
|
p.master = self.master
|
2023-01-06 16:08:43 +01:00
|
|
|
option = self.options
|
|
|
|
self.rendered_secret = yield p.render(self.secret)
|
2021-03-09 21:54:06 +01:00
|
|
|
signature = hmac.new(
|
2023-01-06 16:08:43 +01:00
|
|
|
unicode2bytes(self.rendered_secret),
|
2021-04-16 03:55:32 +02:00
|
|
|
unicode2bytes(content_text.strip()),
|
2021-03-09 21:54:06 +01:00
|
|
|
digestmod=hashlib.sha256)
|
|
|
|
header_signature = bytes2unicode(
|
|
|
|
request.getHeader(_HEADER_SIGNATURE))
|
2023-01-04 16:21:06 +01:00
|
|
|
self.verifyTransifexSignature(
|
2023-01-06 16:08:43 +01:00
|
|
|
request, content, self.rendered_secret,
|
2023-01-04 16:21:06 +01:00
|
|
|
signature, header_signature
|
2022-12-28 09:04:44 +01:00
|
|
|
)
|
2021-03-09 21:54:06 +01:00
|
|
|
|
2023-01-06 16:08:43 +01:00
|
|
|
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"
|
|
|
|
|
|
|
|
transifex_request_data["properties"] = {
|
|
|
|
"branch": branch,
|
|
|
|
"revision": revision
|
|
|
|
}
|
|
|
|
transifex_request_data["changes"] = {
|
|
|
|
"author": "buildbot-transifex",
|
|
|
|
"repository": project,
|
|
|
|
}
|
|
|
|
|
2022-12-28 09:04:44 +01:00
|
|
|
log.msg("Received event '{}' from transifex".format(event_type))
|
2018-09-04 12:40:36 +02:00
|
|
|
|
2022-12-28 09:04:44 +01:00
|
|
|
codebase = ""
|
2018-09-04 15:04:29 +02:00
|
|
|
changes = []
|
2019-10-09 18:33:06 +02:00
|
|
|
|
|
|
|
handler_function = getattr(self, 'process_{}'.format(event_type), None)
|
|
|
|
if not handler_function:
|
2022-12-28 09:04:44 +01:00
|
|
|
log.msg("Ignoring transifex event '{}'".format(event_type))
|
2019-10-09 18:33:06 +02:00
|
|
|
else:
|
|
|
|
changes = handler_function(payload, event_type, codebase)
|
2018-09-04 15:04:29 +02:00
|
|
|
|
2022-12-28 09:04:44 +01:00
|
|
|
return (changes, 'transifex')
|
2019-10-09 18:33:06 +02:00
|
|
|
|
|
|
|
|
2023-01-02 16:00:09 +01:00
|
|
|
transifex = TransifexHandler(transifex_dict)
|