Author: sebb
Date: Tue Jun 24 12:40:04 2025
New Revision: 1926687

URL: http://svn.apache.org/viewvc?rev=1926687&view=rev
Log:
Update to use REST API and key

Modified:
    comdev/reporter.apache.org/trunk/scripts/bugzillastats.py

Modified: comdev/reporter.apache.org/trunk/scripts/bugzillastats.py
URL: 
http://svn.apache.org/viewvc/comdev/reporter.apache.org/trunk/scripts/bugzillastats.py?rev=1926687&r1=1926686&r2=1926687&view=diff
==============================================================================
--- comdev/reporter.apache.org/trunk/scripts/bugzillastats.py (original)
+++ comdev/reporter.apache.org/trunk/scripts/bugzillastats.py Tue Jun 24 
12:40:04 2025
@@ -2,7 +2,7 @@ import sys
 if sys.hexversion < 0x030000F0:
     raise RuntimeError("This script requires Python3")
 """
-   This script runs a couple of Bugzilla reports:
+   This script runs a couple of Bugzilla searches:
    - all bugs that were created in the last 3 months  
    - all RESOLVED bugs that changed to RESOLVED in the last 3 months
    and creates an output file:
@@ -16,47 +16,35 @@ if sys.hexversion < 0x030000F0:
 import errtee  # this is used, even though it is not referenced
 import json
 import requests
+from collections import defaultdict
 
-LOGIN = 'https://bz.apache.org/bugzilla/index.cgi'
-BASE = 
"https://bz.apache.org/bugzilla/report.cgi?y_axis_field=product&ctype=csv&format=table&action=wrap";
+BASE = "https://bz.apache.org/bugzilla/rest.cgi/bug?include_fields=product";
 CREATED = "&f1=creation_ts&o1=greaterthaneq&v1=-3m"
 RESOLVED = 
"&chfield=bug_status&chfieldvalue=RESOLVED&chfieldfrom=-3m&chfieldto=Now"
 
-# Maintained in Puppet; contains user:password
-# For local testing, if the password is '*', user is prompted for the password.
+# Maintained in Puppet; contains api key
 BUGZILLA_CREDFILE = '/usr/local/etc/tokens/bugzilla.txt'
 
 __BUGZILLA_STATS = "../data/bugzillastats.json"
 
-def bzlogin():
+def bzapikey():
     try:
         with open(BUGZILLA_CREDFILE, "r") as f:
-            user, password = f.read().strip().split(':',1)
+            apikey = f.read().strip()
     except Exception:
         raise RuntimeError("Failed to read/parse Bugzilla credentials")
-    if password == '*': # for local testing
+    if apikey == '*': # for local testing
         import getpass
-        password = getpass.getpass(f"Pass for {user}: ")
-    data = f"Bugzilla_login={user}&Bugzilla_password={password}".encode()
-    jar = requests.cookies.RequestsCookieJar()
-    rsp = requests.post(LOGIN, cookies=jar, data=data, headers={'Referer': 
'https://bz.apache.org/bugzilla/'})
-    data = user = password = None # no longer needed
-    cookies = rsp.cookies
-    if not 'Bugzilla_logincookie' in cookies:
-        raise RuntimeError("Failed to login")
-    return cookies
-
-def getCSV(url, cookies):
-    csv = {}
-    rsp = requests.get(url, cookies=cookies)
+        apikey = getpass.getpass(f"API key: ")
+    return apikey
+
+def getCSV(url, apikey):
+    csv = defaultdict(int)
+    rsp = requests.get(f"{url}&api_key={apikey}")
     text = rsp.text
-    if 'ASF Bugzilla needs a legitimate login and password to continue.' in 
text:
-        raise RuntimeError("Bugzilla needs login")
-    lines = text.splitlines()
-    for l in lines[1:]: # Drop header
-        product, count = l.split(',')
-        # product is enclosed in quotes; drop them
-        csv[product[1:-1]] = int(count)
+    bugs = json.loads(text)['bugs']
+    for k in bugs:
+        csv[k['product']] += 1
     return csv
 
 # test data:
@@ -65,14 +53,14 @@ def getCSV(url, cookies):
 # resolved = {'JMeter': 33, 'Ant': 8, 'APR': 2, 'Log4j': 2, 'WebSH': 1, 
'Tomcat 9': 3, 'Apache httpd-2': 35, 
 #           'Tomcat 7': 26, 'Tomcat Modules': 1, 'Tomcat Connectors': 7, 
'Tomcat 6': 5, 'Taglibs': 2, 'Tomcat Native': 1, 'Tomcat 8': 88, 'POI': 115, 
'Rivet': 0}
 
-print("Logging into Bugzilla")
-cookies = bzlogin()
+print("Getting Bugzilla API key")
+apikey = bzapikey()
 
 print("Getting list of bugs created in the last 3 months")
-created = getCSV(BASE+CREATED, cookies)
- 
+created = getCSV(BASE+CREATED, apikey)
+
 print("Getting list of bugs resolved in the last 3 months")
-resolved = getCSV(BASE+RESOLVED, cookies)
+resolved = getCSV(BASE+RESOLVED, apikey)
 
 stats = {}
 


Reply via email to