|
| 1 | +#!/usr/bin/env python |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +class GitHubActionChecker: |
| 8 | + def __init__(self) -> None: |
| 9 | + # Pattern for actions with SHA-1 hashes (pinned) |
| 10 | + self.pinned_pattern = re.compile(r"uses:\s+([^@\s]+)@([a-f0-9]{40})") |
| 11 | + |
| 12 | + # Pattern for actions with version tags (unpinned) |
| 13 | + self.unpinned_pattern = re.compile( |
| 14 | + r"uses:\s+([^@\s]+)@(v\d+(?:\.\d+)*(?:-[a-zA-Z0-9]+(?:\.\d+)*)?)", |
| 15 | + ) |
| 16 | + |
| 17 | + # Pattern for all uses statements |
| 18 | + self.all_uses_pattern = re.compile(r"uses:\s+([^@\s]+)@([^\s\n]+)") |
| 19 | + |
| 20 | + def format_terminal_link(self, file_path: str, line_number: int) -> str: |
| 21 | + """Format a terminal link to a file and line number. |
| 22 | +
|
| 23 | + Args: |
| 24 | + file_path: Path to the file |
| 25 | + line_number: Line number in the file |
| 26 | +
|
| 27 | + Returns: |
| 28 | + str: Formatted string with file path and line number |
| 29 | + """ |
| 30 | + return f"{file_path}:{line_number}" |
| 31 | + |
| 32 | + def get_line_numbers(self, content: str, pattern: re.Pattern[str]) -> list[tuple[str, int]]: |
| 33 | + """Find matches with their line numbers.""" |
| 34 | + matches = [] |
| 35 | + matches.extend( |
| 36 | + (match.group(0), i) |
| 37 | + for i, line in enumerate(content.splitlines(), 1) |
| 38 | + for match in pattern.finditer(line) |
| 39 | + ) |
| 40 | + return matches |
| 41 | + |
| 42 | + def check_file(self, file_path: str) -> bool: |
| 43 | + """Check a single file for unpinned dependencies.""" |
| 44 | + try: |
| 45 | + content = Path(file_path).read_text() |
| 46 | + except (FileNotFoundError, PermissionError, IsADirectoryError, OSError) as e: |
| 47 | + print(f"\033[91mError reading file {file_path}: {e}\033[0m") |
| 48 | + return False |
| 49 | + |
| 50 | + # Get matches with line numbers |
| 51 | + pinned_matches = self.get_line_numbers(content, self.pinned_pattern) |
| 52 | + unpinned_matches = self.get_line_numbers(content, self.unpinned_pattern) |
| 53 | + all_matches = self.get_line_numbers(content, self.all_uses_pattern) |
| 54 | + |
| 55 | + print(f"\n\033[1m[=] Checking file: {file_path}\033[0m") |
| 56 | + |
| 57 | + # Print pinned dependencies |
| 58 | + if pinned_matches: |
| 59 | + print("\033[92m[+] Pinned:\033[0m") |
| 60 | + for match, line_num in pinned_matches: |
| 61 | + print(f" |- {match} \033[90m({file_path}:{line_num})\033[0m") |
| 62 | + |
| 63 | + # Track all found actions for validation |
| 64 | + found_actions = set() |
| 65 | + for match, _ in pinned_matches + unpinned_matches: |
| 66 | + action_name = self.pinned_pattern.match(match) or self.unpinned_pattern.match(match) |
| 67 | + if action_name: |
| 68 | + found_actions.add(action_name.group(1)) |
| 69 | + |
| 70 | + has_errors = False |
| 71 | + |
| 72 | + # Check for unpinned dependencies |
| 73 | + if unpinned_matches: |
| 74 | + has_errors = True |
| 75 | + print("\033[93m[!] Unpinned (using version tags):\033[0m") |
| 76 | + for match, line_num in unpinned_matches: |
| 77 | + print(f" |- {match} \033[90m({file_path}:{line_num})\033[0m") |
| 78 | + |
| 79 | + # Check for completely unpinned dependencies (no SHA or version) |
| 80 | + unpinned_without_hash = [ |
| 81 | + (match, line_num) |
| 82 | + for match, line_num in all_matches |
| 83 | + if not any(match in pinned[0] for pinned in pinned_matches) |
| 84 | + and not any(match in unpinned[0] for unpinned in unpinned_matches) |
| 85 | + ] |
| 86 | + |
| 87 | + if unpinned_without_hash: |
| 88 | + has_errors = True |
| 89 | + print("\033[91m[!] Completely unpinned (no SHA or version):\033[0m") |
| 90 | + for match, line_num in unpinned_without_hash: |
| 91 | + print( |
| 92 | + f" |- {match} \033[90m({self.format_terminal_link(file_path, line_num)})\033[0m", |
| 93 | + ) |
| 94 | + |
| 95 | + # Print summary |
| 96 | + total_actions = len(pinned_matches) + len(unpinned_matches) + len(unpinned_without_hash) |
| 97 | + if total_actions == 0: |
| 98 | + print("\033[93m[!] No GitHub Actions found in this file\033[0m") |
| 99 | + else: |
| 100 | + print("\n\033[1mSummary:\033[0m") |
| 101 | + print(f"Total actions: {total_actions}") |
| 102 | + print(f"Pinned: {len(pinned_matches)}") |
| 103 | + print(f"Unpinned with version: {len(unpinned_matches)}") |
| 104 | + print(f"Completely unpinned: {len(unpinned_without_hash)}") |
| 105 | + |
| 106 | + return not has_errors |
| 107 | + |
| 108 | + |
| 109 | +def main() -> None: |
| 110 | + checker = GitHubActionChecker() |
| 111 | + files_to_check = sys.argv[1:] |
| 112 | + |
| 113 | + if not files_to_check: |
| 114 | + print("\033[91mError: No files provided to check\033[0m") |
| 115 | + print("Usage: python script.py <file1> <file2> ...") |
| 116 | + sys.exit(1) |
| 117 | + |
| 118 | + results = {file: checker.check_file(file) for file in files_to_check} |
| 119 | + |
| 120 | + # Print final summary |
| 121 | + print("\n\033[1mFinal Results:\033[0m") |
| 122 | + for file, passed in results.items(): |
| 123 | + status = "\033[92m✓ Passed\033[0m" if passed else "\033[91m✗ Failed\033[0m" |
| 124 | + print(f"{status} {file}") |
| 125 | + |
| 126 | + if not all(results.values()): |
| 127 | + sys.exit(1) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments