openfix-cli 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openfix/__init__.py +3 -0
- openfix/agent/__init__.py +4 -0
- openfix/agent/loop.py +105 -0
- openfix/agent/prompts.py +76 -0
- openfix/agent/tools.py +346 -0
- openfix/cli.py +211 -0
- openfix/console.py +68 -0
- openfix/deps.py +112 -0
- openfix/engine.py +477 -0
- openfix/findings.py +140 -0
- openfix/github.py +119 -0
- openfix/gitutil.py +93 -0
- openfix/ingest.py +512 -0
- openfix/policy.py +310 -0
- openfix/pricing.py +40 -0
- openfix/providers/__init__.py +104 -0
- openfix/providers/anthropic.py +114 -0
- openfix/providers/base.py +86 -0
- openfix/providers/gemini.py +82 -0
- openfix/providers/openai_compat.py +134 -0
- openfix/providers/scripted.py +75 -0
- openfix/receipt.py +143 -0
- openfix/redact.py +47 -0
- openfix/report.py +92 -0
- openfix/sandbox.py +230 -0
- openfix/sarif_out.py +74 -0
- openfix/scanners.py +187 -0
- openfix/verify.py +251 -0
- openfix_cli-0.4.0.dist-info/METADATA +322 -0
- openfix_cli-0.4.0.dist-info/RECORD +33 -0
- openfix_cli-0.4.0.dist-info/WHEEL +4 -0
- openfix_cli-0.4.0.dist-info/entry_points.txt +2 -0
- openfix_cli-0.4.0.dist-info/licenses/LICENSE +21 -0
openfix/github.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Open a pull request for a verified fix. OpenFix never merges."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import httpx
|
|
11
|
+
|
|
12
|
+
from .gitutil import git
|
|
13
|
+
|
|
14
|
+
API = os.environ.get("GITHUB_API_URL", "https://api.github.com")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class PublishError(RuntimeError):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def repo_slug(repo: Path) -> str:
|
|
22
|
+
if os.environ.get("GITHUB_REPOSITORY"):
|
|
23
|
+
return os.environ["GITHUB_REPOSITORY"]
|
|
24
|
+
url = git(repo, "remote", "get-url", "origin", check=False).strip()
|
|
25
|
+
m = re.search(r"github\.com[:/]([^/]+/[^/]+?)(?:\.git)?$", url)
|
|
26
|
+
if not m:
|
|
27
|
+
raise PublishError(f"Cannot determine the GitHub repository from origin '{url}'. Set GITHUB_REPOSITORY.")
|
|
28
|
+
return m.group(1)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _token() -> str:
|
|
32
|
+
tok = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
|
|
33
|
+
if not tok:
|
|
34
|
+
raise PublishError("Set GITHUB_TOKEN (contents:write, pull-requests:write) to open pull requests.")
|
|
35
|
+
return tok
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _client(token: str) -> httpx.Client:
|
|
39
|
+
return httpx.Client(
|
|
40
|
+
base_url=API,
|
|
41
|
+
timeout=60,
|
|
42
|
+
headers={
|
|
43
|
+
"Authorization": f"Bearer {token}",
|
|
44
|
+
"Accept": "application/vnd.github+json",
|
|
45
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
46
|
+
"User-Agent": "openfix",
|
|
47
|
+
},
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def default_branch(slug: str, token: str) -> str:
|
|
52
|
+
if os.environ.get("GITHUB_BASE_REF"):
|
|
53
|
+
return os.environ["GITHUB_BASE_REF"]
|
|
54
|
+
with _client(token) as c:
|
|
55
|
+
r = c.get(f"/repos/{slug}")
|
|
56
|
+
r.raise_for_status()
|
|
57
|
+
return r.json()["default_branch"]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def open_pull_request(
|
|
61
|
+
work: Path,
|
|
62
|
+
*,
|
|
63
|
+
branch: str,
|
|
64
|
+
title: str,
|
|
65
|
+
body: str,
|
|
66
|
+
commit_message: str,
|
|
67
|
+
labels: list[str],
|
|
68
|
+
draft: bool,
|
|
69
|
+
base_branch: str | None = None,
|
|
70
|
+
) -> str:
|
|
71
|
+
token = _token()
|
|
72
|
+
slug = repo_slug(work)
|
|
73
|
+
base_branch = base_branch or default_branch(slug, token)
|
|
74
|
+
# -C: a rerun reuses the branch name; the worktree is disposable and the branch is OpenFix's own.
|
|
75
|
+
git(work, "switch", "-C", branch)
|
|
76
|
+
git(work, "add", "-A")
|
|
77
|
+
git(
|
|
78
|
+
work,
|
|
79
|
+
"-c",
|
|
80
|
+
"user.name=openfix[bot]",
|
|
81
|
+
"-c",
|
|
82
|
+
"user.email=openfix-bot@users.noreply.github.com",
|
|
83
|
+
"commit",
|
|
84
|
+
"--no-verify",
|
|
85
|
+
"-m",
|
|
86
|
+
commit_message,
|
|
87
|
+
)
|
|
88
|
+
auth = base64.b64encode(f"x-access-token:{token}".encode()).decode()
|
|
89
|
+
git(
|
|
90
|
+
work,
|
|
91
|
+
"-c",
|
|
92
|
+
f"http.https://github.com/.extraheader=AUTHORIZATION: basic {auth}",
|
|
93
|
+
"push",
|
|
94
|
+
"--force-with-lease",
|
|
95
|
+
"origin",
|
|
96
|
+
f"HEAD:refs/heads/{branch}",
|
|
97
|
+
)
|
|
98
|
+
with _client(token) as c:
|
|
99
|
+
r = c.post(
|
|
100
|
+
f"/repos/{slug}/pulls",
|
|
101
|
+
json={
|
|
102
|
+
"title": title,
|
|
103
|
+
"head": branch,
|
|
104
|
+
"base": base_branch,
|
|
105
|
+
"body": body,
|
|
106
|
+
"draft": draft,
|
|
107
|
+
"maintainer_can_modify": True,
|
|
108
|
+
},
|
|
109
|
+
)
|
|
110
|
+
if r.status_code == 422 and "already exists" in r.text:
|
|
111
|
+
existing = c.get(f"/repos/{slug}/pulls", params={"head": f"{slug.split('/')[0]}:{branch}"}).json()
|
|
112
|
+
if existing:
|
|
113
|
+
return existing[0]["html_url"]
|
|
114
|
+
if r.status_code >= 300:
|
|
115
|
+
raise PublishError(f"GitHub refused the pull request ({r.status_code}): {r.text[:300]}")
|
|
116
|
+
pr = r.json()
|
|
117
|
+
if labels:
|
|
118
|
+
c.post(f"/repos/{slug}/issues/{pr['number']}/labels", json={"labels": labels})
|
|
119
|
+
return pr["html_url"]
|
openfix/gitutil.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Thin, hardened wrappers around git."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import subprocess
|
|
7
|
+
import tempfile
|
|
8
|
+
from collections.abc import Iterator
|
|
9
|
+
from contextlib import contextmanager, suppress
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class GitError(RuntimeError):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def git(repo: Path | str, *args: str, check: bool = True, input: str | None = None) -> str:
|
|
18
|
+
env = {k: v for k, v in os.environ.items() if k in ("PATH", "HOME", "LANG", "TMPDIR", "SSH_AUTH_SOCK")}
|
|
19
|
+
env.update({"GIT_TERMINAL_PROMPT": "0", "GIT_CONFIG_NOSYSTEM": "1", "LC_ALL": "C"})
|
|
20
|
+
res = subprocess.run(
|
|
21
|
+
["git", "-C", str(repo), "-c", "core.hooksPath=/dev/null", *args],
|
|
22
|
+
capture_output=True,
|
|
23
|
+
text=True,
|
|
24
|
+
env=env,
|
|
25
|
+
input=input,
|
|
26
|
+
timeout=300,
|
|
27
|
+
)
|
|
28
|
+
if check and res.returncode != 0:
|
|
29
|
+
raise GitError(f"git {' '.join(args)} failed: {res.stderr.strip() or res.stdout.strip()}")
|
|
30
|
+
return res.stdout
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def repo_root(path: Path | str) -> Path:
|
|
34
|
+
return Path(git(path, "rev-parse", "--show-toplevel").strip())
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def head_commit(repo: Path | str) -> str:
|
|
38
|
+
return git(repo, "rev-parse", "HEAD").strip()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def is_dirty(repo: Path | str) -> bool:
|
|
42
|
+
return bool(git(repo, "status", "--porcelain", "--untracked-files=no").strip())
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@contextmanager
|
|
46
|
+
def worktree(repo: Path | str, commit: str, keep: bool = False) -> Iterator[Path]:
|
|
47
|
+
"""A detached, disposable checkout of ``commit``. The user's working tree is never touched."""
|
|
48
|
+
base = Path(tempfile.mkdtemp(prefix="openfix-wt-"))
|
|
49
|
+
path = base / "repo"
|
|
50
|
+
git(repo, "worktree", "add", "--detach", "--force", str(path), commit)
|
|
51
|
+
try:
|
|
52
|
+
yield path
|
|
53
|
+
finally:
|
|
54
|
+
if not keep:
|
|
55
|
+
git(repo, "worktree", "remove", "--force", str(path), check=False)
|
|
56
|
+
git(repo, "worktree", "prune", check=False)
|
|
57
|
+
with suppress(OSError):
|
|
58
|
+
base.rmdir()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def diff_against(work: Path, base: str) -> str:
|
|
62
|
+
"""Unified diff of every change (including new files) in ``work`` relative to ``base``."""
|
|
63
|
+
git(work, "add", "-A")
|
|
64
|
+
return git(work, "diff", "--cached", "--binary", "--no-color", "--no-ext-diff", base)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def changed_files(work: Path, base: str) -> list[tuple[str, int, int]]:
|
|
68
|
+
git(work, "add", "-A")
|
|
69
|
+
rows = []
|
|
70
|
+
# --relative: paths relative to (and limited to) ``work`` when it is a subdirectory of the checkout.
|
|
71
|
+
for line in git(work, "diff", "--cached", "--numstat", "--relative", base).splitlines():
|
|
72
|
+
added, removed, path = line.split("\t", 2)
|
|
73
|
+
rows.append((path, int(added) if added.isdigit() else 0, int(removed) if removed.isdigit() else 0))
|
|
74
|
+
return rows
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def file_at(repo: Path, commit: str, path: str) -> str | None:
|
|
78
|
+
try:
|
|
79
|
+
return git(repo, "show", f"{commit}:{path}")
|
|
80
|
+
except GitError:
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def exclude_locally(work: Path, pattern: str) -> None:
|
|
85
|
+
"""Add an ignore pattern for this checkout only (never committed)."""
|
|
86
|
+
common = Path(git(work, "rev-parse", "--path-format=absolute", "--git-common-dir").strip()).resolve()
|
|
87
|
+
exclude = common / "info" / "exclude"
|
|
88
|
+
if not (common / "HEAD").is_file():
|
|
89
|
+
raise GitError(f"unexpected git directory {common}")
|
|
90
|
+
exclude.parent.mkdir(parents=True, exist_ok=True)
|
|
91
|
+
existing = exclude.read_text() if exclude.exists() else ""
|
|
92
|
+
if pattern not in existing.splitlines():
|
|
93
|
+
exclude.write_text(existing + ("" if existing.endswith("\n") or not existing else "\n") + pattern + "\n")
|