Untitled

 avatar
unknown
plain_text
a year ago
7.7 kB
3
Indexable
  @Override
    public boolean isExtraDataExistInS3(int featureId, MultipartFile[] multipartFiles,
                                        ExtraDataType type) {
        if (multipartFiles.length != 1) return false;

        ExtraData extraData;
        try {
            extraData = new ExtraData(multipartFiles[0].getOriginalFilename(),
                    multipartFiles[0].getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return isExtraDataExistInS3(
                getFilePath(featureId, extraData.getName(), type));
    }
////////////////
    @Around(value = "target(com.sec.config.dashboard_api.service.FeatureExtraDataService)"
            + " && execution(* *..upload*(..))" + " && args(featureId, multipartFiles, type, ..)")
    private String logFeatureExtraDataUploaded(ProceedingJoinPoint joinPoint, int featureId,
                                               MultipartFile[] multipartFiles,
                                               ExtraDataType type) throws Throwable {
        ChangeType changeType =
                featureExtraDataService.isExtraDataExistInS3(featureId, multipartFiles, type)
                        ? ChangeType.UPDATED : ChangeType.CREATED;
        String endpointUrl = (String) joinPoint.proceed();

        if (!TextUtils.isEmpty(endpointUrl)) {
            String extraDataName =
                    endpointUrl.substring(endpointUrl.lastIndexOf("/") + 1, endpointUrl.length());
            Map<String, String> detail = new HashMap<>();
            detail.put("extraDataName", extraDataName);
            saveChangeLog(featureId, null, DataType.EXTRA_DATA,
                    changeType, toJsonString(detail));
        }

        return endpointUrl;
    }

    @AfterReturning(value = "target(com.sec.config.dashboard_api.service.FeatureExtraDataService)"
            + " && execution(* *..delete*(..))" + " && args(featureId, extraDataName)",
            returning = "success")
    private void logFeatureExtraDataDeleted(JoinPoint joinPoint, int featureId,
                                            String extraDataName, Boolean success) {
        if (success) {
            Map<String, String> detail = new HashMap<>();
            detail.put("extraDataName", extraDataName);

            saveChangeLog(featureId, null, DataType.EXTRA_DATA,
                    ChangeType.DELETED, toJsonString(detail));
        }
    }
//////////////////
import argparse
import mimetypes
import os
import sys
from io import BytesIO
from zipfile import ZipFile

import requests

import env
from presubmit.report_handler.jacoco_handler import JacocoHandler
from presubmit.report_handler.jest_handler import JestHandler
from presubmit.report_handler.py_coverage_handler import PyCoverageHandler
from utils import github_utils, common_utils, aws_util

SAM_INDICATORS = ['mcd', 'cc', 'cbo', 'loc', 'dc', 'floc', 'fme']

TEST_RESULT_URL = 'https://circleci.sec.samsung.net/gh/{account}/{repository}/{build_no}/artifacts/0{file_path}'

RESULT_HANDLER_MAP = {
    'jest': JestHandler(),
    'jacoco': JacocoHandler(),
    'py_coverage': PyCoverageHandler()
}

TARGET_MODULE = env.REPOSITORY


def generate_report_comment(result):
    msg = '''
**[SAM Result]**
Score : {}
Test Result : {}  
'''.format(result['project_data']['score2'],
           github_utils.get_artifact_page_url(env.REPOSITORY, env.RUN_ID))

    return msg


def extract_zip(zip_file, prefix):
    files = []
    with ZipFile(BytesIO(zip_file)) as zipfile:
        for file in zipfile.filelist:
            # Check directory
            if file.is_dir():
                continue

            if not file.filename.startswith(prefix):
                continue

            mimetype = mimetypes.guess_type(file.filename)[0]
            if mimetype is None:
                mimetype = 'binary/octet-stream'
            files.append({
                'path': file.filename.replace(prefix, '', 1),
                'data': zipfile.read(file.filename),
                'mimetype': mimetype})
    return files


def get_sam_result_files():
    download_url = github_utils.get_artifact_download_url(
        env.GITHUB_TOKEN, env.REPOSITORY, env.RUN_ID, 'report_sam')
    print(download_url)
    if not download_url:
        return []

    zip_file = github_utils.download_artifact(env.GITHUB_TOKEN, download_url)
    if not zip_file:
        return []

    return extract_zip(zip_file, 'html/')


def update_sam_result(total_result):
    upload_result_files(get_sam_result_files())

    upload_badge_to_s3('TOTAL', float(total_result['project_data']['score2']))
    for json_key in total_result.keys():
        item = json_key.replace('_data', '')
        if item in SAM_INDICATORS:
            try:
                upload_badge_to_s3(item.upper(), float(total_result[json_key]['score']))
            except KeyError:
                print('# unknown indicator : ' + json_key)
                continue


def upload_result_files(files):
    for file in files:
        aws_util.upload_binary(env.DEVOPS_TOOLS_BUCKET,
                               'sam/{}/{}'.format(TARGET_MODULE, file['path']),
                               file['data'],
                               content_type=file['mimetype'])


def upload_badge_to_s3(key, score):
    color = get_badge_color(score)
    badge_url = f'https://img.shields.io/badge/SAM%3A{key}-{score}-{color}.svg'

    print('# Downloading : ' + badge_url)
    response = requests.get(badge_url, stream=True)

    if not response.ok:
        sys.exit('response : ' + str(response))

    aws_util.upload_binary(env.DEVOPS_TOOLS_BUCKET,
                           'sam/{}/{}.svg'.format(TARGET_MODULE, key),
                           response.content,
                           content_type='image/svg+xml')


def get_badge_color(coverage):
    if coverage >= 5:
        return 'black'
    elif coverage >= 4.8:
        return 'blue'
    elif coverage >= 4.6:
        return 'brightgreen'
    elif coverage >= 4.5:
        return 'yellowgreen'
    elif coverage >= 4:
        return 'yellow'
    elif coverage >= 3:
        return 'orange'
    return 'red'


def parse_report(report_file, reporter, total_result):
    report_dict = common_utils.load_file_to_dic(report_file)
    result = RESULT_HANDLER_MAP[reporter].parse_report(report_dict)
    total_result.add_module(result)


def main(input_args):
    file = input_args.file

    update_dashboard = input_args.update
    if input_args.module:
        global TARGET_MODULE
        TARGET_MODULE = input_args.module

    if os.path.exists(file):
        sam_result = common_utils.load_file_to_dic(file)

    comment_body = generate_report_comment(sam_result)
    print(comment_body)

    github_utils.write_comment(env.GITHUB_TOKEN,
                               env.ACCOUNT, env.REPOSITORY,
                               env.COMMIT_ID,
                               comment_body)

    if env.TRIGGERED_BRANCH == 'dev' or update_dashboard is True:
        update_sam_result(sam_result)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--file', help='Report file',
                        required=True)
    parser.add_argument('-u', '--update', action='store_true', help='Update dashboard',
                        required=False)
    parser.add_argument('-m', '--module', help='Module', required=False)

    input_args = parser.parse_args()

    main(input_args)
Leave a Comment