Updated method names

This commit is contained in:
shekhar-cis 2023-01-09 21:56:28 +05:30
parent 89305ec602
commit ab6d1083ec
Signed by untrusted user: shekhar-cis
GPG Key ID: F4F00AB04E83F9A7

View File

@ -1,14 +1,12 @@
"""Transifex webhook handler """ """Transifex webhook handler """
import os
import base64 import base64
import time
import json
import hmac
import hashlib import hashlib
from subprocess import call import hmac
from base64 import b64encode import json
import os
import requests import requests
import re
import time
from buildbot.process.properties import Properties from buildbot.process.properties import Properties
from buildbot.util import bytes2unicode, unicode2bytes from buildbot.util import bytes2unicode, unicode2bytes
@ -17,37 +15,36 @@ from buildbot.www.hooks.base import BaseHookHandler
from twisted.internet import defer from twisted.internet import defer
from twisted.python import log from twisted.python import log
from dateutil.parser import parse as dateparse
_HEADER_USER_AGENT = 'User-Agent' _HEADER_USER_AGENT = 'User-Agent'
_HEADER_SIGNATURE = 'X-TX-Signature' _HEADER_SIGNATURE = 'X-TX-Signature'
_HEADER_URL_PATH = 'X-TX-Url' _HEADER_URL_PATH = 'X-TX-Url'
HTTP_DATE = 'date' _HTTP_DATE = 'date'
_EVENT_KEY = 'event' _EVENT_KEY = 'event'
transifex_request_data = {} author = 'buildbot-transifex'
class TransifexHandler(BaseHookHandler): 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: if not options:
options = {} options = {}
self.secret = secret self.secret = secret
self.master = master self.master = master
self.options = options self.options = options
self.transifex_dict = transifex_dict self.transifex_to_github_map = transifex_to_github_map
def returnMessage(self, status = False, message = "Unimplemented"): def returnMessage(self, status = False, message = "Unimplemented"):
output = json.dumps({"status": "OK" if status else "FAIL", "message": message}) output = json.dumps({"status": "OK" if status else "FAIL", "message": message})
return [output, [('Content-type', 'application/json')]] return [output, [('Content-type', 'application/json')]]
def verifyTransifexSignature( def _verifyTransifexSignature(
self, request, content, signature, header_signature self, request, content, signature, header_signature
): ):
http_verb = 'POST' http_verb = 'POST'
http_url_path = request.getHeader(_HEADER_URL_PATH) 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() content_md5 = hashlib.md5(content).hexdigest()
msg = b'\n'.join([ msg = b'\n'.join([
http_verb, http_url_path, http_gmt_date, content_md5 http_verb, http_url_path, http_gmt_date, content_md5
@ -60,39 +57,25 @@ class TransifexHandler(BaseHookHandler):
).digest() ).digest()
) )
if tx_signature != header_signature: if tx_signature != header_signature:
return False raise ValueError("Tx Signature mismatch")
if signature != request.getHeader(_HEADER_SIGNATURE): 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 = [] changes = []
transifex_response = self._transform_variables(payload, transifex_dict) translated_request = self._transform_variables(payload['project'], payload['resource'])
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 [])
change = { change = {
'author': 'buildbot-transifex, 'author': "buildbot-transifex",
'resource': transifex_response['resource'], 'branch': translated_request["branch"],
'branch': transifex_dict['branch'], 'project': translated_request["project"],
'project': transifex_response['project'],
'event': event_type,
'properties': { 'properties': {
'branch': branch, "transifex_language": payload.get("language", "None"),
'revision': revision, "transifex_event": payload.get("event", "None"),
'language': lang, "transifex_project": payload.get("project", "None"),
'resource': resource, "transifex_resource": payload.get("resource", "None"),
'project': project "transifex_branch": "v0.6"
} }
} }
if codebase is not None: if codebase is not None:
@ -100,18 +83,22 @@ class TransifexHandler(BaseHookHandler):
changes.insert(0, change) changes.insert(0, change)
return changes return changes
def _transform_variables(self, payload, transifex_dict): def _transform_variables(self, transifex_project, transifex_resource):
project = payload.get('project', 'None') if transifex_project is None:
transform_values = { raise ValueError("Unknown project %s from transifex".format(transifex_project))
project: { key = "{}/{}".format(transifex_project, transifex_resource)
"repository": "https://github.com/Bitmessage/PyBitmessage", _map = self.map[key]
"branch": "v0.6" repository = _map["repository"]
project = re.sub(r'^.*/(.*?)(\.git)?$', r'\1', repository)
return{
'project': project,
'repository': repository,
'branch': _map["branch"],
} }
}
return transform_values
@defer.inlineCallbacks @defer.inlineCallbacks
def getChanges(self, request): def getChanges(self, request):
change = {}
self.secret = None self.secret = None
if isinstance(self.options, dict): if isinstance(self.options, dict):
self.secret = self.options.get('secret') self.secret = self.options.get('secret')
@ -126,36 +113,23 @@ class TransifexHandler(BaseHookHandler):
p = Properties() p = Properties()
p.master = self.master p.master = self.master
option = self.options option = self.options
self.rendered_secret = yield p.render(self.secret) rendered_secret = yield p.render(self.secret)
signature = hmac.new( signature = hmac.new(
unicode2bytes(self.rendered_secret), unicode2bytes(rendered_secret),
unicode2bytes(content_text.strip()), unicode2bytes(content_text.strip()),
digestmod=hashlib.sha256) digestmod=hashlib.sha256)
header_signature = bytes2unicode( header_signature = bytes2unicode(
request.getHeader(_HEADER_SIGNATURE)) request.getHeader(_HEADER_SIGNATURE))
self.verifyTransifexSignature( self._verifyTransifexSignature(request, content, rendered_secret, signature, header_signature)
request, content, self.rendered_secret,
signature, header_signature
)
event_type = payload.get("event", "None") 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" mapped_request = self._transform_variables(payload['project'], payload['resource'])
transifex_request_data['revision'] = ""
transifex_request_data["properties"] = "langugage"
transifex_request_data["properties"] = "resource"
transifex_request_data["properties"] = "project"
transifex_request_data["properties"] = { change["changes"] = {
"branch": branch, "author": author,
"revision": revision "repository": mapped_request["repository"],
} "project": mapped_request["project"],
transifex_request_data["changes"] = { "branch": mapped_request["branch"]
"author": "buildbot-transifex",
"repository": project,
} }
log.msg("Received event '{}' from transifex".format(event_type)) log.msg("Received event '{}' from transifex".format(event_type))
@ -171,5 +145,4 @@ class TransifexHandler(BaseHookHandler):
return (changes, 'transifex') return (changes, 'transifex')
transifex = TransifexHandler
transifex = TransifexHandler(transifex_dict)