2021-09-16 12:17:47 +00:00
#!/usr/bin/env python3
2021-10-27 07:03:24 +00:00
# pylint: disable=line-too-long
2021-09-16 12:17:47 +00:00
import os
import json
import logging
2021-10-27 07:03:24 +00:00
import sys
2021-09-16 12:17:47 +00:00
from github import Github
from s3_helper import S3Helper
2021-11-29 14:17:17 +00:00
from pr_info import PRInfo , get_event
2021-10-22 09:26:39 +00:00
from get_robot_token import get_best_robot_token , get_parameter_from_ssm
2021-11-12 11:39:00 +00:00
from upload_result_helper import upload_results
2021-11-12 12:36:25 +00:00
from commit_status_helper import get_commit
2021-11-19 14:47:04 +00:00
from clickhouse_helper import ClickHouseHelper , prepare_tests_results_for_clickhouse
from stopwatch import Stopwatch
2021-12-01 14:23:51 +00:00
from rerun_helper import RerunHelper
2021-12-03 08:33:16 +00:00
from tee_popen import TeePopen
2021-09-16 12:17:47 +00:00
NAME = ' PVS Studio (actions) '
2021-09-16 13:14:24 +00:00
LICENCE_NAME = ' Free license: ClickHouse, Yandex '
2021-09-16 12:17:47 +00:00
HTML_REPORT_FOLDER = ' pvs-studio-html-report '
TXT_REPORT_NAME = ' pvs-studio-task-report.txt '
2021-09-16 13:52:16 +00:00
def _process_txt_report ( path ) :
2021-09-16 12:17:47 +00:00
warnings = [ ]
errors = [ ]
with open ( path , ' r ' ) as report_file :
for line in report_file :
if ' viva64 ' in line :
continue
2021-10-27 07:03:24 +00:00
if ' warn ' in line :
2021-09-16 12:17:47 +00:00
warnings . append ( ' : ' . join ( line . split ( ' \t ' ) [ 0 : 2 ] ) )
elif ' err ' in line :
errors . append ( ' : ' . join ( line . split ( ' \t ' ) [ 0 : 2 ] ) )
2021-10-27 07:03:24 +00:00
2021-09-16 12:17:47 +00:00
return warnings , errors
if __name__ == " __main__ " :
logging . basicConfig ( level = logging . INFO )
2021-11-19 14:47:04 +00:00
stopwatch = Stopwatch ( )
2021-09-17 09:59:31 +00:00
repo_path = os . path . join ( os . getenv ( " REPO_COPY " , os . path . abspath ( " ../../ " ) ) )
2021-11-08 13:52:50 +00:00
temp_path = os . path . join ( os . getenv ( " TEMP_PATH " ) )
2021-09-16 12:17:47 +00:00
2021-11-29 14:17:17 +00:00
pr_info = PRInfo ( get_event ( ) )
2021-09-17 08:20:56 +00:00
# this check modify repository so copy it to the temp directory
2021-09-17 09:41:31 +00:00
logging . info ( " Repo copy path %s " , repo_path )
2021-09-17 08:20:56 +00:00
2021-10-20 11:25:14 +00:00
gh = Github ( get_best_robot_token ( ) )
2021-12-01 14:23:51 +00:00
rerun_helper = RerunHelper ( gh , pr_info , NAME )
if rerun_helper . is_already_finished_by_status ( ) :
logging . info ( " Check is already finished according to github status, exiting " )
sys . exit ( 0 )
2021-10-20 11:25:14 +00:00
2021-09-16 12:17:47 +00:00
images_path = os . path . join ( temp_path , ' changed_images.json ' )
docker_image = ' clickhouse/pvs-test '
if os . path . exists ( images_path ) :
logging . info ( " Images file exists " )
with open ( images_path , ' r ' ) as images_fd :
images = json . load ( images_fd )
logging . info ( " Got images %s " , images )
if ' clickhouse/pvs-test ' in images :
docker_image + = ' : ' + images [ ' clickhouse/pvs-test ' ]
logging . info ( " Got docker image %s " , docker_image )
2021-10-20 11:48:27 +00:00
s3_helper = S3Helper ( ' https://s3.amazonaws.com ' )
2021-09-16 12:17:47 +00:00
2021-10-22 09:26:39 +00:00
licence_key = get_parameter_from_ssm ( ' pvs_studio_key ' )
2021-09-17 08:20:56 +00:00
cmd = f " docker run -u $(id -u $ {{ USER }} ):$(id -g $ {{ USER }} ) --volume= { repo_path } :/repo_folder --volume= { temp_path } :/test_output -e LICENCE_NAME= ' { LICENCE_NAME } ' -e LICENCE_KEY= ' { licence_key } ' { docker_image } "
2021-09-16 13:02:40 +00:00
commit = get_commit ( gh , pr_info . sha )
2021-09-16 12:17:47 +00:00
2021-12-03 08:33:16 +00:00
run_log_path = os . path . join ( temp_path , ' run_log.log ' )
with TeePopen ( cmd , run_log_path ) as process :
retcode = process . wait ( )
if retcode != 0 :
logging . info ( " Run failed " )
else :
logging . info ( " Run Ok " )
if retcode != 0 :
2021-09-16 12:44:42 +00:00
commit . create_status ( context = NAME , description = ' PVS report failed to build ' , state = ' failure ' , target_url = f " https://github.com/ClickHouse/ClickHouse/actions/runs/ { os . getenv ( ' GITHUB_RUN_ID ' ) } " )
sys . exit ( 1 )
2021-09-16 12:17:47 +00:00
2021-09-16 15:51:43 +00:00
try :
s3_path_prefix = str ( pr_info . number ) + " / " + pr_info . sha + " / " + NAME . lower ( ) . replace ( ' ' , ' _ ' )
html_urls = s3_helper . upload_test_folder_to_s3 ( os . path . join ( temp_path , HTML_REPORT_FOLDER ) , s3_path_prefix )
index_html = None
for url in html_urls :
if ' index.html ' in url :
index_html = ' <a href= " {} " >HTML report</a> ' . format ( url )
break
2021-09-16 19:35:38 +00:00
if not index_html :
2021-11-19 12:58:26 +00:00
commit . create_status ( context = NAME , description = ' PVS report failed to build ' , state = ' failure ' ,
target_url = f " { os . getenv ( ' GITHUB_SERVER_URL ' ) } / { os . getenv ( ' GITHUB_REPOSITORY ' ) } /actions/runs/ { os . getenv ( ' GITHUB_RUN_ID ' ) } " )
2021-09-16 19:35:38 +00:00
sys . exit ( 1 )
2021-09-16 15:51:43 +00:00
txt_report = os . path . join ( temp_path , TXT_REPORT_NAME )
warnings , errors = _process_txt_report ( txt_report )
errors = errors + warnings
status = ' success '
test_results = [ ( index_html , " Look at the report " ) , ( " Errors count not checked " , " OK " ) ]
description = " Total errors {} " . format ( len ( errors ) )
additional_logs = [ txt_report , os . path . join ( temp_path , ' pvs-studio.log ' ) ]
2021-11-12 11:39:00 +00:00
report_url = upload_results ( s3_helper , pr_info . number , pr_info . sha , test_results , additional_logs , NAME )
2021-09-16 15:51:43 +00:00
print ( " ::notice ::Report url: {} " . format ( report_url ) )
2021-10-20 11:25:14 +00:00
commit = get_commit ( gh , pr_info . sha )
commit . create_status ( context = NAME , description = description , state = status , target_url = report_url )
2021-11-19 14:47:04 +00:00
ch_helper = ClickHouseHelper ( )
prepared_events = prepare_tests_results_for_clickhouse ( pr_info , test_results , status , stopwatch . duration_seconds , stopwatch . start_time_str , report_url , NAME )
ch_helper . insert_events_into ( db = " gh-data " , table = " checks " , events = prepared_events )
2021-09-16 15:51:43 +00:00
except Exception as ex :
print ( " Got an exception " , ex )
sys . exit ( 1 )