|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import yaml |
| 5 | + |
| 6 | +if len(sys.argv) < 2: |
| 7 | + print("Usage: prune-dependabot.py <repo-path>") |
| 8 | + sys.exit(1) |
| 9 | + |
| 10 | +repo_path = sys.argv[1] |
| 11 | +dependabot_yml = os.path.join(repo_path, '.github', 'dependabot.yml') |
| 12 | +dependabot_yaml = os.path.join(repo_path, '.github', 'dependabot.yaml') |
| 13 | + |
| 14 | +file_to_process = None |
| 15 | +if os.path.exists(dependabot_yml): |
| 16 | + file_to_process = dependabot_yml |
| 17 | +elif os.path.exists(dependabot_yaml): |
| 18 | + file_to_process = dependabot_yaml |
| 19 | + |
| 20 | +if not file_to_process: |
| 21 | + sys.exit(0) |
| 22 | + |
| 23 | +try: |
| 24 | + with open(file_to_process, 'r') as f: |
| 25 | + config = yaml.safe_load(f) |
| 26 | +except Exception as e: |
| 27 | + print(f"Failed to load yaml: {e}") |
| 28 | + sys.exit(0) |
| 29 | + |
| 30 | +if not config or 'updates' not in config: |
| 31 | + sys.exit(0) |
| 32 | + |
| 33 | +def has_manifest(repo, ecosystem): |
| 34 | + if ecosystem == 'cargo': |
| 35 | + return os.path.exists(os.path.join(repo, 'Cargo.toml')) |
| 36 | + if ecosystem == 'mix': |
| 37 | + return os.path.exists(os.path.join(repo, 'mix.exs')) |
| 38 | + if ecosystem == 'npm': |
| 39 | + return os.path.exists(os.path.join(repo, 'package.json')) |
| 40 | + if ecosystem == 'bundler': |
| 41 | + return os.path.exists(os.path.join(repo, 'Gemfile')) |
| 42 | + if ecosystem == 'pip': |
| 43 | + return os.path.exists(os.path.join(repo, 'requirements.txt')) or os.path.exists(os.path.join(repo, 'setup.py')) |
| 44 | + if ecosystem == 'gomod': |
| 45 | + return os.path.exists(os.path.join(repo, 'go.mod')) |
| 46 | + if ecosystem == 'github-actions': |
| 47 | + return True |
| 48 | + return False |
| 49 | + |
| 50 | +original_updates = config['updates'] |
| 51 | +new_updates = [] |
| 52 | +changed = False |
| 53 | + |
| 54 | +for update in original_updates: |
| 55 | + ecosystem = update.get('package-ecosystem') |
| 56 | + if has_manifest(repo_path, ecosystem): |
| 57 | + new_updates.append(update) |
| 58 | + else: |
| 59 | + changed = True |
| 60 | + print(f"Pruned missing ecosystem: {ecosystem}") |
| 61 | + |
| 62 | +if changed: |
| 63 | + config['updates'] = new_updates |
| 64 | + with open(file_to_process, 'w') as f: |
| 65 | + yaml.dump(config, f, sort_keys=False) |
| 66 | + print("dependabot.yml updated.") |
0 commit comments