Skip to content

Commit 3152192

Browse files
cosmo0920edsiper
authored andcommitted
build: Detect version bump to permit "release: " prefix
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 680f14c commit 3152192

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

.github/scripts/commit_prefix_check.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,47 @@ def detect_bad_squash(body):
178178
# Validate commit based on expected behavior and test rules
179179
# ------------------------------------------------
180180
def validate_commit(commit):
181+
VERSION_PATTERN = re.compile(
182+
r"set\(FLB_VERSION_(MAJOR|MINOR|PATCH)\s+\d+\)"
183+
)
184+
185+
def is_version_bump(commit):
186+
if not commit.parents:
187+
return False
188+
189+
diffs = commit.diff(commit.parents[0], create_patch=True)
190+
found_version_change = False
191+
saw_cmakelists = False
192+
193+
for d in diffs:
194+
path = (d.b_path or "").replace("\\", "/")
195+
196+
if not path.endswith("CMakeLists.txt"):
197+
continue
198+
199+
saw_cmakelists = True
200+
patch = d.diff.decode(errors="ignore")
201+
202+
for line in patch.splitlines():
203+
stripped = line.lstrip()
204+
205+
if not stripped:
206+
continue
207+
208+
if stripped.startswith(("diff --git ", "index ", "@@ ", "+++ ", "--- ")):
209+
continue
210+
211+
if not stripped.startswith(("+", "-")):
212+
continue
213+
214+
if VERSION_PATTERN.search(stripped):
215+
found_version_change = True
216+
continue
217+
218+
return False
219+
220+
return saw_cmakelists and found_version_change
221+
181222
msg = commit.message.strip()
182223
first_line, *rest = msg.split("\n")
183224
body = "\n".join(rest)
@@ -225,6 +266,14 @@ def validate_commit(commit):
225266
"Use a full-depth checkout for commit-prefix validation."
226267
)
227268

269+
# -------------------------------
270+
# release special rule
271+
# -------------------------------
272+
if is_version_bump(commit):
273+
if not first_line.startswith("release:"):
274+
return False, "Version bump must use release: prefix"
275+
return True, ""
276+
228277
expected, build_optional = infer_prefix_from_paths(files)
229278

230279
# When no prefix can be inferred (docs/tools), allow anything

.github/scripts/tests/test_commit_lint.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,68 @@ def test_valid_commit_multiline_subject_ignored():
910910
)
911911
ok, _ = validate_commit(commit)
912912
assert ok is True
913+
914+
915+
class FakeDiff:
916+
def __init__(self, path, patch):
917+
self.b_path = path
918+
self.diff = patch.encode()
919+
920+
921+
class FakeStats:
922+
def __init__(self, files):
923+
self.files = {f: {} for f in files}
924+
925+
926+
class FakeCommit:
927+
def __init__(self, message, diffs):
928+
self.message = message
929+
self._diffs = diffs
930+
self.parents = [object()]
931+
932+
file_paths = [d.b_path for d in diffs]
933+
self.stats = FakeStats(file_paths)
934+
935+
def diff(self, parent, create_patch=True):
936+
return self._diffs
937+
938+
939+
def make_fake_commit(message, changes):
940+
"""
941+
changes: list of (path, patch)
942+
"""
943+
diffs = [FakeDiff(path, patch) for path, patch in changes]
944+
return FakeCommit(message, diffs)
945+
946+
def test_release_with_version_bump():
947+
commit = make_fake_commit(
948+
"release: update to 5.0.2\n\nSigned-off-by: User",
949+
[
950+
("CMakeLists.txt", """
951+
-set(FLB_VERSION_PATCH 0)
952+
+set(FLB_VERSION_PATCH 2)
953+
"""),
954+
("dockerfiles/Dockerfile", """
955+
-FROM fluent-bit:5.0.0
956+
+FROM fluent-bit:5.0.2
957+
""")
958+
]
959+
)
960+
961+
ok, msg = validate_commit(commit)
962+
assert ok is True, msg
963+
964+
def test_release_rejected_when_cmakelists_has_non_version_change():
965+
commit = make_fake_commit(
966+
"release: update to 5.0.2\n\nSigned-off-by: User",
967+
[
968+
("CMakeLists.txt", """
969+
-set(FLB_VERSION_PATCH 0)
970+
+set(FLB_VERSION_PATCH 2)
971+
+set(SOME_FLAG ON)
972+
""")
973+
]
974+
)
975+
976+
ok, _ = validate_commit(commit)
977+
assert ok is False

0 commit comments

Comments
 (0)