Skip to content

Commit

Permalink
Fix upload command
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Feb 10, 2024
1 parent 4e78e42 commit 624b79f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
default_language_version:
python: python3.11

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand All @@ -17,6 +20,6 @@ repos:
hooks:
- id: mypy
- repo: https://github.com/getcodelimit/codelimit
rev: v0.5.0
rev: v0.7.0
hooks:
- id: codelimit
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ and any contributions are appreciated.

To show your project uses Code Limit place this badge in the README markdown:

[![Checked with Code Limit](https://img.shields.io/badge/CodeLimit-checked-green.svg)](https://github.com/getcodelimit/codelimit)
[![Checked with Code Limit](https://img.shields.io/badge/Code%20Limit-checked-green.svg)](https://github.com/getcodelimit/codelimit)

```
![Checked with Code Limit](https://img.shields.io/badge/CodeLimit-checked-green.svg)](https://github.com/getcodelimit/codelimit)
![Checked with Code Limit](https://img.shields.io/badge/Code%20Limit-checked-green.svg)](https://github.com/getcodelimit/codelimit)
```

# License
Expand Down
24 changes: 20 additions & 4 deletions codelimit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from codelimit.common.report.ReportReader import ReportReader
from codelimit.common.report.ReportWriter import ReportWriter
from codelimit.tui.CodeLimitApp import CodeLimitApp
from codelimit.utils import upload_report, check_file
from codelimit.utils import upload_report, check_file, read_cached_report

cli = typer.Typer(no_args_is_help=True, add_completion=False)

Expand Down Expand Up @@ -73,18 +73,34 @@ def scan(
app.run()


@cli.command(help="Upload report to CodeLimit server", hidden=True)
@cli.command(help="Upload report to Code Limit")
def upload(
report_path: Path = typer.Argument(help="JSON report for a code base"),
report_file: Path = typer.Option(
None,
"--report",
exists=True,
dir_okay=False,
file_okay=True,
help="JSON report file",
),
url: str = typer.Option(
"https://codelimit-web.vercel.app/api/upload",
"--url",
"-u",
help="Upload JSON report to this URL.",
),
):
if report_file:
report = ReportReader.from_json(report_file.read_text())
else:
cached_report = read_cached_report(Path("."))
if not cached_report:
typer.secho("No cached report found in current folder", fg="red")
raise typer.Exit(code=1)
else:
report = cached_report
try:
upload_report(report_path, url)
upload_report(report, url)
raise typer.Exit(code=0)
except FileNotFoundError as error:
typer.secho(f"File not found: {error}", fg="red")
Expand Down
28 changes: 19 additions & 9 deletions codelimit/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from pathlib import Path
from typing import Optional

import requests # type: ignore
import typer
from rich.progress import Progress, SpinnerColumn, TextColumn

from codelimit.common.CheckResult import CheckResult
from codelimit.common.Scanner import scan_file, languages
from codelimit.common.report.Report import Report
from codelimit.common.report.ReportReader import ReportReader
from codelimit.common.report.ReportWriter import ReportWriter


def check_file(path: Path, check_result: CheckResult):
Expand All @@ -20,28 +24,34 @@ def check_file(path: Path, check_result: CheckResult):
check_result.add(path, risks)


def upload_report(path: Path, url: str) -> None:
def read_cached_report(path: Path) -> Optional[Report]:
cache_dir = path.joinpath(".codelimit_cache").resolve()
report_path = cache_dir.joinpath("codelimit.json").resolve()
if report_path.exists():
return ReportReader.from_json(report_path.read_text())
else:
return None


def upload_report(report: Report, url: str) -> None:
data_template = (
'{{"repository": "getcodelimit/codelimit", "branch": "main", "report":{}}}'
)

if not path.exists():
raise FileNotFoundError(str(path))

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description=f"Uploading {path.name} to {url}", total=None)
progress.add_task(description=f"Uploading report to {url}", total=None)
result = requests.post(
url,
data=data_template.format(path.read_text()),
data=data_template.format(
ReportWriter(report, pretty_print=False).to_json()
),
headers={"Content-Type": "application/json"},
)

if result.ok:
typer.secho("Uploaded", fg="green")
typer.secho("Upload successful!", fg="green")
else:
typer.secho(f"Upload unsuccessful: {result.status_code}", fg="red")
raise typer.Exit(code=1)

0 comments on commit 624b79f

Please sign in to comment.