|
| 1 | +import click |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from codesage.semantic_digest.python_snapshot_builder import PythonSemanticSnapshotBuilder, SnapshotConfig |
| 8 | +from codesage.semantic_digest.go_snapshot_builder import GoSemanticSnapshotBuilder |
| 9 | +from codesage.semantic_digest.shell_snapshot_builder import ShellSemanticSnapshotBuilder |
| 10 | +from codesage.snapshot.models import ProjectSnapshot |
| 11 | +from codesage.reporters import ConsoleReporter, JsonReporter, GitHubPRReporter |
| 12 | + |
| 13 | +def get_builder(language: str, path: Path): |
| 14 | + config = SnapshotConfig() |
| 15 | + if language == 'python': |
| 16 | + return PythonSemanticSnapshotBuilder(path, config) |
| 17 | + elif language == 'go': |
| 18 | + return GoSemanticSnapshotBuilder(path, config) |
| 19 | + elif language == 'shell': |
| 20 | + return ShellSemanticSnapshotBuilder(path, config) |
| 21 | + else: |
| 22 | + return None |
| 23 | + |
| 24 | +@click.command('scan') |
| 25 | +@click.argument('path', type=click.Path(exists=True, dir_okay=True)) |
| 26 | +@click.option('--language', '-l', type=click.Choice(['python', 'go', 'shell']), default='python', help='Language to analyze.') |
| 27 | +@click.option('--reporter', '-r', type=click.Choice(['console', 'json', 'github']), default='console', help='Reporter to use.') |
| 28 | +@click.option('--output', '-o', help='Output path for JSON reporter.') |
| 29 | +@click.option('--fail-on-high', is_flag=True, help='Exit with non-zero code if high severity issues are found.') |
| 30 | +@click.option('--ci-mode', is_flag=True, help='Enable CI mode (auto-detect GitHub environment).') |
| 31 | +@click.pass_context |
| 32 | +def scan(ctx, path, language, reporter, output, fail_on_high, ci_mode): |
| 33 | + """ |
| 34 | + Scan the codebase and report issues. |
| 35 | + """ |
| 36 | + click.echo(f"Scanning {path} for {language}...") |
| 37 | + |
| 38 | + root_path = Path(path) |
| 39 | + builder = get_builder(language, root_path) |
| 40 | + |
| 41 | + if not builder: |
| 42 | + click.echo(f"Unsupported language: {language}", err=True) |
| 43 | + ctx.exit(1) |
| 44 | + |
| 45 | + try: |
| 46 | + snapshot: ProjectSnapshot = builder.build() |
| 47 | + except Exception as e: |
| 48 | + click.echo(f"Scan failed: {e}", err=True) |
| 49 | + ctx.exit(1) |
| 50 | + |
| 51 | + # Select Reporter |
| 52 | + reporters = [] |
| 53 | + |
| 54 | + # Always add console reporter unless we are in json mode only? |
| 55 | + # Usually CI logs want console output too. |
| 56 | + if reporter == 'console': |
| 57 | + reporters.append(ConsoleReporter()) |
| 58 | + elif reporter == 'json': |
| 59 | + out_path = output or "codesage_report.json" |
| 60 | + reporters.append(JsonReporter(output_path=out_path)) |
| 61 | + elif reporter == 'github': |
| 62 | + reporters.append(ConsoleReporter()) # Still print to console |
| 63 | + |
| 64 | + # Check environment |
| 65 | + token = os.environ.get("GITHUB_TOKEN") |
| 66 | + repo = os.environ.get("GITHUB_REPOSITORY") |
| 67 | + |
| 68 | + # Try to get PR number |
| 69 | + pr_number = None |
| 70 | + ref = os.environ.get("GITHUB_REF") # refs/pull/123/merge |
| 71 | + if ref and "pull" in ref: |
| 72 | + try: |
| 73 | + pr_number = int(ref.split("/")[2]) |
| 74 | + except (IndexError, ValueError): |
| 75 | + pass |
| 76 | + |
| 77 | + # Or from event.json |
| 78 | + event_path = os.environ.get("GITHUB_EVENT_PATH") |
| 79 | + if not pr_number and event_path and os.path.exists(event_path): |
| 80 | + import json |
| 81 | + try: |
| 82 | + with open(event_path) as f: |
| 83 | + event = json.load(f) |
| 84 | + pr_number = event.get("pull_request", {}).get("number") |
| 85 | + except Exception: |
| 86 | + pass |
| 87 | + |
| 88 | + if token and repo and pr_number: |
| 89 | + reporters.append(GitHubPRReporter(token=token, repo=repo, pr_number=pr_number)) |
| 90 | + else: |
| 91 | + click.echo("GitHub reporter selected but missing environment variables (GITHUB_TOKEN, GITHUB_REPOSITORY) or not in a PR context.", err=True) |
| 92 | + |
| 93 | + # CI Mode overrides |
| 94 | + if ci_mode and os.environ.get("GITHUB_ACTIONS") == "true": |
| 95 | + # In CI mode, we might force certain reporters or behavior |
| 96 | + pass |
| 97 | + |
| 98 | + # Execute Reporters |
| 99 | + for r in reporters: |
| 100 | + r.report(snapshot) |
| 101 | + |
| 102 | + # Check Fail Condition |
| 103 | + if fail_on_high: |
| 104 | + has_high_risk = False |
| 105 | + if snapshot.issues_summary: |
| 106 | + if snapshot.issues_summary.by_severity.get('high', 0) > 0 or \ |
| 107 | + snapshot.issues_summary.by_severity.get('error', 0) > 0: |
| 108 | + has_high_risk = True |
| 109 | + |
| 110 | + # Also check risk summary if issues are not populated but risk is |
| 111 | + if snapshot.risk_summary and snapshot.risk_summary.high_risk_files > 0: |
| 112 | + has_high_risk = True |
| 113 | + |
| 114 | + if has_high_risk: |
| 115 | + click.echo("Failure: High risk issues detected.", err=True) |
| 116 | + ctx.exit(1) |
| 117 | + |
| 118 | + click.echo("Scan finished successfully.") |
0 commit comments