Compare commits
1 Commits
4b38bcc252
...
1e92388b78
Author | SHA1 | Date |
---|---|---|
shekhar-cis | 1e92388b78 |
|
@ -23,37 +23,32 @@ from twisted.python import log
|
|||
|
||||
from dateutil.parser import parse as dateparse
|
||||
|
||||
HTTP_USER_AGENT = 'User-Agent'
|
||||
_HEADER_USER_AGENT = 'User-Agent'
|
||||
_HEADER_SIGNATURE = 'X-TX-Signature'
|
||||
_EVENT_KEY = 'event'
|
||||
branch = 'v0.6'
|
||||
transifexSecret = ""
|
||||
transifexUsername = ""
|
||||
transifexPassword = ""
|
||||
transifex_webhook_url = 'https://buildbot.bitmessage.org/change_hook/transifex'
|
||||
transifex_dict = {}
|
||||
secret = ""
|
||||
master = ""
|
||||
|
||||
from transifex.api import transifex_api
|
||||
|
||||
transifex_api.setup(auth='my_token')
|
||||
org = transifex_api.Organization.get(slug='my_org')
|
||||
proj = org.fetch('projects').get(slug='my_project')
|
||||
lang = transifex_api.Language.get(code='<lang>')
|
||||
resource = proj.fetch('resources').get(slug='my_resource')
|
||||
url = transifex_api.ResourceTranslationsAsyncDownload.download(
|
||||
resource=resource, language=lang,content_encoding = 'text',
|
||||
file_type = 'default', pseudo = False
|
||||
)
|
||||
gitHubToken = os.environ('gitHubToken')
|
||||
|
||||
class TransifexHandler(BaseHookHandler):
|
||||
|
||||
def __init__(self, transifex_dict, secret, master,):
|
||||
def __init__(self, master, secret, transifex_dict):
|
||||
self.secret = secret
|
||||
self.master = master
|
||||
self.transifex_dict = transifex_dict
|
||||
|
||||
|
||||
def returnMessage(self, status = False, message = "Unimplemented"):
|
||||
output = json.dumps({"status": "OK" if status else "FAIL", "message": message})
|
||||
return [output, [('Content-type', 'text/plain'),
|
||||
('Content-Length', str(len(output)))
|
||||
]]
|
||||
|
||||
def verifyTransifexSignature(
|
||||
self, request, content, rendered_secret, signature, header_signature
|
||||
):
|
||||
|
@ -73,7 +68,7 @@ class TransifexHandler(BaseHookHandler):
|
|||
)
|
||||
if tx_signature() != header_signature:
|
||||
raise ValueError('Invalid secret')
|
||||
|
||||
|
||||
try:
|
||||
if signature != os.environ.get('HTTP_X_TX_SIGNATURE'):
|
||||
return False
|
||||
|
@ -81,14 +76,7 @@ class TransifexHandler(BaseHookHandler):
|
|||
except:
|
||||
return False
|
||||
|
||||
def updateLocalTranslationDestination(self, ts, lang, branch):
|
||||
call(["git", "pull", "--all", "-q"])
|
||||
call(["git", "stash", "-q"])
|
||||
call(["git", "checkout", "-q", branch])
|
||||
call(["git", "checkout", "-q", "-b", "translate_" + lang + "_" + str(ts)])
|
||||
call(["git", "branch", "-q", "--set-upstream-to=origin/v0.6"])
|
||||
|
||||
def downloadTranslatedLanguage(ts, lang):
|
||||
def downloadTranslatedLanguage(self, ts, lang):
|
||||
headers = {"Authorization": "Basic " + b64encode(transifexUsername + ":" + transifexPassword)}
|
||||
fname = "bitmessage_" + lang.lower() + ".po"
|
||||
with open("src/translations/" + fname, "wt") as handle:
|
||||
|
@ -99,32 +87,46 @@ class TransifexHandler(BaseHookHandler):
|
|||
handle.write(content.encode("utf-8"))
|
||||
return response
|
||||
|
||||
def process_translation_completed(self, transifex_dict, event_type, codebase):
|
||||
# refname = payload["ref"]
|
||||
payload = transifex_dict
|
||||
def commitTranslatedLanguage(self, ts, lang):
|
||||
call(["kivy", "src/translations/messages.pro"])
|
||||
call(["git", "add", "src/translations/bitmessage_" + lang + ".ts", "src/translations/bitmessage_" + lang + ".qm"])
|
||||
call(["git", "commit", "-q", "-S", "-m", "Auto-updated language %s from transifex" % (lang)])
|
||||
newbranch = "translate_" + lang + "_" + str(ts)
|
||||
call(["git", "push", "-q", "translations", newbranch + ":" + newbranch])
|
||||
branch = transifex_dict['branch']
|
||||
|
||||
request = {
|
||||
"title": "Translation update " + lang,
|
||||
"body": "Auto-updated from transifex",
|
||||
"head": "PyBitmessageTranslations:" + newbranch,
|
||||
"base": branch
|
||||
}
|
||||
headers = {"Authorization": "token " + gitHubToken}
|
||||
response = requests.post("https://api.github.com/repos/Bitmessage/PyBitmessage/pulls",
|
||||
headers=headers, data=json.dumps(request))
|
||||
return response
|
||||
|
||||
|
||||
def process_translation_completed(self, payload, transifex_dict, event_type, codebase):
|
||||
changes = []
|
||||
# We only care about regular heads or tags
|
||||
# match = re.match(r"^refs/(heads|tags)/(.+)$", refname)
|
||||
if "Transifex" in os.environ.get("HTTP_USER_AGENT"):
|
||||
if not self.verifyTransifexSignature(
|
||||
request, content, rendered_secret, signature, header_signature):
|
||||
print('Invalid transifex signature!')
|
||||
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'] and 100 in transifex_response['translated']:
|
||||
ts = int(time.time())
|
||||
lang = transifex_response['language']
|
||||
branch = transifex_dict['branch']
|
||||
self.updateLocalTranslationDestination(ts, lang.lower(), branch)
|
||||
self.downloadTranslatedLanguage(ts, lang.lower())
|
||||
response = self.commitTranslatedLanguage(ts, lang.lower())
|
||||
if response.ok:
|
||||
output, responseHeaders = self.returnMessage(True, "Processed.")
|
||||
else:
|
||||
output, responseHeaders = self.returnMessage(False, "Error: %i." % (response.status_code))
|
||||
else:
|
||||
payload = self.transifex_dict
|
||||
if 'pybitmessage-test' in payload['transifex_data']['project'] and 'messagespot' in payload['transifex_data']['translation_completed']['resource']:
|
||||
if 'translation_completed' in payload['transifex_data'] and '100' in payload['transifex_data']['translated']:
|
||||
ts = int(time.time())
|
||||
lang = payload['transifex_data']['translation_completed']['language']
|
||||
branch = payload['transifex_data']['branch']
|
||||
self.updateLocalTranslationDestination(ts, lang.lower(), branch)
|
||||
self.downloadTranslatedLanguage(ts, lang.lower())
|
||||
output, responseHeaders = self.returnMessage(False, "Nothing to do")
|
||||
else:
|
||||
output, responseHeaders = self.returnMessage(False, "Nothing to do")
|
||||
|
||||
|
||||
repository = payload['repository']
|
||||
repo_url = repository['ssh_url']
|
||||
project = repository['full_name']
|
||||
|
||||
commits = payload['commits']
|
||||
# if isinstance(self.options, dict) and self.options.get('onlyIncludePushCommit', False):
|
||||
# commits = commits[:1]
|
||||
|
||||
|
@ -154,17 +156,19 @@ class TransifexHandler(BaseHookHandler):
|
|||
# if codebase is not None:
|
||||
# change['codebase'] = codebase
|
||||
# changes.insert(0, change)
|
||||
# return changes
|
||||
return changes
|
||||
|
||||
def process_review_completed(self, payload, transifex_data):
|
||||
pass
|
||||
|
||||
|
||||
def _transform_variables(self, payload=transifex_dict):
|
||||
def _transform_variables(self, payload, transifex_dict):
|
||||
retval = {
|
||||
'project': payload[''].get('project'),
|
||||
'repository': [payload.get('resource')],
|
||||
'branch': payload.get('language')
|
||||
'project': payload['project'],
|
||||
"translated": payload['translated'],
|
||||
"resource": payload['resource'],
|
||||
"event": payload['event'],
|
||||
"language": payload['language']
|
||||
}
|
||||
return retval
|
||||
|
||||
|
@ -190,6 +194,11 @@ class TransifexHandler(BaseHookHandler):
|
|||
digestmod=hashlib.sha256)
|
||||
header_signature = bytes2unicode(
|
||||
request.getHeader(_HEADER_SIGNATURE))
|
||||
self.verifyTransifexSignature(
|
||||
request, content, rendered_secret,
|
||||
signature, header_signatur
|
||||
)
|
||||
|
||||
event_type = bytes2unicode(payload.get(_EVENT_KEY), "None")
|
||||
log.msg("Received event '{}' from transifex".format(event_type))
|
||||
|
||||
|
|
Loading…
Reference in New Issue