Skip to content

Commit

Permalink
Extend google spreadsheet driver
Browse files Browse the repository at this point in the history
  • Loading branch information
abregman committed Nov 8, 2021
1 parent f10372e commit 2a7626a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ To run full triaging process of pulling and publishing the data: `cinfo triage`
To only pull data: `cinfo pull`
To only publish existing local data: `cinfo publish`

## Supported Sources & Targets

Sources:

* Jenkins
* (TODO) Report Portal
* (TODO) ELK

Targets:

* Google Spreadsheet
* (TODO) Email

## Contributions

Contributions are made by submitting pull requests
67 changes: 59 additions & 8 deletions cinfo/drivers/google_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import gspread
from gspread.models import Cell
from oauth2client.service_account import ServiceAccountCredentials
import time

import logging

LOG = logging.getLogger(__name__)

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
cell_colors_by_status = {'SUCCESS': [0, 1, 0],
'UNSTABLE': [0.9, 0.9, 0.2],
'FAILURE': [1, 0, 0]}


class Google_spreadsheet(object):
Expand All @@ -29,6 +33,7 @@ def __init__(self):
self.client = gspread.authorize(self.creds)

def publish(self, data):
cells_list = []
sheet = self.client.open("Unified Jobs Triaging")

# Create worksheets
Expand All @@ -40,11 +45,57 @@ def publish(self, data):
LOG.debug("Worksheets already exist...skipping")

ws = sheet.worksheet("Per Job")
import pdb;pdb.set_trace()
for job in data['jobs']:
ws.update_cell(i, 1, 'Bingo!')


cells_list = []
cells_list.append(Cell(row=1, col=1 , value="This spreadsheet is auto-generated by cinfo (github.com/bregman-arie/cinfo). PLEASE DON'T EDIT MANUALLY :)"))
general_sheet.update_cells(cells_list)
cells_list.append(Cell(1, 1, "This is generated automatically by CInfo. You can edit the spreadsheet and add additional data"))
i = 2
for job_name, job_data in data['jobs'].items():
ws.format("A{0}:H{0}".format(i), {
"backgroundColor": {
"red": 0.3,
"green": 0.8,
"blue": 1,
}
})
i+=1
cells_list.append(Cell(i, 1, "Job Name"))
cells_list.append(Cell(i, 2, job_name))
i+=1
cells_list.append(Cell(i, 1, "Build Number"))
cells_list.append(Cell(i, 2, job_data['build_number']))
i+=1
cells_list.append(Cell(i, 1, "Build Result"))
cells_list.append(Cell(i, 2, job_data['result']))
ws.format("B{0}:B{0}".format(i), {
"backgroundColor": {
"red": cell_colors_by_status[job_data['result']][0],
"green": cell_colors_by_status[job_data['result']][1],
"blue": cell_colors_by_status[job_data['result']][2],
}
})
i+=1
if job_data['result'] == "UNSTABLE":
cells_list.append(Cell(i, 1, "Tests Results"))
ws.format("A{0}:C{0}".format(i), {
"backgroundColor": {
"red": 0.9,
"green": 0.8,
"blue": 0.8
}
})
i+=1
cells_list.append(Cell(i, 1, 'className'))
cells_list.append(Cell(i, 2, 'name'))
cells_list.append(Cell(i, 3, 'status'))
cells_list.append(Cell(i, 4, 'duration'))
cells_list.append(Cell(i, 5, 'skipped'))
cells_list.append(Cell(i, 6, 'skippedMessage'))
i+=1
for test in job_data['tests']:
if test['status'] != "SKIPPED" and test['status'] != "PASSED":
cells_list.append(Cell(i, 1, test['className']))
cells_list.append(Cell(i, 2, test['name']))
cells_list.append(Cell(i, 3, test['status']))
cells_list.append(Cell(i, 4, test['duration']))
cells_list.append(Cell(i, 5, test['skipped']))
cells_list.append(Cell(i, 6, test['skippedMessage']))
i+=1
ws.update_cells(cells_list)
36 changes: 32 additions & 4 deletions cinfo/drivers/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

CALLS = {'get_jobs':
"/api/json/?tree=jobs[name,lastBuild[result,number,timestamp]]",
'get_builds': "/job/{}/api/json?tree=allBuilds[number,url]"}
'get_builds': "/job/{}/api/json?tree=allBuilds[number,url,result,actions[causes]]",
'get_tests': "/job/{}/{}/testReport/api/json"
}
LOG = logging.getLogger(__name__)


Expand All @@ -27,11 +29,37 @@ class Jenkins(object):
def pull(self, url, jobs=[]):
data = {'jobs': {},
'tests': {},
'job_statuses': {},
'test_statuses': {}
'build_statuses': {'SUCCESS': [], 'FAILURE': [],
'UNSTABLE': [], 'ABORTED': []},
}
for job in jobs:
LOG.info(" Job: {}".format(job))
request = requests.get(url + CALLS['get_builds'].format(job), verify=False)
result_json = json.loads(request.text)
return result_json
data['jobs'][job] = {'build_number': -1}
for build in result_json['allBuilds']:
if "GerritCause" in build['actions'][0]['causes'][0]['_class']:
pass
elif "BuildUpstreamCause" in build['actions'][0]['causes'][0]['_class']:
if build['number'] > data['jobs'][job]['build_number']:
build_data = {'build_number': build['number'], 'result': build['result'],
'url': build['url'], 'job': job, 'tests': []}
if build_data['result'] == 'UNSTABLE':
tests_request = requests.get(url + CALLS['get_tests'].format(
job,build_data['build_number']), verify=False)
build_tests_json = json.loads(tests_request.text)
build_data['tests_duration'] = build_tests_json['duration']
build_data['tests_fail_count'] = build_tests_json['failCount']
build_data['tests_pass_count'] = build_tests_json['passCount']
build_data['tests_skip_count'] = build_tests_json['skipCount']
test_data = {}
for test_suite in build_tests_json['suites']:
for case in test_suite['cases']:
tests_data = {'age': case['age'], "className": case['className'],
'skipped': case['skipped'], 'skippedMessage': case['skippedMessage'],
'name': case['name'], 'status': case['status'],
'duration': case['duration']}
build_data['tests'].append(tests_data)
data['jobs'][job] = build_data
data['build_statuses'][build_data['result']].append(build_data)
return data

0 comments on commit 2a7626a

Please sign in to comment.