Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/tests/commit_msg_hook_contract_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from pathlib import Path
import subprocess
import tempfile
import unittest


ROOT = Path(__file__).resolve().parents[2]
HOOK = ROOT / "scripts" / "check-commit-msg.sh"

ACCEPTED = [
"feat(scope): x",
"fix!: x",
"revert: x",
"chore(deps/a.b-c): x",
"Merge branch 'x'",
"Merge pull request #1 from a/b",
"Merge remote-tracking branch 'o/x'",
"fixup! feat: x",
"squash! feat: x",
'Revert "feat: x"',
]

REJECTED = [
"feat: ",
"Feat: x",
"wip: x",
"feat(Bad Scope): x",
"feat x",
"feat:x",
]


def run_hook(message):
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "COMMIT_EDITMSG"
path.write_text(message, encoding="utf-8")
return subprocess.run(
["bash", str(HOOK), str(path)], capture_output=True, text=True
)


class CommitMsgHookContractTest(unittest.TestCase):
def test_accepts_valid_subjects(self):
for subject in ACCEPTED:
with self.subTest(subject=subject):
result = run_hook(subject + "\n")
self.assertEqual(0, result.returncode, result.stderr)

def test_rejects_invalid_subjects(self):
for subject in REJECTED:
with self.subTest(subject=subject):
result = run_hook(subject + "\n")
self.assertEqual(1, result.returncode)
self.assertIn(subject, result.stderr)
self.assertIn("plain Conventional Commits", result.stderr)

def test_only_first_line_is_checked(self):
result = run_hook("feat: x\n\nthis body is not a conventional subject\nwip: junk\n")
self.assertEqual(0, result.returncode, result.stderr)


if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletions .github/tests/community_health_contract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,24 @@ def test_zizmor_action_owns_its_supported_cli_version(self):

self.assertNotIn("\n version:", zizmor_step)

def test_actionlint_download_retries_and_is_bounded(self):
workflow = self.read_text(".github/workflows/standards-validation.yml")
curl_line = next(
line for line in workflow.splitlines()
if "curl" in line and "actionlint_1.7.12" in line
)

for flag in (
"--proto =https",
"--tlsv1.2",
"--retry 3",
"--retry-all-errors",
"--connect-timeout 10",
"--max-time 120",
):
self.assertIn(flag, curl_line)
self.assertIn("sha256", workflow)

def test_linter_configs_do_not_suppress_workflow_findings(self):
markdown_config = self.read_text(".github/markdownlint-profile.yaml")

Expand Down
97 changes: 97 additions & 0 deletions .github/tests/greptile_summon_contract_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from pathlib import Path
import json
import os
import re
import shutil
import subprocess
import tempfile
import textwrap
import unittest


Expand Down Expand Up @@ -83,6 +89,97 @@ def test_workflow_pins_actions_and_is_run_by_standards_validation(self):
).read_text()
self.assertIn("greptile_summon_contract_test.py", validation)

def test_summon_script_behavior(self):
node = shutil.which("node")
if node is None:
self.skipTest("node is not available")

sha = "a" * 40
other = "b" * 40
marker = f"<!-- greptile-summon:{sha} -->"

def pull(head, state="open"):
return {"data": {"state": state, "head": {"sha": head}}}

bot_comment = {"user": {"login": "github-actions[bot]"}, "body": marker}
cases = (
("abc", [], [], 1, 0, 0),
("1x", [], [], 1, 0, 0),
("7", [pull(sha, "closed")], [], 1, 0, 0),
("7", [pull(other)], [], 1, 0, 0),
("7", [pull(sha)], [bot_comment], 0, 0, 1),
("7", [pull(sha), pull(other)], [], 1, 0, 0),
("7", [pull(sha), pull(sha)], [], 0, 1, 0),
)

body = self.script_body(self.read_workflow())
harness = textwrap.dedent(
"""\
const body = %s;
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
const run = new AsyncFunction("github", "core", "context", "process", body);
const scenario = JSON.parse(process.argv[2]);
const calls = { setFailed: [], notice: [], createComment: [] };
const queue = [...scenario.pulls];
const listComments = () => {};
const github = {
rest: {
pulls: { get: async () => queue.shift() },
issues: {
listComments,
createComment: async (args) => { calls.createComment.push(args); },
},
},
paginate: async () => scenario.comments,
};
const core = {
setFailed: (m) => calls.setFailed.push(m),
notice: (m) => calls.notice.push(m),
};
const context = { repo: { owner: "o", repo: "r" } };
run(github, core, context, process).then(
() => console.log(JSON.stringify(calls)),
(e) => { console.error(e); process.exit(1); },
);
"""
) % json.dumps(body)

with tempfile.TemporaryDirectory() as tmp:
harness_path = Path(tmp) / "harness.js"
harness_path.write_text(harness)
for pr_number, pulls, comments, failed, created, notices in cases:
with self.subTest(pr_number=pr_number, pulls=pulls):
env = {
**os.environ,
"PR_NUMBER": pr_number,
"HEAD_SHA": sha,
"REVIEW_FOCUS": "check the diff",
}
result = subprocess.run(
[node, str(harness_path), json.dumps({"pulls": pulls, "comments": comments})],
check=True,
capture_output=True,
text=True,
env=env,
)
calls = json.loads(result.stdout)
self.assertEqual(len(calls["setFailed"]), failed, calls)
self.assertEqual(len(calls["createComment"]), created, calls)
self.assertEqual(len(calls["notice"]), notices, calls)
if created:
comment_body = calls["createComment"][0]["body"]
self.assertIn("@greptileai", comment_body)
self.assertIn(marker, comment_body)

def script_body(self, workflow):
lines = workflow.split(" script: |\n", 1)[1].splitlines()
body = []
for line in lines:
if line.strip() and not line.startswith(" " * 12):
break
body.append(line[12:])
return "\n".join(body)

def read_workflow(self):
self.assertTrue(WORKFLOW.is_file(), f"missing workflow: {WORKFLOW}")
return WORKFLOW.read_text()
Expand Down
Loading
Loading