Skip to content

Commit b11a882

Browse files
Handle case for source outside translations folder
1 parent 8402e33 commit b11a882

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,4 @@ cython_debug/
170170
# PyPI configuration file
171171
.pypirc
172172
.DS_Store
173-
temp/
173+
temp/

main.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@
1010
from datetime import datetime
1111
from subprocess import Popen, PIPE
1212
from pathlib import Path
13+
from typing import Optional, Union
1314

1415
from github import Github, Auth
1516
from dotenv import load_dotenv
17+
1618
load_dotenv()
1719

1820

19-
def run(cmds: list[str]) -> tuple[str, str, int]:
21+
def run(
22+
cmds: list[str], cwd: Optional[Union[str, Path]] = None
23+
) -> tuple[str, str, int]:
2024
"""Run a command in the shell and print the standard output, error and return code.
2125
2226
Parameters
2327
----------
2428
cmds : list
2529
List of commands to run.
30+
cwd : str, optional
31+
Current working directory to run the command in. If None, use the current working directory.
2632
2733
Returns
2834
-------
@@ -33,11 +39,12 @@ def run(cmds: list[str]) -> tuple[str, str, int]:
3339
rc : int
3440
Return code of the command.
3541
"""
36-
p = Popen(cmds, stdout=PIPE, stderr=PIPE)
42+
p = Popen(cmds, stdout=PIPE, stderr=PIPE, cwd=cwd)
3743
out, err = p.communicate()
3844
stdout = out.decode()
3945
stderr = err.decode()
4046
print("\n\n\nCmd: \n" + " ".join(cmds))
47+
print("Cwd: \n", cwd or os.getcwd())
4148
print("Out: \n", stdout)
4249
print("Err: \n", stderr)
4350
print("Code: \n", p.returncode)
@@ -84,10 +91,13 @@ def sync_website_content(
8491
base_folder = Path(os.getcwd())
8592
source_folder_path = base_folder / source_folder
8693
translations_folder_path = base_folder / translations_folder
87-
print("\n\n### Syncing content from source repository to translations repository.\n\n")
94+
print(
95+
"\n\n### Syncing content from source repository to translations repository.\n\n"
96+
)
8897
print("Base folder: ", base_folder)
8998
print("Source folder: ", source_folder_path)
9099
print("Translation folder: ", translations_folder_path)
100+
91101
run(["git", "config", "--global", "user.name", f'"{name}"'])
92102
run(["git", "config", "--global", "user.email", f'"{email}"'])
93103

@@ -128,24 +138,27 @@ def sync_website_content(
128138

129139
date_time = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
130140
branch_name = f"content-sync-{date_time}"
131-
os.chdir(translations_repo.split("/")[1])
132-
print("\n\ngetcwd:", os.getcwd())
133-
run(["git", "checkout", "-b", branch_name])
134141

135-
# os.chdir(translations_folder)
136142
# FIXME: If on the same level do this, otherwise no parent?
137-
run(["rsync", "-avr", "--delete", str(source_folder_path), str(translations_folder_path.parent)])
138-
run(["git", "status"])
143+
if source_folder_path.name == translations_folder_path.name:
144+
dest = str(translations_folder_path.parent)
145+
else:
146+
dest = str(translations_folder_path)
139147

140-
run(["git", "add", "."])
148+
run(["rsync", "-avr", "--delete", str(source_folder_path), dest])
149+
run(["git", "status"], cwd=translations_folder_path)
150+
run(["git", "add", "."], cwd=translations_folder_path)
141151
_out, _err, rc = run(["git", "diff", "--staged", "--quiet"])
142152

143153
pr_title = "Update content"
144154
github_token = os.environ.get("GITHUB_TOKEN", "")
145155
if rc:
146-
run(["git", "commit", "-S", "-m", "Update content."])
147-
run(["git", "remote", "-v"])
148-
run(["git", "push", "-u", "origin", branch_name])
156+
run(
157+
["git", "commit", "-S", "-m", "Update content."],
158+
cwd=translations_folder_path,
159+
)
160+
run(["git", "remote", "-v"], cwd=translations_folder_path)
161+
run(["git", "push", "-u", "origin", branch_name], cwd=translations_folder_path)
149162

150163
os.environ["GITHUB_TOKEN"] = token
151164
run(
@@ -161,7 +174,8 @@ def sync_website_content(
161174
pr_title,
162175
"--body",
163176
"Automated content update.",
164-
]
177+
],
178+
cwd=translations_folder_path,
165179
)
166180
os.environ["GITHUB_TOKEN"] = github_token
167181

@@ -192,23 +206,24 @@ def sync_website_content(
192206
and signed_by in commit.commit.verification.payload # type: ignore
193207
)
194208

195-
# if all(checks):
196-
# print("\n\nAll commits are signed, auto-merging!")
197-
# # https://cli.github.com/manual/gh_pr_merge
198-
# os.environ["GITHUB_TOKEN"] = token
199-
# run(
200-
# [
201-
# "gh",
202-
# "pr",
203-
# "merge",
204-
# branch_name,
205-
# "--auto",
206-
# "--squash",
207-
# "--delete-branch",
208-
# ]
209-
# )
210-
# else:
211-
# print("\n\nNot all commits are signed, abort merge!")
209+
if all(checks):
210+
print("\n\nAll commits are signed, auto-merging!")
211+
# https://cli.github.com/manual/gh_pr_merge
212+
os.environ["GITHUB_TOKEN"] = token
213+
run(
214+
[
215+
"gh",
216+
"pr",
217+
"merge",
218+
branch_name,
219+
"--auto",
220+
"--squash",
221+
"--delete-branch",
222+
],
223+
cwd=translations_folder_path,
224+
)
225+
else:
226+
print("\n\nNot all commits are signed, abort merge!")
212227

213228
break
214229

0 commit comments

Comments
 (0)