Last active
July 1, 2025 08:45
-
-
Save nosmo/306d50581f4069958206d772b6d49176 to your computer and use it in GitHub Desktop.
pre-commit hook to prevent committing a Helm chart change without bumping Chart.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """Check a git commit for changes to charts with no bumps of Chart.yaml""" | |
| import os | |
| import re | |
| import subprocess | |
| import sys | |
| def command(command_s: str) -> (str, str): | |
| with subprocess.Popen(command_s.split(" "), | |
| stdout=subprocess.PIPE, stderr=subprocess.PIPE) as command_p: | |
| stdout, stderr = command_p.communicate() | |
| if command_p.returncode: | |
| raise Exception(f"Failed to execute process {command_s}: {stderr}") | |
| return stdout.decode() | |
| def check_amend() -> bool: | |
| ppid = os.getppid() | |
| ps_output = subprocess.Popen(["ps", "-f", "-p", str(ppid)], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE) | |
| if "--amend" in ps_output.communicate()[0].decode(): | |
| return True | |
| return False | |
| def main(): | |
| if check_amend(): | |
| # If the commit is an amend, assume that we have already run | |
| # the hook on the base commit | |
| sys.exit(0) | |
| changed_files = command("git diff HEAD --name-only") | |
| changed_charts = set() | |
| bumped_charts = set() | |
| for changed_file in changed_files.strip().split("\n"): | |
| chart_match = re.match("^charts/([\w-]+)/(.*)", changed_file) | |
| if chart_match: | |
| chartname, chartrest = chart_match.groups() | |
| changed_charts.add(chartname) | |
| if chartrest == "Chart.yaml": | |
| bumped_charts.add(chartname) | |
| not_bumped = changed_charts.difference(bumped_charts) | |
| if not_bumped: | |
| print("Charts changed without Chart.yaml bumped: {}".format(",".join(not_bumped))) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment