-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_maintainers_json.py
executable file
·80 lines (73 loc) · 3.25 KB
/
update_maintainers_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
import sys
from google.oauth2 import service_account
from googleapiclient.discovery import build
import json_generation_lib
from linaro_vault_lib import get_vault_secret
nesting_level = 0
SCOPES = [
"https://www.googleapis.com/auth/spreadsheets.readonly"
]
SPREADSHEET_ID = "1b6O-YbZhj1t94zUS1sTRFQ9S4uqNTzTGCiDaGhdpugI"
def create_json_object(delegated_creds):
# Create our new dict
json_blob = {
"maintainers_by_company": [],
"maintainers_by_project": [],
}
# Create new google sheets service with delegated_creds
with build('sheets', 'v4', credentials=delegated_creds) as service:
sheet = service.spreadsheets()
# Get data from projects sheet
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range="maintainers by project!A1:B").execute()
values = result.get('values', [])
if not values:
print("Failed to retreive projects data from areas by project!A1:B for spreadsheet with Id - {}".format(SPREADSHEET_ID))
return False
else:
for row in values[1:]:
# Print columns A and E, which correspond to indices 0 and 4.
print(row)
json_blob["maintainers_by_project"].append({"name": row[0], "num": row[1] })
# Get data from maintainers sheet
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range="pivot: area by company!A1:B").execute()
values = result.get('values', [])
if not values:
print("Failed to retreive projects data from 'pivot: area by company!A1:B' for spreadsheet with Id - {}".format(SPREADSHEET_ID))
return False
else:
for row in values[1:]:
# Print columns A and E, which correspond to indices 0 and 4.
print(row)
json_blob["maintainers_by_company"].append({"name": row[0], "num": row[1] })
return json_blob
def initialise_auth():
# Username (email) of user to run scripts as.
username = "[email protected]"
# Get the Google Service Account JSON blob
google_service_account_json = json.loads(get_vault_secret(
"secret/misc/google-gitmaintainerssync.json",
iam_role="arn:aws:iam::968685071553:role/vault_jira_project_updater"))
# Instantiate a new service account auth object
service_account_auth = service_account.Credentials.from_service_account_info(
google_service_account_json, scopes=SCOPES)
delegated_creds = service_account_auth.with_subject(username)
return delegated_creds
def main():
# Initialize Google Auth
delegated_creds = initialise_auth()
# Create the JSON files with the returned credentials
json_data = create_json_object(delegated_creds)
# Check if files have been created successfully.
if not json_data:
print("Failed to created the maintainers/projects JSON files")
sys.exit(1)
else:
print("Maintainers data fetched. Checking in the changes.")
# Check for changes
working_dir = json_generation_lib.working_dir()
json_generation_lib.do_the_git_bits(json_data, "%s/website/_data/maintainers.json" % working_dir)
if __name__ == '__main__':
main()