diff --git a/action.yml b/action.yml index 4d02a9c..7a1823d 100644 --- a/action.yml +++ b/action.yml @@ -46,7 +46,7 @@ runs: python3 -m pip install -r "$GITHUB_ACTION_PATH/requirements.txt" - name: Run commit-check shell: bash - run: ${{ github.action_path }}/entrypoint.sh + run: python3 ${{ github.action_path }}/main.py env: MESSAGE: ${{ inputs.message }} BRANCH: ${{ inputs.branch }} diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100755 index 0523cae..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash -set -euo pipefail - -ret_code=0 - -run_commit_check(){ - args="" - if [[ "$MESSAGE" == "true" ]]; then - args="$args --message" - fi - if [[ "$BRANCH" == "true" ]]; then - args="$args --branch" - fi - if [[ "$AUTHOR_NAME" == "true" ]]; then - args="$args --author-name" - fi - if [[ "$AUTHOR_EMAIL" == "true" ]]; then - args="$args --author-email" - fi - if [[ "$COMMIT_SIGNOFF" == "true" ]]; then - args="$args --commit-signoff" - fi - - echo "commit-check $args" - commit-check $args > result.txt - ret_code=$? -} - -add_job_summary(){ - if [[ "$JOB_SUMMARY" == "false" ]]; then - exit - fi - - if [ -s result.txt ]; then - # strips ANSI colors - sed -i "s,\x1B\[[0-9;]*[a-zA-Z],,g" result.txt - cat result.txt - echo "### Commit-Check ❌" >> "$GITHUB_STEP_SUMMARY" - echo '```' >> "$GITHUB_STEP_SUMMARY" - cat result.txt >> "$GITHUB_STEP_SUMMARY" - echo '```' >> "$GITHUB_STEP_SUMMARY" - ret_code=1 - else - echo "### Commit-Check ✔️" >> "$GITHUB_STEP_SUMMARY" - ret_code=0 - fi -} - -run_commit_check -add_job_summary - -if [[ "$DRY_RUN" == "true" ]]; then - ret_code=0 -fi - -exit $ret_code diff --git a/main.py b/main.py new file mode 100755 index 0000000..2b07aec --- /dev/null +++ b/main.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import os +import sys +import subprocess +import re + + +def run_commit_check() -> int: + args = ["--message", "--branch", "--author-name", "--author-email", "--commit-signoff"] + args = [arg for arg, value in zip(args, [MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF]) if value == "true"] + + command = ["commit-check"] + args + print(" ".join(command)) + with open("result.txt", "w") as result_file: + result = subprocess.run(command, stdout=result_file, stderr=subprocess.PIPE, check=False) + return result.returncode + + +def add_job_summary() -> int: + if JOB_SUMMARY == "false": + sys.exit() + + if os.path.getsize("result.txt") > 0: + with open("result.txt", "r") as result_file: + result_text = re.sub(r'\x1B\[[0-9;]*[a-zA-Z]', '', result_file.read()) # Remove ANSI colors + + with open(GITHUB_STEP_SUMMARY, "a") as summary_file: + summary_file.write("### Commit-Check ❌\n```\n") + summary_file.write(result_text) + summary_file.write("```") + return 1 + else: + with open(GITHUB_STEP_SUMMARY, "a") as summary_file: + summary_file.write("### Commit-Check ✔️\n") + return 0 + + +MESSAGE = os.getenv("MESSAGE", "false") +BRANCH = os.getenv("BRANCH", "false") +AUTHOR_NAME = os.getenv("AUTHOR_NAME", "false") +AUTHOR_EMAIL = os.getenv("AUTHOR_EMAIL", "false") +COMMIT_SIGNOFF = os.getenv("COMMIT_SIGNOFF", "false") +DRY_RUN = os.getenv("DRY_RUN", "false") +JOB_SUMMARY = os.getenv("JOB_SUMMARY", "false") +GITHUB_STEP_SUMMARY = os.environ["GITHUB_STEP_SUMMARY"] + +print(f"MESSAGE = {MESSAGE}") +print(f"BRANCH = {BRANCH}") +print(f"AUTHOR_NAME = {AUTHOR_NAME}") +print(f"AUTHOR_EMAIL = {AUTHOR_EMAIL}") +print(f"COMMIT_SIGNOFF = {COMMIT_SIGNOFF}") +print(f"DRY_RUN = {DRY_RUN}") +print(f"JOB_SUMMARY = {JOB_SUMMARY}\n") + +ret_code = run_commit_check() +ret_code += add_job_summary() # Combine return codes + +if DRY_RUN == "true": + ret_code = 0 + +sys.exit(ret_code)