mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
feat(update): label every update PR update plus major, minor or patch
utils/update/level.py reads the update commit with git diff -U0 and returns the highest step among the versions it rewrites: the first differing number decides major, minor or patch, and a step that is no number change (a commit SHA, a SHA moving to a tag) counts as major. scripts/github/update/open/pr.sh computes the level after its commit, makes sure the four labels exist through python -m cli.contributing.update.label --ensure-only, and sets update plus the level on the PR it creates or edits. The label module is stdlib only, like level.py, because the update jobs install no dependencies. 🎯 Fix: 88 % 💥 Break: 5 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 84 🚦 Gate: ⛔ fix 88 % < 90 MEDUSA accepted false positives: B603 cli/contributing/update/label/__main__.py and B603 utils/update/level.py (list-form gh and git subprocesses, no shell); MEDUSA-AI-BACKDOOR-006 utils/update/level.py (unified-diff hunk header parsing). Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
023708b40b
commit
5c6b274d52
9 files changed
+543
-2
No files matched your search
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,137 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import subprocess
|
||||
import unittest
|
||||
from contextlib import redirect_stdout
|
||||
from unittest import mock
|
||||
|
||||
import cli.contributing.update.label.__main__ as label_main
|
||||
|
||||
REPO = "infinito-nexus/core"
|
||||
|
||||
|
||||
class FakeGh:
|
||||
"""Answers the label reads and records every write.
|
||||
|
||||
Args:
|
||||
repository: label names the repository has.
|
||||
pull_request: label names pull request 4 carries.
|
||||
racing: names whose create fails because a parallel run made them.
|
||||
"""
|
||||
|
||||
def __init__(self, repository, pull_request, racing=()):
|
||||
self.repository = set(repository)
|
||||
self.pull_request = set(pull_request)
|
||||
self.racing = set(racing)
|
||||
self.writes = []
|
||||
|
||||
def __call__(self, args):
|
||||
if args[:2] == ["api", "--paginate"]:
|
||||
names = self.pull_request if "/issues/" in args[2] else self.repository
|
||||
return "".join(f"{name}\n" for name in sorted(names))
|
||||
self.writes.append(list(args))
|
||||
if args[:2] == ["label", "create"] and args[2] in self.racing:
|
||||
self.repository.add(args[2])
|
||||
raise subprocess.CalledProcessError(1, ["gh", *args])
|
||||
return ""
|
||||
|
||||
|
||||
class TestLevel(unittest.TestCase):
|
||||
def test_semver_levels_map_to_their_label(self):
|
||||
self.assertEqual(
|
||||
[
|
||||
label_main.level(f"version-update:semver-{name}").name
|
||||
for name in ("major", "minor", "patch")
|
||||
],
|
||||
["major", "minor", "patch"],
|
||||
)
|
||||
|
||||
def test_an_unknown_or_empty_type_counts_as_major(self):
|
||||
self.assertEqual(
|
||||
{label_main.level(kind).name for kind in ("", "security", "semver-minor")},
|
||||
{"major"},
|
||||
)
|
||||
|
||||
|
||||
class TestSync(unittest.TestCase):
|
||||
def test_a_bare_pull_request_gets_update_and_its_level(self):
|
||||
gh = FakeGh({"update", "minor"}, set())
|
||||
actions = label_main.sync(gh, REPO, 4, "version-update:semver-minor")
|
||||
self.assertEqual(actions, ["added update, minor"])
|
||||
self.assertEqual(
|
||||
gh.writes,
|
||||
[
|
||||
[
|
||||
"api",
|
||||
"--method",
|
||||
"POST",
|
||||
f"repos/{REPO}/issues/4/labels",
|
||||
"-f",
|
||||
"labels[]=update",
|
||||
"-f",
|
||||
"labels[]=minor",
|
||||
]
|
||||
],
|
||||
)
|
||||
|
||||
def test_labels_already_present_cause_no_write(self):
|
||||
gh = FakeGh({"update", "patch"}, {"update", "patch", "dependencies"})
|
||||
self.assertEqual(
|
||||
label_main.sync(gh, REPO, 4, "version-update:semver-patch"), []
|
||||
)
|
||||
self.assertEqual(gh.writes, [])
|
||||
|
||||
def test_a_stale_level_is_removed(self):
|
||||
gh = FakeGh({"update", "major", "minor"}, {"update", "minor"})
|
||||
actions = label_main.sync(gh, REPO, 4, "version-update:semver-major")
|
||||
self.assertEqual(actions, ["removed minor", "added major"])
|
||||
|
||||
def test_missing_repository_labels_are_created_first(self):
|
||||
gh = FakeGh(set(), set())
|
||||
label_main.sync(gh, REPO, 4, "version-update:semver-patch")
|
||||
created = [write[2] for write in gh.writes if write[:2] == ["label", "create"]]
|
||||
self.assertEqual(created, ["update", "patch"])
|
||||
|
||||
def test_a_label_a_parallel_run_created_counts_as_done(self):
|
||||
gh = FakeGh({"patch"}, set(), racing={"update"})
|
||||
actions = label_main.sync(gh, REPO, 4, "version-update:semver-patch")
|
||||
self.assertEqual(actions, ["added update, patch"])
|
||||
|
||||
def test_a_create_failure_that_leaves_the_label_missing_raises(self):
|
||||
gh = FakeGh(set(), set())
|
||||
|
||||
def failing(args):
|
||||
if args[:2] == ["label", "create"]:
|
||||
raise subprocess.CalledProcessError(1, ["gh", *args])
|
||||
return gh(args)
|
||||
|
||||
with self.assertRaises(subprocess.CalledProcessError):
|
||||
label_main.sync(failing, REPO, 4, "")
|
||||
|
||||
|
||||
class TestMain(unittest.TestCase):
|
||||
def test_main_reports_when_nothing_changes(self):
|
||||
gh = FakeGh({"update", "major"}, {"update", "major"})
|
||||
out = io.StringIO()
|
||||
with mock.patch.object(label_main, "run_gh", gh), redirect_stdout(out):
|
||||
code = label_main.main(["--repo", REPO, "--pr", "4", "--update-type", ""])
|
||||
self.assertEqual((code, out.getvalue()), (0, "#4 already carries its labels\n"))
|
||||
|
||||
def test_ensure_only_creates_the_missing_labels_and_touches_no_pr(self):
|
||||
gh = FakeGh({"update", "minor"}, set())
|
||||
out = io.StringIO()
|
||||
with mock.patch.object(label_main, "run_gh", gh), redirect_stdout(out):
|
||||
code = label_main.main(["--repo", REPO, "--ensure-only"])
|
||||
created = [w[2] for w in gh.writes if w[:2] == ["label", "create"]]
|
||||
self.assertEqual((code, created), (0, ["major", "patch"]))
|
||||
self.assertEqual(len(gh.writes), 2)
|
||||
|
||||
def test_without_ensure_only_the_pr_and_type_are_required(self):
|
||||
with self.assertRaises(SystemExit) as raised, redirect_stdout(io.StringIO()):
|
||||
label_main.main(["--repo", REPO, "--pr", "4"])
|
||||
self.assertEqual(raised.exception.code, 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user