Retrieve Active Jobs for a Project

Coverity on Polaris

Version
latest
You can use the Coverity on Polaris API to retrieve active jobs for a project with the following cURL, Python, and Java API examples.

cURL: Retrieve the Active Jobs for a Project

  1. Get the project list and extract a project ID.
    $ curl -X GET \
    'https://subdomain.cop.blackduck.com/api/common/v0/projects?page[limit]=10&page[offset]=0' \
    -H 'Accept: application/vnd.api+json' \
    -H 'Authorization: Bearer {JWT}'
  2. Then, use the project ID to get the list of running jobs.
    $ curl -X GET \
    'https://subdomain.cop.blackduck.com/api/jobs/v2/jobs?filter[jobs][project][id]={{project-id}}&filter[jobs][status][state]=RUNNING&page[limit]=25&page[offset]=0' \
    -H 'Accept: application/vnd.api+json' \
    -H 'Authorization: Bearer {JWT}'
    

Python: Retrieve the Active Jobs for a Project


 
 
    import json
    from http.client import HTTPSConnection, HTTPResponse
    
    polaris = HTTPSConnection('subdomain.cop.blackduck.com')
    
    jwt = ''
    
       json_api_resource_headers = {
       'Content-Type': "application/vnd.api+json",
       'Accept': "application/vnd.api+json",
       'Authorization': 'Bearer ' + jwt
       }
    
    project_name = ''
    
    # step 1 get the project list, extract a project ID
    polaris.request("GET", f"/api/common/v0/projects?filter[project][name][$eq]={project_name}&page[limit]=1&page[offset]=0", None, json_api_resource_headers)
    projects_response = polaris.getresponse()
    print(f'Projects: \n{json.dumps(projects_response.read(), indent=4)}')
    
    project_id = ''
    
    # step 2 use the project ID to get the list of running jobs
    polaris.request("GET", f"/api/jobs/v2/jobs?filter[jobs][project][id]={project_id}&filter[jobs][status][state]=RUNNING&page[limit]=25&page[offset]=0", None, json_api_resource_headers)
    jobs_response = polaris.getresponse()
    print(f'Running jobs: \n{json.dumps(jobs_response.read(), indent=4)}')                   
                    

Java: Retrieve the Active Jobs for a Project


    
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    
    final static String JSON_API_STRING = "application/vnd.api+json";
    
    String jwt = "";
    String host = "https://subdomain.cop.blackduck.com";
    
    OkHttpClient client = new OkHttpClient();
    
    String projectName = "";
    
    // step 1 get the project list, extract a project ID
    Request getProjectRequest = new Request.Builder()
        .url(host+"/api/common/v0/projects?filter[project][name][$eq]="+projectName+"&page[limit]=1&page[offset]=0")
        .get()
        .addHeader("Accept", JSON_API_STRING)
        .addHeader("Authorization", "Bearer "+jwt)
        .build();
    
    Response getProjectResponse = client.newCall(getProjectRequest).execute();
    
    String projectName = "";
    
    // step 2 use the project ID to get the list of running jobs
    Request getRunningJobsRequest = new Request.Builder()
        .url(host+"/api/jobs/v2/jobs?filter[jobs][project][id]="+projectId+"&filter[jobs][status][state]=RUNNING&page[limit]=25&page[offset]=0")
        .get()
        .addHeader("Accept", JSON_API_STRING)
        .addHeader("Authorization", "Bearer "+jwt)
        .build();
    
    Response getRunningJobsResponse = client.newCall(getRunningJobsRequest).execute();