This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint
executable file
·92 lines (74 loc) · 3.08 KB
/
entrypoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2020 Pier Luigi Fiorini <[email protected]>
#
# SPDX-License-Identifier: MIT
import glob
import os
import subprocess
import shlex
import tempfile
def run(cmd, **kwargs):
out = []
for c in cmd:
out.append(shlex.quote(c))
print('+ ' + repr(' '.join(out))[1:-1])
return subprocess.run(cmd, **kwargs)
def main():
github_repository = os.environ['GITHUB_REPOSITORY']
tx_token = os.environ.get('INPUT_TX_TOKEN') or os.environ.get('TX_TOKEN')
ssh_key = os.environ.get('INPUT_SSH_KEY')
translations_folder = os.environ.get('INPUT_TRANSLATIONS_FOLDER')
committer_email = os.environ.get('INPUT_COMMITTER_EMAIL', '[email protected]')
committer_name = os.environ.get('INPUT_COMMITTER_NAME', 'Transifex Github action')
pull_translations = os.environ.get('INPUT_PULL_TRANSLATIONS', 'false') == 'true'
push_sources = os.environ.get('INPUT_PUSH_SOURCES', 'false') == 'true'
try:
minimum_perc = int(os.environ.get('INPUT_MINIMUM_PERC', '0'))
except ValueError:
minimum_perc = 0
args = []
if minimum_perc > 0:
args.append(f'--minimum-perc={minimum_perc}')
# Set TX_TOKEN
if tx_token:
os.environ['TX_TOKEN'] = tx_token
# Save ssh key
ssh_tempfile = ''
if ssh_key:
fd, ssh_tempfile = tempfile.mkstemp()
with open(ssh_tempfile, 'w') as f:
f.write(ssh_key)
# Configure email and name
subprocess.check_output(['git', 'config', '--local', 'user.email', committer_email])
subprocess.check_output(['git', 'config', '--local', 'user.name', committer_name])
# Default push policy
subprocess.check_output(['git', 'config', 'push.default', 'simple'])
# Git checkout action does a shallow clone. That prevents us to
# access common history of branches
run(['git', 'fetch', '--unshallow'], check=True)
# Push and pull
if pull_translations:
pull_args = ['tx', 'pull', '--no-interactive', '--force', '--all']
run(pull_args + args, check=True)
if push_sources:
run(['tx', 'push', '--no-interactive', '--source'], check=True)
# Add files
paths = glob.glob(translations_folder, recursive=True)
for path in paths:
run(['git', 'add', path], check=True)
# Show staged changes (check=False otherwise it will raise an exception
# when there are no changes)
run(['git', 'diff', '--staged'])
# Commit (but stash non needed changes, for example sometimes .tx/config)
if run(['git', 'diff', '--staged', '--quiet']).returncode != 0:
if ssh_tempfile:
os.environ['GIT_SSH_COMMAND'] = f'ssh -i {ssh_tempfile} -o StrictHostKeyChecking=no'
run(['git', 'remote', 'set-url', '--push', 'origin', f'[email protected]:{github_repository}'], check=True)
msg = 'Automatic merge of Transifex translations\n\n[ci skip]'
run(['git', 'commit', '--message=' + msg], check=True)
run(['git', 'stash'])
run(['git', 'push'], check=True)
else:
print('Nothing to commit')
if __name__ == '__main__':
main()