|
3 | 3 | import sys
|
4 | 4 | import subprocess
|
5 | 5 | import re
|
| 6 | +from github import Github |
| 7 | + |
| 8 | + |
| 9 | +# Constants for message titles |
| 10 | +SUCCESS_TITLE = "# Commit-Check ✔️" |
| 11 | +FAILURE_TITLE = "# Commit-Check ❌" |
| 12 | + |
| 13 | +# Environment variables |
| 14 | +MESSAGE = os.getenv("MESSAGE", "false") |
| 15 | +BRANCH = os.getenv("BRANCH", "false") |
| 16 | +AUTHOR_NAME = os.getenv("AUTHOR_NAME", "false") |
| 17 | +AUTHOR_EMAIL = os.getenv("AUTHOR_EMAIL", "false") |
| 18 | +COMMIT_SIGNOFF = os.getenv("COMMIT_SIGNOFF", "false") |
| 19 | +DRY_RUN = os.getenv("DRY_RUN", "false") |
| 20 | +JOB_SUMMARY = os.getenv("JOB_SUMMARY", "false") |
| 21 | +PR_COMMENTS = os.getenv("PR_COMMENTS", "false") |
| 22 | +GITHUB_STEP_SUMMARY = os.environ["GITHUB_STEP_SUMMARY"] |
| 23 | +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") |
| 24 | +GITHUB_REPOSITORY = os.getenv("GITHUB_REPOSITORY") |
| 25 | +GITHUB_REF = os.getenv("GITHUB_REF") |
| 26 | + |
| 27 | + |
| 28 | +def log_env_vars(): |
| 29 | + """Logs the environment variables for debugging purposes.""" |
| 30 | + print(f"MESSAGE = {MESSAGE}") |
| 31 | + print(f"BRANCH = {BRANCH}") |
| 32 | + print(f"AUTHOR_NAME = {AUTHOR_NAME}") |
| 33 | + print(f"AUTHOR_EMAIL = {AUTHOR_EMAIL}") |
| 34 | + print(f"COMMIT_SIGNOFF = {COMMIT_SIGNOFF}") |
| 35 | + print(f"DRY_RUN = {DRY_RUN}") |
| 36 | + print(f"JOB_SUMMARY = {JOB_SUMMARY}") |
| 37 | + print(f"PR_COMMENTS = {PR_COMMENTS}\n") |
6 | 38 |
|
7 | 39 |
|
8 | 40 | def run_commit_check() -> int:
|
9 |
| - args = ["--message", "--branch", "--author-name", "--author-email", "--commit-signoff"] |
10 |
| - args = [arg for arg, value in zip(args, [MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF]) if value == "true"] |
| 41 | + """Runs the commit-check command and logs the result.""" |
| 42 | + args = [ |
| 43 | + "--message", |
| 44 | + "--branch", |
| 45 | + "--author-name", |
| 46 | + "--author-email", |
| 47 | + "--commit-signoff", |
| 48 | + ] |
| 49 | + args = [ |
| 50 | + arg |
| 51 | + for arg, value in zip( |
| 52 | + args, [MESSAGE, BRANCH, AUTHOR_NAME, AUTHOR_EMAIL, COMMIT_SIGNOFF] |
| 53 | + ) |
| 54 | + if value == "true" |
| 55 | + ] |
11 | 56 |
|
12 | 57 | command = ["commit-check"] + args
|
13 | 58 | print(" ".join(command))
|
14 | 59 | with open("result.txt", "w") as result_file:
|
15 |
| - result = subprocess.run(command, stdout=result_file, stderr=subprocess.PIPE, check=False) |
| 60 | + result = subprocess.run( |
| 61 | + command, stdout=result_file, stderr=subprocess.PIPE, check=False |
| 62 | + ) |
16 | 63 | return result.returncode
|
17 | 64 |
|
18 | 65 |
|
| 66 | +def read_result_file() -> str | None: |
| 67 | + """Reads the result.txt file and removes ANSI color codes.""" |
| 68 | + if os.path.getsize("result.txt") > 0: |
| 69 | + with open("result.txt", "r") as result_file: |
| 70 | + result_text = re.sub( |
| 71 | + r"\x1B\[[0-9;]*[a-zA-Z]", "", result_file.read() |
| 72 | + ) # Remove ANSI colors |
| 73 | + return result_text.rstrip() |
| 74 | + return None |
| 75 | + |
| 76 | + |
19 | 77 | def add_job_summary() -> int:
|
| 78 | + """Adds the commit check result to the GitHub job summary.""" |
20 | 79 | if JOB_SUMMARY == "false":
|
21 |
| - sys.exit() |
| 80 | + return 0 |
22 | 81 |
|
23 |
| - if os.path.getsize("result.txt") > 0: |
24 |
| - with open("result.txt", "r") as result_file: |
25 |
| - result_text = re.sub(r'\x1B\[[0-9;]*[a-zA-Z]', '', result_file.read()) # Remove ANSI colors |
| 82 | + result_text = read_result_file() |
26 | 83 |
|
27 |
| - with open(GITHUB_STEP_SUMMARY, "a") as summary_file: |
28 |
| - summary_file.write("### Commit-Check ❌\n```\n") |
29 |
| - summary_file.write(result_text) |
30 |
| - summary_file.write("```") |
31 |
| - return 1 |
32 |
| - else: |
33 |
| - with open(GITHUB_STEP_SUMMARY, "a") as summary_file: |
34 |
| - summary_file.write("### Commit-Check ✔️\n") |
| 84 | + summary_content = ( |
| 85 | + SUCCESS_TITLE |
| 86 | + if result_text is None |
| 87 | + else f"{FAILURE_TITLE}\n```\n{result_text}\n```" |
| 88 | + ) |
| 89 | + |
| 90 | + with open(GITHUB_STEP_SUMMARY, "a") as summary_file: |
| 91 | + summary_file.write(summary_content) |
| 92 | + |
| 93 | + return 0 if result_text is None else 1 |
| 94 | + |
| 95 | + |
| 96 | +def add_pr_comments() -> int: |
| 97 | + """Posts the commit check result as a comment on the pull request.""" |
| 98 | + if PR_COMMENTS == "false": |
35 | 99 | return 0
|
36 | 100 |
|
| 101 | + try: |
| 102 | + token = os.getenv("GITHUB_TOKEN") |
| 103 | + repo_name = os.getenv("GITHUB_REPOSITORY") |
| 104 | + pr_number = os.getenv("GITHUB_REF").split("/")[-2] |
37 | 105 |
|
38 |
| -MESSAGE = os.getenv("MESSAGE", "false") |
39 |
| -BRANCH = os.getenv("BRANCH", "false") |
40 |
| -AUTHOR_NAME = os.getenv("AUTHOR_NAME", "false") |
41 |
| -AUTHOR_EMAIL = os.getenv("AUTHOR_EMAIL", "false") |
42 |
| -COMMIT_SIGNOFF = os.getenv("COMMIT_SIGNOFF", "false") |
43 |
| -DRY_RUN = os.getenv("DRY_RUN", "false") |
44 |
| -JOB_SUMMARY = os.getenv("JOB_SUMMARY", "false") |
45 |
| -GITHUB_STEP_SUMMARY = os.environ["GITHUB_STEP_SUMMARY"] |
| 106 | + # Initialize GitHub client |
| 107 | + g = Github(token) |
| 108 | + repo = g.get_repo(repo_name) |
| 109 | + pull_request = repo.get_issue(int(pr_number)) |
| 110 | + |
| 111 | + # Prepare comment content |
| 112 | + result_text = read_result_file() |
| 113 | + pr_comments = ( |
| 114 | + SUCCESS_TITLE |
| 115 | + if result_text is None |
| 116 | + else f"{FAILURE_TITLE}\n```\n{result_text}\n```" |
| 117 | + ) |
| 118 | + |
| 119 | + # Fetch all existing comments on the PR |
| 120 | + comments = pull_request.get_comments() |
| 121 | + |
| 122 | + # Track if we found a matching comment |
| 123 | + matching_comments = [] |
| 124 | + last_comment = None |
| 125 | + |
| 126 | + for comment in comments: |
| 127 | + if comment.body.startswith(SUCCESS_TITLE) or comment.body.startswith( |
| 128 | + FAILURE_TITLE |
| 129 | + ): |
| 130 | + matching_comments.append(comment) |
| 131 | + if matching_comments: |
| 132 | + last_comment = matching_comments[-1] |
| 133 | + |
| 134 | + if last_comment.body == pr_comments: |
| 135 | + print(f"PR comment already up-to-date for PR #{pr_number}.") |
| 136 | + return 0 |
| 137 | + else: |
| 138 | + # If the last comment doesn't match, update it |
| 139 | + print(f"Updating the last comment on PR #{pr_number}.") |
| 140 | + last_comment.edit(pr_comments) |
| 141 | + |
| 142 | + # Delete all older matching comments |
| 143 | + for comment in matching_comments[:-1]: |
| 144 | + print(f"Deleting an old comment on PR #{pr_number}.") |
| 145 | + comment.delete() |
| 146 | + else: |
| 147 | + # No matching comments, create a new one |
| 148 | + print(f"Creating a new comment on PR #{pr_number}.") |
| 149 | + pull_request.create_comment(body=pr_comments) |
| 150 | + |
| 151 | + return 0 if result_text is None else 1 |
| 152 | + except Exception as e: |
| 153 | + print(f"Error posting PR comment: {e}", file=sys.stderr) |
| 154 | + return 1 |
| 155 | + |
| 156 | + |
| 157 | +def main(): |
| 158 | + """Main function to run commit-check, add job summary and post PR comments.""" |
| 159 | + log_env_vars() |
| 160 | + |
| 161 | + # Combine return codes |
| 162 | + ret_code = run_commit_check() |
| 163 | + ret_code += add_job_summary() |
| 164 | + ret_code += add_pr_comments() |
46 | 165 |
|
47 |
| -print(f"MESSAGE = {MESSAGE}") |
48 |
| -print(f"BRANCH = {BRANCH}") |
49 |
| -print(f"AUTHOR_NAME = {AUTHOR_NAME}") |
50 |
| -print(f"AUTHOR_EMAIL = {AUTHOR_EMAIL}") |
51 |
| -print(f"COMMIT_SIGNOFF = {COMMIT_SIGNOFF}") |
52 |
| -print(f"DRY_RUN = {DRY_RUN}") |
53 |
| -print(f"JOB_SUMMARY = {JOB_SUMMARY}\n") |
| 166 | + if DRY_RUN == "true": |
| 167 | + ret_code = 0 |
54 | 168 |
|
55 |
| -ret_code = run_commit_check() |
56 |
| -ret_code += add_job_summary() # Combine return codes |
| 169 | + sys.exit(ret_code) |
57 | 170 |
|
58 |
| -if DRY_RUN == "true": |
59 |
| - ret_code = 0 |
60 | 171 |
|
61 |
| -sys.exit(ret_code) |
| 172 | +if __name__ == "__main__": |
| 173 | + main() |
0 commit comments