Skip to content

Commit 642e085

Browse files
committed
Clean up code
1 parent 17ea4a6 commit 642e085

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ jobs:
122122
123123
## License
124124
125-
The scripts and documentation in this project are released under the [MIT License](https://github.com/Scientific-Python-Translations/translations-sync/blob/main/LICENSE.txt).
125+
The scripts and documentation in this project are released under the [MIT License](https://github.com/Scientific-Python-Translations/content-sync/blob/main/LICENSE.txt).

main.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""Sync content from source repository to translations repository.
2+
3+
This script is intended to be run as a GitHub Action. It will sync content
4+
from a source repository to a translations repository, create a new branch,
5+
open a pull request for the changes and finally merge them.
6+
"""
7+
18
import os
29
import traceback
310
from datetime import datetime
@@ -6,25 +13,8 @@
613
from github import Github, Auth
714

815

9-
# Set the output value by writing to the outputs in the Environment File, mimicking the behavior defined here:
10-
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
11-
def set_github_action_output(output_name, output_value):
12-
"""Set the output value by writing to the outputs in the Environment File.
13-
14-
Parameters
15-
----------
16-
output_name : str
17-
Name of the output.
18-
output_value : str
19-
Value of the output.
20-
"""
21-
f = open(os.path.abspath(os.environ["GITHUB_OUTPUT"]), "a")
22-
f.write(f"{output_name}={output_value}")
23-
f.close()
24-
25-
26-
def run(cmds):
27-
"""Run a command in the shell and print output.
16+
def run(cmds: list[str]) -> tuple[str, str, int]:
17+
"""Run a command in the shell and print the standard output, error and return code.
2818
2919
Parameters
3020
----------
@@ -42,25 +32,27 @@ def run(cmds):
4232
"""
4333
p = Popen(cmds, stdout=PIPE, stderr=PIPE)
4434
out, err = p.communicate()
35+
stdout = out.decode()
36+
stderr = err.decode()
4537
print("\n\n\nCmd: \n" + " ".join(cmds))
46-
print("Out: \n", out.decode())
47-
print("Err: \n", err.decode())
38+
print("Out: \n", stdout)
39+
print("Err: \n", stderr)
4840
print("Code: \n", p.returncode)
49-
return out, err, p.returncode
41+
return stdout, stderr, p.returncode
5042

5143

5244
def sync_website_content(
53-
username,
54-
token,
55-
source_repo,
56-
source_folder,
57-
source_ref,
58-
translations_repo,
59-
translations_folder,
60-
translations_ref,
61-
name,
62-
email,
63-
):
45+
username: str,
46+
token: str,
47+
source_repo: str,
48+
source_folder: str,
49+
source_ref: str,
50+
translations_repo: str,
51+
translations_folder: str,
52+
translations_ref: str,
53+
name: str,
54+
email: str,
55+
) -> None:
6456
"""Sync content from source repository to translations repository.
6557
6658
Parameters
@@ -176,21 +168,31 @@ def sync_website_content(
176168
for commit in pr.get_commits():
177169
print(
178170
[
179-
commit.commit.verification.verified,
171+
commit.commit.verification.verified, # type: ignore
180172
signed_by,
181-
commit.commit.verification.payload,
173+
commit.commit.verification.payload, # type: ignore
182174
]
183175
)
184176
checks.append(
185-
commit.commit.verification.verified
186-
and signed_by in commit.commit.verification.payload
177+
commit.commit.verification.verified # type: ignore
178+
and signed_by in commit.commit.verification.payload # type: ignore
187179
)
188180

189181
if all(checks):
190182
print("\n\nAll commits are signed, auto-merging!")
191183
# https://cli.github.com/manual/gh_pr_merge
192184
os.environ["GITHUB_TOKEN"] = token
193-
run(["gh", "pr", "merge", branch_name, "--auto", "--squash"])
185+
run(
186+
[
187+
"gh",
188+
"pr",
189+
"merge",
190+
branch_name,
191+
"--auto",
192+
"--squash",
193+
"--delete-branch",
194+
]
195+
)
194196
else:
195197
print("\n\nNot all commits are signed, abort merge!")
196198

@@ -205,7 +207,7 @@ def parse_input() -> dict:
205207
gh_input = {
206208
# Automations Bot account
207209
"username": "scientificpythontranslations",
208-
# Provided by organization secrets
210+
# Github Personal Access Token provided by organization secrets
209211
"token": os.environ["TOKEN"],
210212
# Provided by user action input
211213
"source_repo": os.environ["INPUT_SOURCE-REPO"],
@@ -221,7 +223,7 @@ def parse_input() -> dict:
221223
return gh_input
222224

223225

224-
def main():
226+
def main() -> None:
225227
try:
226228
gh_input = parse_input()
227229
sync_website_content(**gh_input)

0 commit comments

Comments
 (0)