From e9cee269abc084faa3a468364e76b63ceacef10d Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Tue, 27 Jun 2023 19:07:30 -0400 Subject: [PATCH 1/4] feat: adds outputs to Action and CLI Signed-off-by: Jennifer Power --- .mega-linter.yml | 3 ++- Dockerfile | 17 +++++---------- action.yml | 22 ++++++++----------- entrypoint.sh | 23 ++++++++++++++++++++ tests/trestlebot/test_bot.py | 34 +++++++++++++++++++++++++---- trestlebot/bot.py | 38 ++++++++++++++++++++++++-------- trestlebot/cli.py | 42 ++++++++++++++++++++++++++---------- 7 files changed, 129 insertions(+), 50 deletions(-) create mode 100644 entrypoint.sh diff --git a/.mega-linter.yml b/.mega-linter.yml index a17e939c..a6b4e31c 100644 --- a/.mega-linter.yml +++ b/.mega-linter.yml @@ -8,4 +8,5 @@ ENABLE_LINTERS: DISABLE_ERRORS_LINTERS: - MAKEFILE_CHECKMAKE - - REPOSITORY_KICS \ No newline at end of file + - REPOSITORY_KICS + - BASH_SHELLCHECK \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 17fd6408..87ac8be1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,16 +55,9 @@ RUN apt-get update \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* -ENTRYPOINT [ "/bin/sh", "-c", "python3.8 -m trestlebot \ - --markdown-path=${MARKDOWN_PATH} \ - --assemble-model=${ASSEMBLE_MODEL} \ - --ssp-index-path=${SSP_INDEX_PATH} \ - --commit-message=${COMMIT_MESSAGE} \ - --branch=${BRANCH} \ - --patterns=${PATTERNS} \ - --committer-name=${COMMIT_USER_NAME} \ - --committer-email=${COMMIT_USER_EMAIL} \ - --author-name=${AUTHOR_NAME} \ - --author-email=${AUTHOR_EMAIL} \ - --working-dir=${WORKING_DIR}" ] +COPY ./entrypoint.sh / + +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["python3.8 -m trestlebot"] diff --git a/action.yml b/action.yml index ff776b2a..91f18509 100644 --- a/action.yml +++ b/action.yml @@ -16,7 +16,7 @@ inputs: commit_message: description: Commit message required: false - default: "chore: sync automatic updates" + default: "Sync automatic updates" branch: description: Git branch name, where changes should be pushed too. Required if Action is used on the `pull_request` event required: false @@ -46,21 +46,17 @@ inputs: required: false default: ${{ github.actor }}@users.noreply.github.com +outputs: + changes: + description: Value is "true" if changes were committed back to the repository. + commit: + description: Full hash of the created commit. Only present if the "changes" output is "true". + runs: using: "docker" image: "Dockerfile" - env: - MARKDOWN_PATH: ${{ inputs.markdown_path }} - ASSEMBLE_MODEL: ${{ inputs.assemble_model }} - SSP_INDEX_PATH: ${{ inputs.ssp_index_path }} - COMMIT_MESSAGE: ${{ inputs.commit_message }} - BRANCH: ${{ inputs.branch }} - PATTERNS: ${{ inputs.file_pattern }} - COMMIT_USER_NAME: ${{ inputs.commit_user_name }} - COMMIT_USER_EMAIL: ${{ inputs.commit_user_email }} - COMMIT_AUTHOR_NAME: ${{ inputs.commit_author_name }} - COMMIT_AUTHOR_EMAIL: ${{ inputs.commit_author_email }} - REPOSITORY: ${{ inputs.repository }} + entrypoint: [ "/entrypoint.sh"] + branding: icon: "check" color: "green" diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..db8280a7 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +set -eu + +commit=$(python3.8 -m trestlebot \ + --markdown-path="${INPUT_MARKDOWN_PATH}" \ + --assemble-model="${INPUT_ASSEMBLE_MODEL}" \ + --ssp-index-path="${INPUT_SSP_INDEX_PATH}" \ + --commit-message="${INPUT_COMMIT_MESSAGE}" \ + --branch="${INPUT_BRANCH}" \ + --patterns="${INPUT_FILE_PATTERN}" \ + --committer-name="${INPUT_COMMIT_USER_NAME}" \ + --committer-email="${INPUT_COMMIT_USER_EMAIL}" \ + --author-name="${INPUT_COMMIT_AUTHOR_NAME}" \ + --author-email="${INPUT_COMMIT_AUTHOR_EMAIL}" \ + --working-dir="${INPUT_WORKING_DIR}") + +if [ -n "$commit" ]; then + echo "changes=true" >> "$GITHUB_OUTPUT" + echo "commit=$commit" >> "$GITHUB_OUTPUT" +else + echo "changes=false" >> "$GITHUB_OUTPUT" +fi diff --git a/tests/trestlebot/test_bot.py b/tests/trestlebot/test_bot.py index 52bf0ce5..be1e25b9 100644 --- a/tests/trestlebot/test_bot.py +++ b/tests/trestlebot/test_bot.py @@ -58,12 +58,13 @@ def test_local_commit(tmp_repo: Tuple[str, Repo]) -> None: repo.index.add(test_file_path) # Commit the test file - bot._local_commit( + commit_sha = bot._local_commit( repo, commit_user="Test User", commit_email="test@example.com", commit_message="Test commit message", ) + assert commit_sha != "" # Verify that the commit is made commit = next(repo.iter_commits()) @@ -88,13 +89,15 @@ def test_local_commit_with_committer(tmp_repo: Tuple[str, Repo]) -> None: repo.index.add(test_file_path) # Commit the test file - bot._local_commit( + commit_sha = bot._local_commit( repo, commit_user="Test Commit User", commit_email="test-committer@example.com", commit_message="Test commit message", ) + assert commit_sha != "" + # Verify that the commit is made commit = next(repo.iter_commits()) assert commit.message.strip() == "Test commit message" @@ -118,7 +121,7 @@ def test_local_commit_with_author(tmp_repo: Tuple[str, Repo]) -> None: repo.index.add(test_file_path) # Commit the test file - bot._local_commit( + commit_sha = bot._local_commit( repo, commit_user="Test User", commit_email="test@example.com", @@ -126,6 +129,7 @@ def test_local_commit_with_author(tmp_repo: Tuple[str, Repo]) -> None: author_name="The Author", author_email="author@test.com", ) + assert commit_sha != "" # Verify that the commit is made commit = next(repo.iter_commits()) @@ -148,7 +152,7 @@ def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None: f.write("Test content") # Test running the bot - bot.run( + commit_sha = bot.run( working_dir=repo_path, branch="main", commit_name="Test User", @@ -159,6 +163,7 @@ def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None: patterns=["*.txt"], dry_run=True, ) + assert commit_sha != "" # Verify that the commit is made commit = next(repo.iter_commits()) @@ -170,3 +175,24 @@ def test_run_dry_run(tmp_repo: Tuple[str, Repo]) -> None: assert os.path.basename(test_file_path) in commit.stats.files clean(repo_path, repo) + + +def test_empty_commit(tmp_repo: Tuple[str, Repo]) -> None: + """Test running bot with no file updates""" + repo_path, repo = tmp_repo + + # Test running the bot + commit_sha = bot.run( + working_dir=repo_path, + branch="main", + commit_name="Test User", + commit_email="test@example.com", + commit_message="Test commit message", + author_name="The Author", + author_email="author@test.com", + patterns=["*.txt"], + dry_run=True, + ) + assert commit_sha == "" + + clean(repo_path, repo) diff --git a/trestlebot/bot.py b/trestlebot/bot.py index 1bd109c1..1272f22f 100644 --- a/trestlebot/bot.py +++ b/trestlebot/bot.py @@ -52,7 +52,7 @@ def _local_commit( commit_message: str, author_name: str = "", author_email: str = "", -) -> None: +) -> str: """Creates a local commit in git working directory""" try: # Set the user and email for the commit @@ -65,7 +65,10 @@ def _local_commit( author = Actor(name=author_name, email=author_email) # Commit the changes - gitwd.index.commit(commit_message, author=author) + commit = gitwd.index.commit(commit_message, author=author) + + # Return commit sha + return commit.hexsha except GitCommandError as e: raise RepoException(f"Git commit failed: {e}") from e @@ -81,8 +84,25 @@ def run( patterns: List[str], pre_tasks: Optional[List[TaskBase]] = None, dry_run: bool = False, -) -> int: - """Run Trestle Bot and return exit code""" +) -> str: + """Run Trestle Bot and return exit code + + Args: + working_dir: Location of the git repo + branch: Branch to put updates to + commit_name: Name of the user for commit creation + commit_email: Email of the user for commit creation + author_name: Name of the commit author + author_email: Email of the commit author + patterns: List of file patterns for `git add` + pre_task: Option task list to executing before updating the workspace + dry_run: Only complete local work. Do not push. + + Returns: + A string containing the full commit sha. Defaults to "" if + there was no updates + """ + commit_sha: str = "" # Execute bot pre-tasks before committing repository updates if pre_tasks is not None: @@ -100,7 +120,7 @@ def run( _stage_files(repo, patterns) if repo.is_dirty(): - _local_commit( + commit_sha = _local_commit( repo, commit_name, commit_email, @@ -111,7 +131,7 @@ def run( if dry_run: logging.info("Dry run mode is enabled. Do not push to remote.") - return 0 + return commit_sha try: # Get the remote repository by name @@ -121,13 +141,13 @@ def run( remote.push(refspec=f"HEAD:{branch}") logging.info(f"Changes pushed to {branch} successfully.") - return 0 + return commit_sha except GitCommandError as e: raise RepoException(f"Git push to {branch} failed: {e}") from e else: logging.info("Nothing to commit") - return 0 + return commit_sha else: logging.info("Nothing to commit") - return 0 + return commit_sha diff --git a/trestlebot/cli.py b/trestlebot/cli.py index 7fe0c3fe..31d8bb5b 100644 --- a/trestlebot/cli.py +++ b/trestlebot/cli.py @@ -112,6 +112,15 @@ def _parse_cli_arguments() -> argparse.Namespace: return parser.parse_args() +def handle_exception( + exception: Exception, msg: str = "Exception occurred during execution" +) -> int: + """Log the exception and return the exit code""" + logging.exception(msg + f": {exception}") + + return 1 + + def run() -> None: """Trestle Bot entry point function.""" args = _parse_cli_arguments() @@ -145,16 +154,27 @@ def run() -> None: ) pre_tasks.append(assemble_task) - exit_code = bot.run( - working_dir=args.working_dir, - branch=args.branch, - commit_name=args.committer_name, - commit_email=args.committer_email, - commit_message=args.commit_message, - author_name=args.author_name, - author_email=args.author_email, - pre_tasks=pre_tasks, - patterns=args.patterns, - ) + exit_code: int = 0 + + # Assume it is a successful run, if the bot + # throws an exception update the exit code accordingly + try: + commit_sha = bot.run( + working_dir=args.working_dir, + branch=args.branch, + commit_name=args.committer_name, + commit_email=args.committer_email, + commit_message=args.commit_message, + author_name=args.author_name, + author_email=args.author_email, + pre_tasks=pre_tasks, + patterns=args.patterns, + ) + + # Print the full commit sha + print(commit_sha) + + except Exception as e: + exit_code = handle_exception(e) sys.exit(exit_code) From 1c13255264f87f8e3676c7bdea7467421103dfb6 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Wed, 28 Jun 2023 13:33:03 -0400 Subject: [PATCH 2/4] chore: fixes comment spelling/grammar errors for run in bot.py Signed-off-by: Jennifer Power --- trestlebot/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trestlebot/bot.py b/trestlebot/bot.py index 1272f22f..269227bc 100644 --- a/trestlebot/bot.py +++ b/trestlebot/bot.py @@ -95,7 +95,7 @@ def run( author_name: Name of the commit author author_email: Email of the commit author patterns: List of file patterns for `git add` - pre_task: Option task list to executing before updating the workspace + pre_tasks: Optional task list to executing before updating the workspace dry_run: Only complete local work. Do not push. Returns: From 0cbdcce0b28069d10ea1db19c5f21aef3a223c7b Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Wed, 28 Jun 2023 14:16:31 -0400 Subject: [PATCH 3/4] fix: updates Dockerfile entrypoint to show log output Signed-off-by: Jennifer Power --- Dockerfile | 2 +- entrypoint.sh | 8 ++++++-- trestlebot/cli.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 87ac8be1..0dfe6c4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -59,5 +59,5 @@ COPY ./entrypoint.sh / RUN chmod +x /entrypoint.sh -ENTRYPOINT ["python3.8 -m trestlebot"] +ENTRYPOINT ["python3.8", "-m" , "trestlebot"] diff --git a/entrypoint.sh b/entrypoint.sh index db8280a7..f673a9a5 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,7 +2,7 @@ set -eu -commit=$(python3.8 -m trestlebot \ +output=$(python3.8 -m trestlebot \ --markdown-path="${INPUT_MARKDOWN_PATH}" \ --assemble-model="${INPUT_ASSEMBLE_MODEL}" \ --ssp-index-path="${INPUT_SSP_INDEX_PATH}" \ @@ -13,7 +13,11 @@ commit=$(python3.8 -m trestlebot \ --committer-email="${INPUT_COMMIT_USER_EMAIL}" \ --author-name="${INPUT_COMMIT_AUTHOR_NAME}" \ --author-email="${INPUT_COMMIT_AUTHOR_EMAIL}" \ - --working-dir="${INPUT_WORKING_DIR}") + --working-dir="${INPUT_WORKING_DIR}" 2>&1 | tee log.txt) + +cat log.txt + +commit=$(echo "$output" | grep "Commit Hash:" | sed 's/.*: //') if [ -n "$commit" ]; then echo "changes=true" >> "$GITHUB_OUTPUT" diff --git a/trestlebot/cli.py b/trestlebot/cli.py index 31d8bb5b..90f1d69f 100644 --- a/trestlebot/cli.py +++ b/trestlebot/cli.py @@ -172,7 +172,7 @@ def run() -> None: ) # Print the full commit sha - print(commit_sha) + print(f' Commit Hash: {commit_sha}') except Exception as e: exit_code = handle_exception(e) From d7b80f956046a039e05cdfe29f33ed8e8265e642 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Wed, 28 Jun 2023 14:21:34 -0400 Subject: [PATCH 4/4] chore: adds linting fix Signed-off-by: Jennifer Power --- trestlebot/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trestlebot/cli.py b/trestlebot/cli.py index 90f1d69f..c817eb14 100644 --- a/trestlebot/cli.py +++ b/trestlebot/cli.py @@ -172,7 +172,7 @@ def run() -> None: ) # Print the full commit sha - print(f' Commit Hash: {commit_sha}') + print(f" Commit Hash: {commit_sha}") except Exception as e: exit_code = handle_exception(e)