示例#1
0
def sonar_main(config):
    """Main function to retrieve and process results of the SonarQube analysis

    Args:
        config - Fully Populated GripConfig object

    Returns:
        No returned value
    """
    global GLOBALS
    # Hardcoded to use default local SonarQube installation
    # You will likely need to change this for your specific situation
    authenticate = ("admin", "admin")
    url_str = ("http://localhost:9000/api/resources?scopes=PRJ&resource={0}"
               "&metrics=lines,ncloc,duplicated_lines,complexity,"
               "class_complexity,function_complexity,file_complexity,tests"
               "&format=json")
                                         
    url = url_str.format(config.sonarqube_project)
    print("Making Request for: '{0}'".format(url))
    sonar_data = get_rest(url, authenticate)

    if sonar_data is not None:
        print("Got back {} results...".format(len(sonar_data[0]['msr'])))
        # Define the look-up table that will map keys from the SonarQube
        # results to functions that will generate the corresponging
        # GripMeasurements
        lut = {"class_complexity":class_complexity
               ,"complexity":complexity
               ,"duplicated_lines":duplicate_lines
               ,"file_complexity":file_complexity
               ,"function_complexity":function_complexity
               ,"lines":lines_comments
               ,"ncloc":loc
               }
        GLOBALS['TIMESTAMP'] = gen_timestamp(sonar_data[0]['date'])
        measurements = []
        for v in sonar_data[0]['msr']:
            lut[v['key']](v, measurements)

        if measurements:
            for i in measurements:
                print(i)
            gen_json(measurements, config.json_basename + "-sq")
    else:
        err_str = "{0}Unable to retrieve issues for {1}.\n"
        sys.stderr.write(err_str.format(ERR_LABEL, ACCOUNT_NAME))
示例#2
0
def jira_main(config):
    """Main function for processing JIRA information

    Uses REST APIs to GET a project's issues and sprint information.
    Analyzes both to gather measurements that are interesting to Grip then
    produces a file containing a JSON representation of the extracted
    measurements

    Args:
        config - configuration object

    Returns:
        No return value
    """
    # set up the counters
    counters = Counters()
    # prepare for getting issues
    authenticate = (config.username, config.password)
    server = config.server
    api = config.jira_rest_api
    #
    # Get the list of projects.  We'll process issues for each project
    proj_url = "{0}{1}project".format(server, api)
    projects = get_rest(proj_url, authenticate)
    print("{0}Found {1} projects.".format(NOTE_LABEL, len(projects)))
    proj_name2key_map = build_proj_name2key_map(projects)
    # flag will be set to True if we find any issues in the projects
    found_issues = False
    for proj in projects:
        if proj['key'] in config.projects_to_analyze:
            query_str = ("search?jql=project={0}+order+by+created+asc"
                         "&startAt=0&expand=changelog&maxResults=-1")
            query = query_str.format(proj['key'])
            url = "{0}{1}{2}".format(server, api, query)
            print("\nProcessing project: {0}".format(proj['key']))
            print("Making Request for: {0}".format(url))    
            issues_rest = get_rest(url, authenticate)
            if issues_rest is not None:
                found_issues = True
                fstr = "{0}Retrieved {1} issues..."
                print(fstr.format(NOTE_LABEL, len(issues_rest['issues'])))
                for i in issues_rest['issues']:
                    # There should be a more elegant way of doing this, but the
                    # REST API apparently doesn't have a way to query for its
                    # own version
                    if api == "rest/api/2.0.alpha1/":
                        # obsolete version of the API, we'll need to adapt the
                        # issue for processing by these functions
                        i = adapt_2alpha1_issue(i
                                                ,config
                                                ,authenticate
                                                ,counters
                                                )
                    # Current version of the API
                    if i is not None:
                        if MEASUREMENTS_OUT:
                            proc_issue(i, config, counters)
                        else:
                            dump_issue(i, config, counters)
                    else:
                        err_str = "{0}Bad Issue Reference\n"
                        sys.stderr.write(err_str.format(ERR_LABEL))

    if found_issues:
        # Take care of the sprints
        sprint_api = config.sprint_api
        # "rest/greenhopper/1.0/"
        # If sprint_api isn't set in the configuration file, we'll skip
        # this step
        if sprint_api is not None:
            proc_sprints(server+sprint_api
                         ,proj_name2key_map
                         ,authenticate
                         ,counters
                         )
        # and the contributors
        handle_contributors(counters.contributors)

        # if we have measurements to pass along
        if counters.measurements:
            # Measurements were placed on the list in order of issue
            # processing.  Re-order by sorting on timestamp
            counters.measurements.sort(key=itemgetter(3))
            if GLOBALS['VERBOSE']:
                for i in counters.measurements:
                    print(i)

            gen_json(counters.measurements, config.json_basename + "-jira")
            
            # Dump the summary results
            fstr = ("\n\nCounter Class Summary:\n"
                    "  ISSUES OPEN   = {0}\n"
                    "  ISSUES CLOSED = {1}\n"
                    "  ISSUES TOTAL  = {2}\n\n"
                    "  REQUIREMENTS OPEN: {3}\n"
                    "  REQUIREMENTS CLOSED: {4}\n"
                    "  REQUIREMENTS TOTAL:  {5}\n\n"
                    "  DEFECTS CREATED = {6}\n"
                    "  DEFECTS CLOSED  = {7}\n"
                    "  DEFECTS TOTAL   = {8}\n\n"
                    "  SPRINTS TOTAL   = {9}\n\n"
                   )
            print(fstr.format(counters.issues.open
                              ,counters.issues.closed
                              ,counters.issues.total
                              ,counters.requirements.open
                              ,counters.requirements.closed
                              ,counters.requirements.total
                              ,counters.defects.created
                              ,counters.defects.closed
                              ,counters.defects.total
                              ,counters.sprints.total))

    else:
        err_str = "ERROR: Unable to retrieve issues for {0}.\n"
        sys.stderr.write(err_str.format(ACCOUNT_NAME))