254 lines
10 KiB
C#
254 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using CoverityWebService.ConfigurationService;
|
|
using CoverityWebService.DefectService;
|
|
|
|
using projectIdDataObj = CoverityWebService.DefectService.projectIdDataObj;
|
|
using streamIdDataObj = CoverityWebService.ConfigurationService.streamIdDataObj;
|
|
using snapshotIdDataObj = CoverityWebService.ConfigurationService.snapshotIdDataObj;
|
|
|
|
namespace CoverityWebService
|
|
{
|
|
public class ExtendedCoverityWebServices : CoverityWebServices
|
|
{
|
|
const int _defaultPageSize = 1000;
|
|
|
|
|
|
public ExtendedCoverityWebServices(string username, string password, string url):
|
|
base(username, password, url)
|
|
{
|
|
}
|
|
|
|
private projectDataObj GetProjectDataObj(string projectName)
|
|
{
|
|
// Create filter to get also the defect instances (so we can look it up)
|
|
// Get the set of stream defects aggregated by the given merged defect
|
|
return _configclient.getProjects(
|
|
new projectFilterSpecDataObj() { namePattern = projectName }).FirstOrDefault();
|
|
}
|
|
|
|
public streamDefectDataObj[] GetStreamDefects(long[] cids,
|
|
DateTime startDate = default(DateTime),
|
|
DateTime endDate = default(DateTime),
|
|
bool includeHistory = true,
|
|
bool includeDefectInstance = true)
|
|
{
|
|
// Create filter to get also the defect instances (so we can look it up)
|
|
var filter = new streamDefectFilterSpecDataObj();
|
|
filter.includeDefectInstances = includeDefectInstance;
|
|
filter.includeHistory = includeHistory;
|
|
if (startDate.CompareTo(default(DateTime)) != 0)
|
|
filter.defectStateStartDate = startDate;
|
|
if (endDate.CompareTo(default(DateTime)) != 0)
|
|
filter.defectStateStartDate = endDate;
|
|
|
|
// Get the set of stream defects aggregated by the given merged defect
|
|
return _defectclient.getStreamDefects(cids, filter);
|
|
}
|
|
|
|
public streamDataObj[] GetStreamDataObj(string projectName)
|
|
{
|
|
var projectDataObj = GetProjectDataObj(projectName);
|
|
if (projectDataObj != null)
|
|
return projectDataObj.streams;
|
|
return null;
|
|
}
|
|
|
|
public mergedDefectDataObj GetDefectDataObj(string projectName, long cid)
|
|
{
|
|
var filter = new mergedDefectFilterSpecDataObj();
|
|
filter.cidList = new long?[] { cid };
|
|
var page = new pageSpecDataObj() { pageSize = 1 };
|
|
var projectId = new projectIdDataObj();
|
|
projectId.name = projectName;
|
|
var defects = _defectclient.getMergedDefectsForProject(projectId, filter, page);
|
|
if (defects.mergedDefects != null && defects.mergedDefects.Length > 0)
|
|
{
|
|
return defects.mergedDefects[0];
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
|
|
public List<mergedDefectDataObj> GetDefectDataObjList(
|
|
string projectName,
|
|
List<string> statusFilters = null,
|
|
List<string> classificationFilters = null,
|
|
List<string> actionFilters = null,
|
|
List<string> severityFilters = null,
|
|
List<string> ownerFilters = null,
|
|
List<string> subcategoryFilters = null,
|
|
DateTime startDate = default(DateTime),
|
|
DateTime endDate = default(DateTime),
|
|
int pageSize = _defaultPageSize)
|
|
{
|
|
var CovProject = new projectIdDataObj();
|
|
CovProject.name = projectName;
|
|
var pageSpec = new pageSpecDataObj();
|
|
pageSpec.pageSize = pageSize;
|
|
|
|
var filter = new mergedDefectFilterSpecDataObj();
|
|
if (statusFilters != null)
|
|
filter.statusNameList = statusFilters.ToArray();
|
|
if (classificationFilters != null)
|
|
filter.classificationNameList = classificationFilters.ToArray();
|
|
if (actionFilters != null)
|
|
filter.actionNameList = actionFilters.ToArray();
|
|
if (severityFilters != null)
|
|
filter.severityNameList = severityFilters.ToArray();
|
|
if (ownerFilters != null)
|
|
filter.ownerNameList = ownerFilters.ToArray();
|
|
|
|
if (subcategoryFilters != null)
|
|
{
|
|
var subcategoryFilterList = new List<DefectService.checkerFilterSpecDataObj>();
|
|
|
|
foreach (string subcategory in subcategoryFilters)
|
|
{
|
|
checkerFilterSpecDataObj checkFilter = new checkerFilterSpecDataObj();
|
|
checkFilter.subcategory = subcategory;
|
|
subcategoryFilterList.Add(checkFilter);
|
|
}
|
|
|
|
filter.checkerFilterSpecList = subcategoryFilterList.ToArray();
|
|
}
|
|
|
|
if (startDate.CompareTo(default(DateTime)) != 0)
|
|
filter.firstDetectedStartDate = startDate;
|
|
if (endDate.CompareTo(default(DateTime)) != 0)
|
|
filter.firstDetectedEndDate = endDate;
|
|
|
|
var matchingDefects = new List<mergedDefectDataObj>();
|
|
int Count = 0;
|
|
while (true)
|
|
{
|
|
pageSpec.startIndex = Count;
|
|
|
|
var mergedDefectsForProject = _defectclient.getMergedDefectsForProject(CovProject, filter, pageSpec);
|
|
|
|
mergedDefectDataObj[] mergedDefects = mergedDefectsForProject.mergedDefects;
|
|
if (mergedDefects != null)
|
|
{
|
|
matchingDefects.AddRange(mergedDefects);
|
|
Count += mergedDefects.Length;
|
|
}
|
|
|
|
if (Count >= mergedDefectsForProject.totalNumberOfRecords)
|
|
break;
|
|
}
|
|
|
|
return matchingDefects;
|
|
}
|
|
|
|
public bool AssignIssueToUser(mergedDefectDataObj defect, string userName)
|
|
{
|
|
var streamDefects = GetStreamDefects(new long[] { defect.cid });
|
|
var defectActions = new defectStateSpecDataObj();
|
|
|
|
if (defect.classification.Equals("Unclassified"))
|
|
{
|
|
defectActions.classification = "Pending";
|
|
}
|
|
defectActions.owner = userName;
|
|
foreach (streamDefectDataObj sdo in streamDefects)
|
|
{
|
|
_defectclient.updateStreamDefects(new streamDefectIdDataObj[] { sdo.id }, defectActions);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public void UpdateExternalLink(mergedDefectDataObj defect, string externalLink)
|
|
{
|
|
streamDefectDataObj[] streamDef = GetStreamDefects(new long[] { defect.cid });
|
|
|
|
foreach (var streamDefect in streamDef)
|
|
{
|
|
if (streamDefect.externalReference != externalLink)
|
|
{
|
|
var defectActions = new defectStateSpecDataObj();
|
|
defectActions.externalReference = externalLink;
|
|
_defectclient.updateStreamDefects(new streamDefectIdDataObj[] { streamDefect.id }, defectActions);
|
|
}
|
|
}
|
|
}
|
|
|
|
public eventDataObj GetDefectEventDataObj(string projectName, mergedDefectDataObj defect)
|
|
{
|
|
var streamProject = GetStreamDataObj(projectName);
|
|
|
|
// Now check if we stored the tested Changelist as the "sourceVersion" (--version XXXX option for cov-commit-defects)
|
|
/*
|
|
snapshotIdDataObj snapId = new snapshotIdDataObj();
|
|
snapId.id = defect.lastDetectedSnapshotId;
|
|
// Recover the snapshotid of this specific defect
|
|
snapshotInfoDataObj[] snpInfo = _defectclient.getSnapshotInformation(new snapshotIdDataObj[] { snapId });
|
|
|
|
bool bCLFound = false;
|
|
long cl = -1;
|
|
if (!string.IsNullOrEmpty(snpInfo[0].sourceVersion))
|
|
{
|
|
// Save stored CL
|
|
bCLFound = long.TryParse(snpInfo[0].sourceVersion, out cl);
|
|
}
|
|
*/
|
|
// Create an array of Stream Ids
|
|
streamIdDataObj[] validStreamIds = (streamIdDataObj[])Array.CreateInstance(typeof(streamIdDataObj),streamProject.Length);
|
|
|
|
// Store the ids found in the validStreams passed to the function
|
|
for (int i = 0; i < streamProject.Length; i++)
|
|
{
|
|
validStreamIds[i] = streamProject[i].id;
|
|
}
|
|
|
|
var streamDefects = GetStreamDefects(new long[] { defect.cid }).Where(
|
|
streamdefect => streamdefect.defectInstances != null);
|
|
|
|
// Now check each defect
|
|
foreach (streamDefectDataObj streamdefect in streamDefects)
|
|
{
|
|
// Check if the stream is a valid stream for this project
|
|
if (validStreamIds.ToList().Exists(sid => sid.name == streamdefect.streamId.name) == false)
|
|
{
|
|
// If not move to next stream defect
|
|
continue;
|
|
}
|
|
// Now cycle through the defect instances
|
|
foreach (defectInstanceDataObj dsdo in streamdefect.defectInstances)
|
|
{
|
|
if (dsdo.events != null)
|
|
{
|
|
eventDataObj edo = null;
|
|
// Check the event
|
|
if (streamdefect.checkerSubcategory.Equals("UNINIT_CTOR") || streamdefect.checkerSubcategory.Equals("UNINIT"))
|
|
{
|
|
// If the defect is an UNINIT_CTOR or an UNINIT, then just store the last event
|
|
if (dsdo.events.Length > 1)
|
|
edo = dsdo.events[dsdo.events.Length - 1];
|
|
}
|
|
else
|
|
{
|
|
|
|
int i = 0;
|
|
// Stop on the first event which is not an "example"
|
|
while (i < dsdo.events.Length)
|
|
{
|
|
if (!dsdo.events[i].eventTag.StartsWith("example"))
|
|
{
|
|
edo = dsdo.events[i];
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
// We found the event we want
|
|
return edo;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|