mirror of https://github.com/tiangolo/fastapi.git
🔨 Refactor translation script to allow committing in place (#14687)
This commit is contained in:
parent
f03a1502a0
commit
49653aa295
|
|
@ -30,6 +30,11 @@ on:
|
||||||
type: string
|
type: string
|
||||||
required: false
|
required: false
|
||||||
default: ""
|
default: ""
|
||||||
|
commit_in_place:
|
||||||
|
description: Whether to commit changes directly instead of making a PR
|
||||||
|
type: boolean
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
langs:
|
langs:
|
||||||
|
|
@ -109,3 +114,4 @@ jobs:
|
||||||
LANGUAGE: ${{ matrix.lang }}
|
LANGUAGE: ${{ matrix.lang }}
|
||||||
EN_PATH: ${{ github.event.inputs.en_path }}
|
EN_PATH: ${{ github.event.inputs.en_path }}
|
||||||
COMMAND: ${{ matrix.command }}
|
COMMAND: ${{ matrix.command }}
|
||||||
|
COMMIT_IN_PLACE: ${{ github.event.inputs.commit_in_place }}
|
||||||
|
|
|
||||||
|
|
@ -360,6 +360,9 @@ def make_pr(
|
||||||
command: Annotated[str | None, typer.Option(envvar="COMMAND")] = None,
|
command: Annotated[str | None, typer.Option(envvar="COMMAND")] = None,
|
||||||
github_token: Annotated[str, typer.Option(envvar="GITHUB_TOKEN")],
|
github_token: Annotated[str, typer.Option(envvar="GITHUB_TOKEN")],
|
||||||
github_repository: Annotated[str, typer.Option(envvar="GITHUB_REPOSITORY")],
|
github_repository: Annotated[str, typer.Option(envvar="GITHUB_REPOSITORY")],
|
||||||
|
commit_in_place: Annotated[
|
||||||
|
bool, typer.Option(envvar="COMMIT_IN_PLACE", show_default=True)
|
||||||
|
] = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
print("Setting up GitHub Actions git user")
|
print("Setting up GitHub Actions git user")
|
||||||
repo = git.Repo(Path(__file__).absolute().parent.parent)
|
repo = git.Repo(Path(__file__).absolute().parent.parent)
|
||||||
|
|
@ -371,6 +374,12 @@ def make_pr(
|
||||||
["git", "config", "user.email", "github-actions[bot]@users.noreply.github.com"],
|
["git", "config", "user.email", "github-actions[bot]@users.noreply.github.com"],
|
||||||
check=True,
|
check=True,
|
||||||
)
|
)
|
||||||
|
current_branch = repo.active_branch.name
|
||||||
|
if current_branch == "master" and commit_in_place:
|
||||||
|
print("Can't commit directly to master")
|
||||||
|
raise typer.Exit(code=1)
|
||||||
|
|
||||||
|
if not commit_in_place:
|
||||||
branch_name = "translate"
|
branch_name = "translate"
|
||||||
if language:
|
if language:
|
||||||
branch_name += f"-{language}"
|
branch_name += f"-{language}"
|
||||||
|
|
@ -379,6 +388,8 @@ def make_pr(
|
||||||
branch_name += f"-{secrets.token_hex(4)}"
|
branch_name += f"-{secrets.token_hex(4)}"
|
||||||
print(f"Creating a new branch {branch_name}")
|
print(f"Creating a new branch {branch_name}")
|
||||||
subprocess.run(["git", "checkout", "-b", branch_name], check=True)
|
subprocess.run(["git", "checkout", "-b", branch_name], check=True)
|
||||||
|
else:
|
||||||
|
print(f"Committing in place on branch {current_branch}")
|
||||||
print("Adding updated files")
|
print("Adding updated files")
|
||||||
git_path = Path("docs")
|
git_path = Path("docs")
|
||||||
subprocess.run(["git", "add", str(git_path)], check=True)
|
subprocess.run(["git", "add", str(git_path)], check=True)
|
||||||
|
|
@ -391,6 +402,7 @@ def make_pr(
|
||||||
subprocess.run(["git", "commit", "-m", message], check=True)
|
subprocess.run(["git", "commit", "-m", message], check=True)
|
||||||
print("Pushing branch")
|
print("Pushing branch")
|
||||||
subprocess.run(["git", "push", "origin", branch_name], check=True)
|
subprocess.run(["git", "push", "origin", branch_name], check=True)
|
||||||
|
if not commit_in_place:
|
||||||
print("Creating PR")
|
print("Creating PR")
|
||||||
g = Github(github_token)
|
g = Github(github_token)
|
||||||
gh_repo = g.get_repo(github_repository)
|
gh_repo = g.get_repo(github_repository)
|
||||||
|
|
@ -400,7 +412,9 @@ def make_pr(
|
||||||
+ f"\n\nIt uses the prompt file https://github.com/fastapi/fastapi/blob/master/docs/{language}/llm-prompt.md."
|
+ f"\n\nIt uses the prompt file https://github.com/fastapi/fastapi/blob/master/docs/{language}/llm-prompt.md."
|
||||||
+ "\n\nIn most cases, it's better to make PRs updating that file so that the LLM can do a better job generating the translations than suggesting changes in this PR."
|
+ "\n\nIn most cases, it's better to make PRs updating that file so that the LLM can do a better job generating the translations than suggesting changes in this PR."
|
||||||
)
|
)
|
||||||
pr = gh_repo.create_pull(title=message, body=body, base="master", head=branch_name)
|
pr = gh_repo.create_pull(
|
||||||
|
title=message, body=body, base="master", head=branch_name
|
||||||
|
)
|
||||||
print(f"Created PR: {pr.number}")
|
print(f"Created PR: {pr.number}")
|
||||||
print("Finished")
|
print("Finished")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue