<commit_before>/*******************************************************************************
 * Copyright (c) 2017 Synopsys, Inc
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Synopsys, Inc - initial implementation and documentation
 *******************************************************************************/
package jenkins.plugins.coverity.ws;


import java.io.IOException;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import com.coverity.ws.v9.CovRemoteServiceException_Exception;
import com.coverity.ws.v9.DefectService;
import com.coverity.ws.v9.MergedDefectDataObj;
import com.coverity.ws.v9.MergedDefectFilterSpecDataObj;
import com.coverity.ws.v9.MergedDefectsPageDataObj;
import com.coverity.ws.v9.PageSpecDataObj;
import com.coverity.ws.v9.SnapshotScopeSpecDataObj;
import com.coverity.ws.v9.StreamIdDataObj;

import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.model.Result;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import jenkins.plugins.coverity.CIMInstance;
import jenkins.plugins.coverity.CIMStream;
import jenkins.plugins.coverity.CoverityBuildAction;
import jenkins.plugins.coverity.CoverityDefect;
import jenkins.plugins.coverity.CoverityPublisher;
import jenkins.plugins.coverity.DefectFilters;
import org.apache.commons.lang.StringUtils;

/**
 * Class responsible for reading defects from Coverity after the commit process has been completed. The defects
 * will be added as a {@link CoverityBuildAction} to the build.
 */
public class DefectReader {
    private AbstractBuild<?, ?> build;
    private BuildListener listener;
    private CoverityPublisher publisher;

    public DefectReader(AbstractBuild<?, ?> build, BuildListener listener, CoverityPublisher publisher) {
        this.build = build;
        this.listener = listener;
        this.publisher = publisher;
    }

    public boolean getLatestDefectsForBuild()
    {
        if (publisher.isSkipFetchingDefects()) {
            // never get any defects when configured to skip fetching defects
            return false;
        }

        CIMStream cimStream = publisher.getCimStream();
        CIMInstance cimInstance = publisher.getDescriptor().getInstance(cimStream.getInstance());

        if (cimStream == null || StringUtils.isEmpty(cimStream.getStream())) {
            listener.getLogger().println("[Coverity] Stream has not been configured. Skipping fetching defects.");
            return false;
        }

        listener.getLogger().println(MessageFormat.format("[Coverity] Fetching defects for stream \"{0}\"", cimStream.getStream()));

        List<MergedDefectDataObj> defects = null;

        try {
            defects = getDefectsForSnapshot(cimInstance, cimStream, listener.getLogger());

            List<CoverityDefect> matchingDefects = new ArrayList<>();

            // Loop through all defects create defect objects
            for(MergedDefectDataObj defect : defects) {
                matchingDefects.add(new CoverityDefect(defect.getCid(), defect.getCheckerName(), defect.getFunctionDisplayName(), defect.getFilePathname()));
            }<commit_msg>Fix Dereference before null check issue<commit_after>/*******************************************************************************
 * Copyright (c) 2017 Synopsys, Inc
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Synopsys, Inc - initial implementation and documentation
 *******************************************************************************/
package jenkins.plugins.coverity.ws;


import java.io.IOException;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import com.coverity.ws.v9.CovRemoteServiceException_Exception;
import com.coverity.ws.v9.DefectService;
import com.coverity.ws.v9.MergedDefectDataObj;
import com.coverity.ws.v9.MergedDefectFilterSpecDataObj;
import com.coverity.ws.v9.MergedDefectsPageDataObj;
import com.coverity.ws.v9.PageSpecDataObj;
import com.coverity.ws.v9.SnapshotScopeSpecDataObj;
import com.coverity.ws.v9.StreamIdDataObj;

import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.model.Result;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import jenkins.plugins.coverity.CIMInstance;
import jenkins.plugins.coverity.CIMStream;
import jenkins.plugins.coverity.CoverityBuildAction;
import jenkins.plugins.coverity.CoverityDefect;
import jenkins.plugins.coverity.CoverityPublisher;
import jenkins.plugins.coverity.DefectFilters;
import org.apache.commons.lang.StringUtils;

/**
 * Class responsible for reading defects from Coverity after the commit process has been completed. The defects
 * will be added as a {@link CoverityBuildAction} to the build.
 */
public class DefectReader {
    private AbstractBuild<?, ?> build;
    private BuildListener listener;
    private CoverityPublisher publisher;

    public DefectReader(AbstractBuild<?, ?> build, BuildListener listener, CoverityPublisher publisher) {
        this.build = build;
        this.listener = listener;
        this.publisher = publisher;
    }

    public boolean getLatestDefectsForBuild()
    {
        if (publisher.isSkipFetchingDefects()) {
            // never get any defects when configured to skip fetching defects
            return false;
        }

        CIMStream cimStream = publisher.getCimStream();
        CIMInstance cimInstance = publisher.getDescriptor().getInstance(cimStream.getInstance());

        if (StringUtils.isEmpty(cimStream.getStream())) {
            listener.getLogger().println("[Coverity] Stream has not been configured. Skipping fetching defects.");
            return false;
        }

        listener.getLogger().println(MessageFormat.format("[Coverity] Fetching defects for stream \"{0}\"", cimStream.getStream()));

        List<MergedDefectDataObj> defects = null;

        try {
            defects = getDefectsForSnapshot(cimInstance, cimStream, listener.getLogger());

            List<CoverityDefect> matchingDefects = new ArrayList<>();

            // Loop through all defects create defect objects
            for(MergedDefectDataObj defect : defects) {
                matchingDefects.add(new CoverityDefect(defect.getCid(), defect.getCheckerName(), defect.getFunctionDisplayName(), defect.getFilePathname()));
            }
{'fraction_non_alphanumeric': 0.13421090284733153, 'fraction_numerical': 0.004864787523250823, 'mean_word_length': 4.2242152466367715, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3953922', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1887, 'n_words': 451}