|
| 1 | +import os |
| 2 | +from datetime import datetime |
| 3 | +from subprocess import check_output, Popen, PIPE |
| 4 | + |
| 5 | +from github import Github, Auth |
| 6 | + |
| 7 | + |
| 8 | +# Set the output value by writing to the outputs in the Environment File, mimicking the behavior defined here: |
| 9 | +# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter |
| 10 | +def set_github_action_output(output_name, output_value): |
| 11 | + f = open(os.path.abspath(os.environ["GITHUB_OUTPUT"]), "a") |
| 12 | + f.write(f'{output_name}={output_value}') |
| 13 | + f.close() |
| 14 | + |
| 15 | + |
| 16 | +def sync_website_content(token, source_repo, source_folder, source_ref, translations_repo, translations_folder, translations_ref): |
| 17 | + cmds = ['git', 'clone', f'https://github.com/{source_repo}.git'] |
| 18 | + out = check_output(cmds) |
| 19 | + print(out) |
| 20 | + |
| 21 | + cmds = ['git', 'clone', f'https://github.com/{translations_repo}.git'] |
| 22 | + out = check_output(cmds) |
| 23 | + print(out) |
| 24 | + |
| 25 | + cmds = ['rsync', '-av', '--delete', source_folder, translations_folder] |
| 26 | + out = check_output(cmds) |
| 27 | + print(out) |
| 28 | + |
| 29 | + branch_name = datetime.now().strftime('updates-%Y-%m-%d-%H-%M-%S') |
| 30 | + os.chdir(translations_repo.split('/')[1]) |
| 31 | + |
| 32 | + cmds = ['git', 'checkout', '-b', branch_name] |
| 33 | + out = check_output(cmds) |
| 34 | + print(out) |
| 35 | + |
| 36 | + cmds = ['git', 'config', '--global', 'user.email', '"actions@github.com"'] |
| 37 | + out = check_output(cmds) |
| 38 | + print(out) |
| 39 | + |
| 40 | + cmds = ['git', 'config', '--global', 'user.name', '"GitHub Actions"'] |
| 41 | + out = check_output(cmds) |
| 42 | + print(out) |
| 43 | + |
| 44 | + cmds = ['git', 'add', '.'] |
| 45 | + out = check_output(cmds) |
| 46 | + print(out) |
| 47 | + |
| 48 | + auth = Auth.Token(token) |
| 49 | + g = Github(auth=auth) |
| 50 | + repo = g.get_repo(translations_repo) |
| 51 | + pulls = repo.get_pulls(state='closed', sort='created', direction='desc') |
| 52 | + pr_branch = None |
| 53 | + for pr in pulls: |
| 54 | + print(pr.number, pr.title) |
| 55 | + pr_branch = pr.head.ref |
| 56 | + if pr.title == "Update source content": |
| 57 | + break |
| 58 | + g.close() |
| 59 | + |
| 60 | + # cmds = ['git', 'diff', f'{pr_branch}..{branch_name}'] |
| 61 | + # out = check_output(cmds) |
| 62 | + # print(out) |
| 63 | + |
| 64 | + # git add . |
| 65 | + # # Only proceed to commit if there are changes |
| 66 | + # if git diff --staged --quiet; then |
| 67 | + # echo "No changes to commit." |
| 68 | + # echo "CONTENT_CHANGED=false" >> $GITHUB_ENV |
| 69 | + # else |
| 70 | + # git commit -m "Update website content" |
| 71 | + # echo "CONTENT_CHANGED=true" >> $GITHUB_ENV |
| 72 | + # git push -u origin ${{ env.BRANCH_NAME }} |
| 73 | + # fi |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +def parse_input(): |
| 78 | + gh_input = { |
| 79 | + 'github_token': os.environ["GITHUB_TOKEN"], |
| 80 | + 'source_repo': os.environ["INPUT_SOURCE-REPO"], |
| 81 | + 'source_folder': os.environ["INPUT_SOURCE-FOLDER"], |
| 82 | + 'source_ref': os.environ["INPUT_SOURCE-REF"], |
| 83 | + 'translations_repo': os.environ["INPUT_TRANSLATIONS-REPO"], |
| 84 | + 'translations_folder': os.environ["INPUT_TRANSLATIONS-FOLDER"], |
| 85 | + 'translations_ref': os.environ["INPUT_TRANSLATIONS-REF"], |
| 86 | + } |
| 87 | + return gh_input |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + github_token = os.environ["GITHUB_TOKEN"] |
| 94 | + source_repo = os.environ["INPUT_SOURCE-REPO"] |
| 95 | + source_folder = os.environ["INPUT_SOURCE-FOLDER"] |
| 96 | + source_ref = os.environ["INPUT_SOURCE-REF"] |
| 97 | + translations_repo = os.environ["INPUT_TRANSLATIONS-REPO"] |
| 98 | + translations_folder = os.environ["INPUT_TRANSLATIONS-FOLDER"] |
| 99 | + translations_ref = os.environ["INPUT_TRANSLATIONS-REF"] |
| 100 | + |
| 101 | + # repository = os.environ["GITHUB_REPOSITORY"] |
| 102 | + |
| 103 | + sync_website_content(github_token, source_repo, source_folder, source_ref, translations_repo, translations_folder, translations_ref) |
| 104 | + set_github_action_output('todo', 'Hello world') |
| 105 | + print("TESTING") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments