From f170d0fc0e42cf9d65d53b102f64da99dd5aea07 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 04:32:39 +0000 Subject: [PATCH 1/7] feat: add main.py and remove entrypoint.sh --- entrypoint.sh | 56 ---------------------------------------------- main.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 56 deletions(-) delete mode 100755 entrypoint.sh create mode 100644 main.py 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 100644 index 0000000..a86adaa --- /dev/null +++ b/main.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import sys +import subprocess +import os + + +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 = result_file.read().replace("\x1B[0m", "") # 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("\n```\n") + 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.getenv("GITHUB_STEP_SUMMARY", "false") + +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}") +print(f"GITHUB_STEP_SUMMARY = {GITHUB_STEP_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) From 1ca5b527e840dc208bcb837fae80a91b3077953a Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 05:50:55 +0000 Subject: [PATCH 2/7] feat: update action.yml --- action.yml | 2 +- main.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) mode change 100644 => 100755 main.py 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/main.py b/main.py old mode 100644 new mode 100755 index a86adaa..d0689c5 --- a/main.py +++ b/main.py @@ -41,7 +41,6 @@ def add_job_summary() -> int: 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.getenv("GITHUB_STEP_SUMMARY", "false") print(f"MESSAGE = {MESSAGE}") print(f"BRANCH = {BRANCH}") @@ -49,8 +48,7 @@ def add_job_summary() -> int: print(f"AUTHOR_EMAIL = {AUTHOR_EMAIL}") print(f"COMMIT_SIGNOFF = {COMMIT_SIGNOFF}") print(f"DRY_RUN = {DRY_RUN}") -print(f"JOB_SUMMARY = {JOB_SUMMARY}") -print(f"GITHUB_STEP_SUMMARY = {GITHUB_STEP_SUMMARY}\n") +print(f"JOB_SUMMARY = {JOB_SUMMARY}\n") ret_code = run_commit_check() ret_code += add_job_summary() # Combine return codes From 17443769aeda5a87684da4610a75aec145f73e36 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 06:02:07 +0000 Subject: [PATCH 3/7] fix: define GITHUB_STEP_SUMMARY --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index d0689c5..e113811 100755 --- a/main.py +++ b/main.py @@ -41,6 +41,7 @@ def add_job_summary() -> int: 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}") From b18943fa49b792dd03ec267890df9ebbc13bd395 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 06:55:28 +0000 Subject: [PATCH 4/7] fix: update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index e113811..38ab38c 100755 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ def add_job_summary() -> int: if os.path.getsize("result.txt") > 0: with open("result.txt", "r") as result_file: - result_text = result_file.read().replace("\x1B[0m", "") # Remove ANSI colors + 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") From d71ffbb49f3e625fe9f43498210acf6760835e27 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 06:56:22 +0000 Subject: [PATCH 5/7] fix: update main.py --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 38ab38c..8b9cff4 100755 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 +import os import sys import subprocess -import os +import re def run_commit_check() -> int: From f255b85a2017e0a997bf93e24f9584b440cd10e8 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 06:57:43 +0000 Subject: [PATCH 6/7] fix: update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 8b9cff4..126f1bf 100755 --- a/main.py +++ b/main.py @@ -27,7 +27,7 @@ def add_job_summary() -> int: with open(GITHUB_STEP_SUMMARY, "a") as summary_file: summary_file.write("### Commit-Check ❌\n```\n") summary_file.write(result_text) - summary_file.write("\n```\n") + summary_file.write("\n```") return 1 else: with open(GITHUB_STEP_SUMMARY, "a") as summary_file: From 9eb36243c7fb237cfabcbbdb6a1eea1394703a15 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 11 Mar 2024 06:59:07 +0000 Subject: [PATCH 7/7] fix: update main.py --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 126f1bf..2b07aec 100755 --- a/main.py +++ b/main.py @@ -27,7 +27,7 @@ def add_job_summary() -> int: with open(GITHUB_STEP_SUMMARY, "a") as summary_file: summary_file.write("### Commit-Check ❌\n```\n") summary_file.write(result_text) - summary_file.write("\n```") + summary_file.write("```") return 1 else: with open(GITHUB_STEP_SUMMARY, "a") as summary_file: