Skip to content

Commit 7e6cfd2

Browse files
committed
feat(scripts): add python dependabot pruner to fix-dependabot.sh
1 parent 15fa5fd commit 7e6cfd2

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

scripts/fix-dependabot.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ source "$SCRIPT_DIR/lib/third-party-excludes.sh" 2>/dev/null || true
1717
REPO_PATH="${1:?Usage: fix-dependabot.sh <repo-path> <finding-json>}"
1818
FINDING_JSON="${2:?Usage: fix-dependabot.sh <repo-path> <finding-json>}"
1919

20-
# --- Idempotency check ---
20+
# --- Idempotency check & prune ---
2121
if [[ -f "${REPO_PATH}/.github/dependabot.yml" ]] || \
2222
[[ -f "${REPO_PATH}/.github/dependabot.yaml" ]]; then
23-
echo "[fix-dependabot] dependabot.yml already exists — skipping."
23+
echo "[fix-dependabot] dependabot.yml already exists — pruning invalid ecosystems."
24+
python3 "$SCRIPT_DIR/prune-dependabot.py" "$REPO_PATH"
2425
exit 0
2526
fi
2627

scripts/prune-dependabot.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)