@@ -176,7 +176,7 @@ def get_summary_resuts(xml_path, minimum_passing_coverage, verbose):
176176 return data , timestamp , version , overall_coverage
177177
178178# Send annotations in batches of 100
179- def send_metrics_annoations (annotationData , workspace , repo_slug , commit_hash , email , token , verbose ):
179+ def send_metrics_annotations (annotationData , workspace , repo_slug , commit_hash , email , token , verbose ):
180180
181181 print ("Sending metrics annotations" )
182182
@@ -204,7 +204,7 @@ def send_metrics_annoations(annotationData, workspace, repo_slug, commit_hash, e
204204 for i in range (0 , len (annotations ), 100 ):
205205 batch = annotations [i :i + 100 ]
206206
207- if verbose :
207+ if verbose :
208208 print (json .dumps (annotations [1 :10 ]))
209209
210210 resp = requests .post (
@@ -214,28 +214,67 @@ def send_metrics_annoations(annotationData, workspace, repo_slug, commit_hash, e
214214 headers = {"Accept" : "application/json" , "Content-Type" : "application/json" }
215215 )
216216
217- if resp .status_code != 200 or verbose :
217+ if resp .status_code != 200 and not verbose :
218218 print ("Batch {} response: {} {}" .format (i // 100 + 1 ,resp .status_code , resp .text ))
219+
220+ elif resp .status_code != 200 or verbose :
221+ print ("Batch {} response: {} {}" .format (i // 100 + 1 ,resp .status_code , resp .text ))
222+
223+ else :
224+ print ("Batch {} response: {}" .format (i // 100 + 1 ,resp .status_code ))
225+
219226
220227 print ("Complete" )
221228
222- def send_metrics_md_report_in_bitbucket (
223- summary ,
224- annotationData ,
225- workspace ,
226- repo_slug ,
227- commit_hash ,
228- email ,
229- token ,
230- link ,
231- verbose ):
229+ def saveDataForSending (summary , annotationData , link , verbose ):
230+
231+ with open ("metrics_summary.bb_txt" , "wb" ) as fd :
232+ fd .write (summary .encode (encFmt ,'replace' ))
233+
234+ with open ("metrics_annotation_data.bb_txt" , "wb" ) as fd :
235+ saveData = json .dumps (annotationData , ensure_ascii = False ).encode (encFmt , "replace" )
236+ fd .write (saveData )
237+
238+ with open ("metrics_link.bb_txt" , "wb" ) as fd :
239+ fd .write (link .encode (encFmt ,'replace' ))
240+
241+ def readSavedData (verbose = False ):
242+
243+ with open ("metrics_summary.bb_txt" , "rb" ) as fd :
244+ summary = fd .read ().decode (encFmt , 'replace' )
245+
246+ with open ("metrics_annotation_data.bb_txt" , "rb" ) as fd :
247+ raw = fd .read ()
248+ rawAsText = raw .decode (encFmt , "replace" )
249+ annotationData = json .loads (rawAsText )
250+
251+ with open ("metrics_link.bb_txt" , "rb" ) as fd :
252+ link = fd .read ().decode (encFmt , 'replace' )
253+
254+ return summary , annotationData , link
255+
256+ def sendMetricsReport (verbose ):
257+
258+ bitbucket_workspace = os .environ ['BITBUCKET_WORKSPACE' ]
259+ bitbucket_repo_slug = os .environ ['BITBUCKET_REPO_SLUG' ]
260+ bitbucket_commit_hash = os .environ ['BITBUCKET_COMMIT' ]
261+ bitbucket_api_token = os .environ ['BITBUCKET_API_TOKEN' ]
262+ bitbucket_email = os .environ ['BITBUCKET_EMAIL' ]
232263
233- print ("Sending metrics data in Markdown format for commit {}" .format (commit_hash ))
264+ summary , annotationData , link = readSavedData ()
265+ report_id = "metrics-report"
266+
267+ print ("Sending metrics data in Markdown format for commit {}" .format (bitbucket_commit_hash ))
234268
235269 # CONFIGURATION
236270 report_id = "metrics-report"
237271
238- url = "https://api.bitbucket.org/2.0/repositories/{}/{}/commit/{}/reports/{}" .format (workspace , repo_slug , commit_hash , report_id )
272+ url = "https://api.bitbucket.org/2.0/repositories/{}/{}/commit/{}/reports/{}" .format (
273+ bitbucket_workspace ,
274+ bitbucket_repo_slug ,
275+ bitbucket_commit_hash ,
276+ report_id
277+ )
239278
240279 headers = {"Accept" : "application/json" , "Content-Type" : "application/json" }
241280
@@ -249,9 +288,7 @@ def send_metrics_md_report_in_bitbucket(
249288 }
250289
251290 sendData = json .dumps (report_payload , ensure_ascii = False ).encode (encFmt , "replace" )
252-
253- print (json .dumps (report_payload , indent = 2 ))
254-
291+
255292 if verbose :
256293 print ("report_payload" )
257294 print (json .dumps (report_payload , ensure_ascii = False , indent = 2 ))
@@ -263,47 +300,40 @@ def send_metrics_md_report_in_bitbucket(
263300
264301 resp = requests .put (
265302 url ,
266- auth = (email , token ),
303+ auth = (bitbucket_email , bitbucket_api_token ),
267304 data = sendData ,
268305 headers = headers ,
269306 timeout = 30
270307 )
271308
272309 if resp .status_code == 200 :
273- print ("Metrics Reported Created" )
310+ send_metrics_annotations (
311+ annotationData ,
312+ bitbucket_workspace ,
313+ bitbucket_repo_slug ,
314+ bitbucket_commit_hash ,
315+ bitbucket_email ,
316+ bitbucket_api_token ,
317+ verbose
318+ )
319+
274320 else :
275321 print ("Metrics Reported Creation - FAILED" )
276322 print ("Metrics Report creation status:" , resp .status_code )
277323 print ("Response:" , resp .text )
278324
279- send_metrics_annoations (annotationData , workspace , repo_slug , commit_hash , email , token , verbose )
280-
281- def buildAndSendCoverage (mpName , filename , minimum_passing_coverage , verbose ):
282-
283- workspace = os .environ ['BITBUCKET_WORKSPACE' ]
284- repo_slug = os .environ ['BITBUCKET_REPO_SLUG' ]
285- commit_hash = os .environ ['BITBUCKET_COMMIT' ]
286- bitbucket_api_token = os .environ ['BITBUCKET_API_TOKEN' ]
287- bitbucket_email = os .environ ['BITBUCKET_EMAIL' ]
325+
326+ def buildCoverageData (mpName , filename , minimum_passing_coverage , verbose ):
288327
289328 annotations = parse_cobertura (filename )
290329
291330 with open ("coverage_results.json" , "wb" ) as fd :
292331 fd .write (json .dumps (annotations , indent = 2 ).encode (encFmt ,'replace' ))
293-
332+
294333 summary , annotation_data , link = generate_metrics_md (mpName )
295-
296- send_metrics_md_report_in_bitbucket (
297- summary , annotation_data ,
298- workspace ,
299- repo_slug ,
300- commit_hash ,
301- bitbucket_email ,
302- bitbucket_api_token ,
303- link ,
304- verbose
305- )
306334
335+ saveDataForSending (summary , annotation_data , link , verbose )
336+
307337def cleanup (dirName , fname = "" ):
308338
309339 if fname == "" :
@@ -351,7 +381,7 @@ def moveFiles(html_base_dir, verbose = False):
351381 print ("Error copying {} --> {}\n {}" .format (html , dest , e ))
352382
353383
354- def run (fullMP , minimum_passing_coverage , useCi , html_base_dir , source_root , verbose ):
384+ def run (fullMP , minimum_passing_coverage , useCi , html_base_dir , source_root , generate_data , send_data , verbose ):
355385
356386 if not checkVectorCASTVersion (21 ):
357387 print ("Cannot create Cobertura metrics to send to BitBucket. Please upgrade VectorCAST" )
@@ -370,29 +400,30 @@ def run(fullMP, minimum_passing_coverage, useCi, html_base_dir, source_root, ver
370400 if not os .path .isdir ("reports/html" ):
371401 os .makedirs ("reports/html" )
372402
373- print ("Generating and sending extended cobertura metrics to BitBucket" )
374- cobertura .generateCoverageResults (
375- fullMP ,
376- azure = False ,
377- xml_data_dir = "coverage" ,
378- verbose = verbose ,
379- extended = True ,
380- source_root = source_root )
381-
382- print ("Creating JUnit metrics to be read by BitBucket" )
383- failed_count , passed_count = generate_results .buildReports (
384- FullManageProjectName = fullMP ,
385- level = None ,
386- envName = None ,
387- generate_individual_reports = False ,
388- timing = False ,
389- cbtDict = None ,
390- use_archive_extract = False ,
391- report_only_failures = False ,
392- no_full_report = False ,
393- use_ci = useCi ,
394- xml_data_dir = "test-results" ,
395- useStartLine = False )
403+ if generate_data :
404+ print ("Generating and sending extended cobertura metrics to BitBucket" )
405+ cobertura .generateCoverageResults (
406+ fullMP ,
407+ azure = False ,
408+ xml_data_dir = "coverage" ,
409+ verbose = verbose ,
410+ extended = True ,
411+ source_root = source_root )
412+
413+ print ("Creating JUnit metrics to be read by BitBucket" )
414+ failed_count , passed_count = generate_results .buildReports (
415+ FullManageProjectName = fullMP ,
416+ level = None ,
417+ envName = None ,
418+ generate_individual_reports = False ,
419+ timing = False ,
420+ cbtDict = None ,
421+ use_archive_extract = False ,
422+ report_only_failures = False ,
423+ no_full_report = False ,
424+ use_ci = useCi ,
425+ xml_data_dir = "test-results" ,
426+ useStartLine = False )
396427
397428 name = os .path .splitext (os .path .basename (fullMP ))[0 ] + ".xml"
398429 fname = os .path .join ("coverage" ,"cobertura" ,"coverage_results_" + name )
@@ -404,39 +435,56 @@ def run(fullMP, minimum_passing_coverage, useCi, html_base_dir, source_root, ver
404435
405436 print ("\n Processing {} and sending to BitBucket: " .format (fname ))
406437
407- buildAndSendCoverage (
408- fullMP ,
409- filename = fname ,
410- minimum_passing_coverage = minimum_passing_coverage ,
411- verbose = verbose
412- )
413-
414- moveFiles (
415- html_base_dir = html_base_dir ,
416- verbose = verbose
417- )
438+ if generate_data :
439+ buildCoverageData (
440+ fullMP ,
441+ filename = fname ,
442+ minimum_passing_coverage = minimum_passing_coverage ,
443+ verbose = verbose
444+ )
445+ moveFiles (
446+ html_base_dir = html_base_dir ,
447+ verbose = verbose
448+ )
449+
450+ else :
451+
452+ sendMetricsReport (verbose )
453+
418454
419455
420456if __name__ == "__main__" :
421457 import argparse
422458
423459 parser = argparse .ArgumentParser (
424- description = "Send coverage information to BitBucket."
460+ description = "Generate/ Send coverage information to BitBucket."
425461 )
426462
427463 parser .add_argument (
428464 "vcProject" ,
429- help = "Path to the VectorCAST Project" ,
430- default = "cobertura.xml"
465+ help = "Path to the VectorCAST Project"
431466 )
432467
433468 parser .add_argument (
434469 "--minimum_passing_coverage" ,
435470 type = float ,
436- help = "Minimum overall coverage required to pass (default 80 percent)" ,
471+ #help="Minimum overall coverage required to pass (default 80 percent)",
472+ help = argparse .SUPPRESS ,
437473 default = 80
438474 )
439475
476+ group = parser .add_mutually_exclusive_group (required = True )
477+ group .add_argument (
478+ '--generate_data' ,
479+ action = 'store_true' ,
480+ help = 'Generate data file but do not send it'
481+ )
482+ group .add_argument (
483+ '--send_data' ,
484+ action = 'store_true' ,
485+ help = 'Send previously generated data file to Bitbucket'
486+ )
487+
440488 parser .add_argument (
441489 "--ci" ,
442490 action = "store_true" ,
@@ -447,7 +495,8 @@ def run(fullMP, minimum_passing_coverage, useCi, html_base_dir, source_root, ver
447495 parser .add_argument (
448496 "-v" , "--verbose" ,
449497 action = "store_true" ,
450- help = "Enable verbose output for debugging or detailed reporting"
498+ help = "Enable verbose output for debugging or detailed reporting" ,
499+ default = False
451500 )
452501
453502
@@ -470,12 +519,20 @@ def run(fullMP, minimum_passing_coverage, useCi, html_base_dir, source_root, ver
470519 else :
471520 useCi = ""
472521
522+ if args .generate_data :
523+ print ("Generating data..." )
524+ elif args .send_data :
525+ print ("Sending data..." )
526+
527+
473528 run (
474529 fullMP = args .vcProject ,
475530 minimum_passing_coverage = args .minimum_passing_coverage ,
476531 useCi = useCi ,
477532 html_base_dir = args .html_base_dir ,
478533 source_root = args .source_root ,
534+ generate_data = args .generate_data ,
535+ send_data = args .send_data ,
479536 verbose = args .verbose
480537 )
481538
0 commit comments