1
0
Fork 0

webhook: Rebase testing on master if fast-forwardable

This commit is contained in:
Félix Piédallu 2024-06-01 10:37:38 +02:00 committed by Salamandar
parent 59172cd232
commit 633084e22f

View file

@ -9,7 +9,7 @@ import tempfile
import logging import logging
from pathlib import Path from pathlib import Path
from git import Actor, Repo from git import Actor, Repo, GitCommandError
from sanic import HTTPResponse, Request, Sanic, response from sanic import HTTPResponse, Request, Sanic, response
# add apps/tools to sys.path # add apps/tools to sys.path
@ -86,32 +86,59 @@ def on_push(request: Request) -> HTTPResponse:
logging.info(f"{repository} -> branch '{branch}'") logging.info(f"{repository} -> branch '{branch}'")
need_push = False
with tempfile.TemporaryDirectory() as folder_str: with tempfile.TemporaryDirectory() as folder_str:
folder = Path(folder_str) folder = Path(folder_str)
repo = Repo.clone_from( repo = Repo.clone_from(
f"https://{github_login()}:{github_token()}@github.com/{repository}", f"https://{github_login()}:{github_token()}@github.com/{repository}",
to_path=folder, to_path=folder,
single_branch=True,
branch=branch,
) )
generate_READMEs(folder) # First rebase the testing branch if possible
if branch in ["master", "testing"]:
result = git_repo_rebase_testing_fast_forward(repo)
need_push = need_push or result
repo.git.add("README*.md") repo.git.checkout(branch)
repo.git.add("ALL_README.md") result = generate_and_commit_readmes(repo)
need_push = need_push or result
diff_empty = len(repo.index.diff("HEAD")) == 0 if not need_push:
if diff_empty:
logging.debug("nothing to do") logging.debug("nothing to do")
return response.text("nothing to do") return response.text("nothing to do")
repo.index.commit( logging.debug(f"Pushing {repository}")
"Auto-update READMEs", author=Actor("yunohost-bot", "yunohost@yunohost.org") repo.remote().push(quiet=False, all=True)
)
repo.remote().push(quiet=False)
return response.text("ok") return response.text("ok")
def generate_and_commit_readmes(repo: Repo) -> bool:
assert repo.working_tree_dir is not None
generate_READMEs(Path(repo.working_tree_dir))
repo.git.add("README*.md")
repo.git.add("ALL_README.md")
diff_empty = len(repo.index.diff("HEAD")) == 0
if diff_empty:
return False
repo.index.commit(
"Auto-update READMEs", author=Actor("yunohost-bot", "yunohost@yunohost.org")
)
return True
def git_repo_rebase_testing_fast_forward(repo: Repo) -> bool:
try:
repo.git.checkout("testing")
except GitCommandError:
return False
if not repo.is_ancestor("testing", "master"):
return False
repo.git.merge("master", ff_only=True)
return True
def main() -> None: def main() -> None:
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()