123 lines
3.2 KiB
Python
123 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
[fr] Bot Gerrit pour commenter automatiquement l'impact de compilation
|
||
d’un patch (mode estimation rapide).
|
||
|
||
[en] Gerrit bot to automatically comment on the build impact
|
||
of a patch (quick estimation mode).
|
||
"""
|
||
|
||
import subprocess
|
||
import requests
|
||
import sys
|
||
import json
|
||
|
||
# === CONFIGURATION ===
|
||
GERRIT_URL = "https://gerrit.example.org"
|
||
USERNAME = "build-impact-bot"
|
||
HTTP_PASSWORD = "YOUR_HTTP_PASSWORD"
|
||
|
||
# =====================
|
||
|
||
def gerrit_get(path):
|
||
"""
|
||
[fr] Effectue une requête GET sur l’API Gerrit.
|
||
[en] Performs a GET request on Gerrit REST API.
|
||
"""
|
||
url = f"{GERRIT_URL}{path}"
|
||
r = requests.get(url, auth=(USERNAME, HTTP_PASSWORD))
|
||
r.raise_for_status()
|
||
|
||
# [fr] Supprimer la protection anti-XSSI
|
||
# [en] Remove Gerrit XSSI protection
|
||
text = r.text
|
||
if text.startswith(")]}'"):
|
||
text = text.split("\n", 1)[1]
|
||
|
||
return json.loads(text)
|
||
|
||
|
||
def gerrit_post(path, data):
|
||
"""
|
||
[fr] Effectue une requête POST sur l’API Gerrit.
|
||
[en] Performs a POST request on Gerrit REST API.
|
||
"""
|
||
url = f"{GERRIT_URL}{path}"
|
||
headers = {"Content-Type": "application/json"}
|
||
r = requests.post(url, auth=(USERNAME, HTTP_PASSWORD),
|
||
headers=headers, data=json.dumps(data))
|
||
r.raise_for_status()
|
||
|
||
|
||
def get_changed_files(change_id, revision_id):
|
||
"""
|
||
[fr] Récupère la liste des fichiers modifiés dans un patchset.
|
||
[en] Retrieves the list of files modified in a patchset.
|
||
"""
|
||
files = gerrit_get(f"/a/changes/{change_id}/revisions/{revision_id}/files/")
|
||
return [f for f in files.keys() if f != "/COMMIT_MSG"]
|
||
|
||
|
||
def run_quick_estimation(file_path):
|
||
"""
|
||
[fr] Exécute le script d’estimation rapide pour un fichier.
|
||
[en] Runs the quick estimation script for a file.
|
||
"""
|
||
result = subprocess.run(
|
||
["./predict_rebuild.py", "--quick", file_path],
|
||
capture_output=True,
|
||
text=True
|
||
)
|
||
return result.stdout.strip()
|
||
|
||
|
||
def post_comment(change_id, revision_id, message):
|
||
"""
|
||
[fr] Poste un commentaire sur Gerrit.
|
||
[en] Posts a comment on Gerrit.
|
||
"""
|
||
data = {"message": message}
|
||
gerrit_post(f"/a/changes/{change_id}/revisions/{revision_id}/review", data)
|
||
|
||
|
||
def main():
|
||
"""
|
||
[fr] Point d’entrée du bot.
|
||
Usage: ./gerrit_build_impact_bot.py <change-id> <revision-id>
|
||
[en] Bot entry point.
|
||
Usage: ./gerrit_build_impact_bot.py <change-id> <revision-id>
|
||
"""
|
||
if len(sys.argv) != 3:
|
||
print("Usage: gerrit_build_impact_bot.py <change-id> <revision-id>")
|
||
sys.exit(1)
|
||
|
||
change_id = sys.argv[1]
|
||
revision_id = sys.argv[2]
|
||
|
||
print("📥 Fetching modified files...")
|
||
files = get_changed_files(change_id, revision_id)
|
||
|
||
if not files:
|
||
print("No files modified.")
|
||
sys.exit(0)
|
||
|
||
print("⚡ Running build impact estimation...")
|
||
reports = []
|
||
for f in files:
|
||
report = run_quick_estimation(f)
|
||
reports.append(f"📄 {f}\n{report}")
|
||
|
||
comment = (
|
||
"⚡ **Build impact (automated)**\n\n"
|
||
+ "\n\n".join(reports)
|
||
)
|
||
|
||
print("💬 Posting comment to Gerrit...")
|
||
post_comment(change_id, revision_id, comment)
|
||
print("✅ Done.")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
|