Author: sebb
Date: Sat Jun 21 22:44:29 2025
New Revision: 1926630

URL: http://svn.apache.org/viewvc?rev=1926630&view=rev
Log:
Prepare for bugzilla creds

(Not yet added to Puppet)

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=1926630&r1=1926629&r2=1926630&view=diff
==============================================================================
--- comdev/reporter.apache.org/trunk/scripts/bugzillastats.py (original)
+++ comdev/reporter.apache.org/trunk/scripts/bugzillastats.py Sat Jun 21 
22:44:29 2025
@@ -14,28 +14,49 @@ if sys.hexversion < 0x030000F0:
     ../data/bugzillastats.json
 """
 import errtee  # this is used, even though it is not referenced
-from urlutils import URLopen
 import json
+import requests
 
+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";
 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.
+BUGZILLA_CREDFILE = '/usr/local/etc/tokens/bugzilla.txt'
+
 __BUGZILLA_STATS = "../data/bugzillastats.json"
 
-def getCSV(url):
+def bzlogin():
+    try:
+        with open(BUGZILLA_CREDFILE, "r") as f:
+            user, password = f.read().strip().split(':',1)
+    except Exception:
+        raise RuntimeError("Failed to read/parse Bugzilla credentials")
+    if password == '*': # 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 = {}
-    # TODO add authentication when made available: INFRA-26931
-    with URLopen(url) as f:
-        text = f.read().decode('utf-8')
-        if 'ASF Bugzilla needs a legitimate login and password to continue.' 
in text:
-            raise RuntimeError("Buzilla 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)
-        f.close()
+    rsp = requests.get(url, cookies=cookies)
+    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)
     return csv
 
 # test data:
@@ -44,11 +65,14 @@ def getCSV(url):
 # 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 list of bugs created in the last 3 months")
-created = getCSV(BASE+CREATED)
+created = getCSV(BASE+CREATED, cookies)
  
 print("Getting list of bugs resolved in the last 3 months")
-resolved = getCSV(BASE+RESOLVED)
+resolved = getCSV(BASE+RESOLVED, cookies)
 
 stats = {}
 


Reply via email to