Merge ~dannf/canonical-server-hwe-utils:dgx2-performance-regression-googlesheet into canonical-server-hwe-utils:master

Proposed by dann frazier
Status: Merged
Approved by: Andrew Cloke
Approved revision: 71896df37b8411b01d2c3ea30246627ea8638879
Merged at revision: 71896df37b8411b01d2c3ea30246627ea8638879
Proposed branch: ~dannf/canonical-server-hwe-utils:dgx2-performance-regression-googlesheet
Merge into: canonical-server-hwe-utils:master
Diff against target: 96 lines (+90/-0)
1 file modified
lp-scripts/dgx2-performance-regression-googlesheet.py (+90/-0)
Reviewer Review Type Date Requested Status
Andrew Cloke Approve
Review via email: mp+376087@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Andrew Cloke (andrew-cloke) wrote :

I'll be honest that I've not used any python libraries to automate gdoc uploads, but this small utility programme all seems clear and logical.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lp-scripts/dgx2-performance-regression-googlesheet.py b/lp-scripts/dgx2-performance-regression-googlesheet.py
2new file mode 100755
3index 0000000..e08edf4
4--- /dev/null
5+++ b/lp-scripts/dgx2-performance-regression-googlesheet.py
6@@ -0,0 +1,90 @@
7+#!/usr/bin/env python3
8+#
9+# Updates an existing google sheet that tracks performance regression bugs.
10+# To run, you'll need google sheet API credentials, and a number of pip
11+# libraries installed (a virtualenv is recommended). See
12+# https://developers.google.com/sheets/api/quickstart/python
13+# And, of course, you'll need write-access to the spreadsheet.
14+#
15+import pickle
16+import os.path
17+from googleapiclient.discovery import build
18+from google_auth_oauthlib.flow import InstalledAppFlow
19+from google.auth.transport.requests import Request
20+from launchpadlib.launchpad import Launchpad
21+
22+FIELDNAMES = ["Link", "Title", "Importance", "Status"]
23+GOOGLE_SPREADSHEET_ID = "1CkjLn_yWxR_LN2nhOHCkyiJq-UvuI2XTGjHIiUAW7mU"
24+GOOGLE_RANGE_NAME = "Sheet1!A2"
25+
26+
27+def update_google_sheet(data):
28+ # If modifying these scopes, delete the file token.pickle.
29+ scopes = ["https://www.googleapis.com/auth/spreadsheets"]
30+
31+ creds = None
32+ # The file token.pickle stores the user's access and refresh tokens, and is
33+ # created automatically when the authorization flow completes for the first
34+ # time.
35+ if os.path.exists("token.pickle"):
36+ with open("token.pickle", "rb") as token:
37+ creds = pickle.load(token)
38+ # If there are no (valid) credentials available, let the user log in.
39+ if not creds or not creds.valid:
40+ if creds and creds.expired and creds.refresh_token:
41+ creds.refresh(Request())
42+ else:
43+ flow = InstalledAppFlow.from_client_secrets_file(
44+ "credentials.json", scopes,
45+ )
46+ creds = flow.run_local_server(port=0)
47+ # Save the credentials for the next run
48+ with open("token.pickle", "wb") as token:
49+ pickle.dump(creds, token)
50+
51+ service = build("sheets", "v4", credentials=creds)
52+
53+ result = (
54+ service.spreadsheets()
55+ .values()
56+ .clear(spreadsheetId=GOOGLE_SPREADSHEET_ID, range=GOOGLE_RANGE_NAME,)
57+ .execute()
58+ )
59+
60+ body = {"values": data}
61+ result = (
62+ service.spreadsheets()
63+ .values()
64+ .update(
65+ spreadsheetId=GOOGLE_SPREADSHEET_ID,
66+ range=GOOGLE_RANGE_NAME,
67+ valueInputOption="USER_ENTERED",
68+ body=body,
69+ )
70+ .execute()
71+ )
72+ print("{0} cells updated.".format(result.get("updatedCells")))
73+
74+
75+if __name__ == "__main__":
76+ lp = Launchpad.login_with("lpbugs", "production", version="devel")
77+
78+ p = lp.projects["ubuntu"]
79+
80+ data = [FIELDNAMES]
81+
82+ for task in p.searchTasks(tags=["dgx2-performance-regression"], status=[]):
83+ row = []
84+ bug = int(task.web_link.split("/")[-1])
85+ for field in FIELDNAMES:
86+ if field == "Link":
87+ row.append('=HYPERLINK("{}", "LP: #{}")'.format(task.web_link, bug,))
88+ if field == "Title":
89+ row.append(lp.bugs[bug].title)
90+ if field == "Importance":
91+ row.append(task.importance)
92+ if field == "Status":
93+ row.append(task.status)
94+ data.append(row)
95+
96+ update_google_sheet(data)

Subscribers

People subscribed via source and target branches

to all changes: