Package com.codedx.api.client
Class CodeDxClient
- java.lang.Object
-
- com.codedx.api.client.CodeDxClient
-
- Direct Known Subclasses:
CodeDxRepeatingClient
public class CodeDxClient extends Object
A RESTful client used to access the various API end-points exposed by CodeDx.- Author:
- anthonyd
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CodeDxClient.CodeDxVersionResponse
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.http.impl.client.HttpClientBuilder
httpClientBuilder
protected String
key
protected String
url
-
Constructor Summary
Constructors Constructor Description CodeDxClient(String url, String key)
Creates a new client, ready to be used for communications with CodeDx.CodeDxClient(String url, String key, org.apache.http.impl.client.HttpClientBuilder clientBuilder)
Creates a new client, ready to be used for communications with CodeDx.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description String
buildBrowsableAnalysisRunUrl(int projectId)
Deprecated.String
buildLatestFindingsUrl(int projectId)
protected <T> T
doHttpRequest(org.apache.http.client.methods.HttpRequestBase request, String path, boolean isXApi, Type responseType, Object requestBody)
Perform an HttpRequest to the given api path, with an optional request body, and parse the responseCodeDxVersion
getCodeDxVersion()
int
getFindingsCount(ProjectContext project, Filter filter)
Retrieves the total findings count for a given project using the provided FilterList<CountGroup>
getFindingsGroupedCounts(ProjectContext project, Filter filter, String countBy)
Retrieves an array of CountGroups using the provided Filter and countBy field name.StartAnalysisResponse
getGitJobResult(String id)
Retrieves the result of a Git fetch job from Code DxJob
getJob(String id)
Retrieves a specific job from CodeDxString
getJobStatus(String id)
Retrieves a job status from CodeDx.Project
getProject(ProjectContext project)
Retrieves a specific project from CodeDxList<Branch>
getProjectBranches(ProjectContext project)
GitConfigResponse
getProjectGitConfig(ProjectContext project)
List<Project>
getProjects()
Retrieves a list of projects from CodeDx.boolean
projectPolicyShouldBreakTheBuild(ProjectContext project)
void
setAnalysisName(ProjectContext project, int analysisId, String name)
StartAnalysisResponse
startAnalysis(int projectId, boolean includeGitSource, String specificGitBranch, String parentBranchName, String targetBranchName, Map<String,InputStream> artifacts)
Kicks off a CodeDx analysis run on a specified project (Requires projectId instead of ProjectContext since the use of ProjectContext differs for this endpoint compared to others.
-
-
-
Constructor Detail
-
CodeDxClient
public CodeDxClient(String url, String key)
Creates a new client, ready to be used for communications with CodeDx.- Parameters:
url
- URL of the CodeDx web application. The '/api' part of the URL is optional.key
- The API key. Note that permissions must be set for this key on CodeDx admin page.
-
CodeDxClient
public CodeDxClient(String url, String key, org.apache.http.impl.client.HttpClientBuilder clientBuilder)
Creates a new client, ready to be used for communications with CodeDx.- Parameters:
url
- URL of the CodeDx web application. The '/api' part of the URL is optional.key
- The API key. Note that permissions must be set for this key on CodeDx admin page.clientBuilder
- an HttpClientBuilder that can handle the certificate used by the server
-
-
Method Detail
-
buildBrowsableAnalysisRunUrl
@Deprecated public String buildBrowsableAnalysisRunUrl(int projectId)
Deprecated.
-
buildLatestFindingsUrl
public String buildLatestFindingsUrl(int projectId)
-
getProjects
public List<Project> getProjects() throws CodeDxClientException, IOException
Retrieves a list of projects from CodeDx.- Returns:
- Project list
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getProject
public Project getProject(ProjectContext project) throws CodeDxClientException, IOException
Retrieves a specific project from CodeDx- Parameters:
project
- The project context (project ID and optional branch name)- Returns:
- A project
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getJob
public Job getJob(String id) throws CodeDxClientException, IOException
Retrieves a specific job from CodeDx- Parameters:
id
- The job ID- Returns:
- A Job
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getGitJobResult
public StartAnalysisResponse getGitJobResult(String id) throws CodeDxClientException, IOException
Retrieves the result of a Git fetch job from Code Dx- Parameters:
id
- The job ID- Returns:
- A response with the resulting analysis ID
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getJobStatus
public String getJobStatus(String id) throws CodeDxClientException, IOException
Retrieves a job status from CodeDx. This is a convenience method for polling that relies on getJob.- Parameters:
id
- The job ID- Returns:
- The Job Status
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getFindingsCount
public int getFindingsCount(ProjectContext project, Filter filter) throws CodeDxClientException, IOException
Retrieves the total findings count for a given project using the provided Filter- Parameters:
project
- The project contextfilter
- A Filter object (set to null to not filter)- Returns:
- The count
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getFindingsGroupedCounts
public List<CountGroup> getFindingsGroupedCounts(ProjectContext project, Filter filter, String countBy) throws CodeDxClientException, IOException
Retrieves an array of CountGroups using the provided Filter and countBy field name.- Parameters:
project
- The project context (project ID and optional branch name)filter
- A Filter objectcountBy
- The field to group the counts by- Returns:
- A list of CountGroups
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
setAnalysisName
public void setAnalysisName(ProjectContext project, int analysisId, String name) throws IOException, CodeDxClientException
- Parameters:
project
- The project context (project ID and optional branch name)analysisId
- The analysis IDname
- The name to set for the analysis- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
getCodeDxVersion
public CodeDxVersion getCodeDxVersion() throws IOException, CodeDxClientException
- Throws:
IOException
CodeDxClientException
-
getProjectBranches
public List<Branch> getProjectBranches(ProjectContext project) throws IOException, CodeDxClientException
- Throws:
IOException
CodeDxClientException
-
projectPolicyShouldBreakTheBuild
public boolean projectPolicyShouldBreakTheBuild(ProjectContext project) throws IOException, CodeDxClientException
- Throws:
IOException
CodeDxClientException
-
getProjectGitConfig
public GitConfigResponse getProjectGitConfig(ProjectContext project) throws IOException, CodeDxClientException
- Throws:
IOException
CodeDxClientException
-
doHttpRequest
protected <T> T doHttpRequest(org.apache.http.client.methods.HttpRequestBase request, String path, boolean isXApi, Type responseType, Object requestBody) throws IOException, CodeDxClientException
Perform an HttpRequest to the given api path, with an optional request body, and parse the response- Type Parameters:
T
- Type parameter that determines the parsed response type- Parameters:
request
- Generally a new `HttpGet`, `HttpPost`, or `HttpPut`path
- The relative API path (not including /x/ or /api/)isXApi
- Flag that determines whether the request will prepend /x/ or /api/ to the path (true = /x/)responseType
- A type instance that helps `gson` parse the response bodyrequestBody
- An optional payload that will be converted to json and sent with the request- Returns:
- The parsed response
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
startAnalysis
public StartAnalysisResponse startAnalysis(int projectId, boolean includeGitSource, String specificGitBranch, String parentBranchName, String targetBranchName, Map<String,InputStream> artifacts) throws IOException, CodeDxClientException
Kicks off a CodeDx analysis run on a specified project (Requires projectId instead of ProjectContext since the use of ProjectContext differs for this endpoint compared to others. Other endpoints use the ProjectContext to indicate target project and branch, but this uses ProjectContext to determine parent/base branch.)- Parameters:
projectId
- The project IDincludeGitSource
- Whether Code Dx should fetch the latest source from Git before analysisspecificGitBranch
- The specific Git branch to fetch (ignored if includeGitSource=false)parentBranchName
- The Code Dx branch to use as the parent if targetBranchName is specified and does not yet existtargetBranchName
- The Code Dx branch to store findings inartifacts
- An array of streams to send over as analysis artifacts- Returns:
- A StartAnalysisResponse object
- Throws:
IOException
- If the underlying IO goes wrongCodeDxClientException
- For non 2xx response codes
-
-