Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
590867a0
Commit
590867a0
authored
Aug 28, 2018
by
Greg Messner
Browse files
Added methods to download job artifacts by job ID (#241).
parent
6971e41c
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/JobApi.java
View file @
590867a0
...
...
@@ -31,12 +31,13 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs
*
* @param projectId
the project ID to get the list of jobs for
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path
* @return a list containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobs
(
int
projectId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"jobs"
);
public
List
<
Job
>
getJobs
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
}
...
...
@@ -46,14 +47,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs
*
* @param projectId the project ID to get the
list of
jobs for
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project ID
or path
to get the jobs for
* @param page the page to get
* @param perPage the number of Job instances per page
* @return a list containing the jobs for the specified project ID in the specified page range
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobs
(
in
t
projectId
,
int
page
,
int
perPage
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getPageQueryParams
(
page
,
perPage
),
"projects"
,
projectId
,
"jobs"
);
public
List
<
Job
>
getJobs
(
Objec
t
projectId
OrPath
,
int
page
,
int
perPage
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getPageQueryParams
(
page
,
perPage
),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
}
...
...
@@ -63,13 +64,13 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs
*
* @param projectId the project ID to get the
list of
jobs for
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project ID
or path
to get the jobs for
* @param itemsPerPage the number of Job instances that will be fetched per page
* @return a Pager containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
Pager
<
Job
>
getJobs
(
in
t
projectId
,
int
itemsPerPage
)
throws
GitLabApiException
{
return
(
new
Pager
<
Job
>(
this
,
Job
.
class
,
itemsPerPage
,
null
,
"projects"
,
projectId
,
"jobs"
));
public
Pager
<
Job
>
getJobs
(
Objec
t
projectId
OrPath
,
int
itemsPerPage
)
throws
GitLabApiException
{
return
(
new
Pager
<
Job
>(
this
,
Job
.
class
,
itemsPerPage
,
null
,
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
));
}
/**
...
...
@@ -77,14 +78,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs
*
* @param projectId the project ID to get the
list of
jobs for
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project ID
or path
to get the jobs for
* @param scope the scope of jobs, one of: CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL
* @return a list containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobs
(
in
t
projectId
,
JobScope
scope
)
throws
GitLabApiException
{
public
List
<
Job
>
getJobs
(
Objec
t
projectId
OrPath
,
JobScope
scope
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"scope"
,
scope
).
withParam
(
PER_PAGE_PARAM
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"jobs"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{}));
}
...
...
@@ -93,14 +94,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/pipelines/:pipeline_id/jobs
*
* @param projectId
the project ID to get the list of
pipeline for
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path to get the
pipeline
s
for
* @param pipelineId the pipeline ID to get the list of jobs for
* @return a list containing the jobs for the specified project ID and pipeline ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobsForPipeline
(
in
t
projectId
,
int
pipelineId
)
throws
GitLabApiException
{
public
List
<
Job
>
getJobsForPipeline
(
Objec
t
projectId
OrPath
,
int
pipelineId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"pipelines"
,
pipelineId
,
"jobs"
);
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"pipelines"
,
pipelineId
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{}));
}
...
...
@@ -109,15 +110,15 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/pipelines/:pipeline_id/jobs
*
* @param projectId
the project ID to get the list of
pipeline for
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path to get the
pipeline
s
for
* @param pipelineId the pipeline ID to get the list of jobs for
* @param scope the scope of jobs, one of: CREATED, PENDING, RUNNING, FAILED, SUCCESS, CANCELED, SKIPPED, MANUAL
* @return a list containing the jobs for the specified project ID and pipeline ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobsForPipeline
(
in
t
projectId
,
int
pipelineId
,
JobScope
scope
)
throws
GitLabApiException
{
public
List
<
Job
>
getJobsForPipeline
(
Objec
t
projectId
OrPath
,
int
pipelineId
,
JobScope
scope
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"scope"
,
scope
).
withParam
(
PER_PAGE_PARAM
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"pipelines"
,
pipelineId
,
"jobs"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"pipelines"
,
pipelineId
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{}));
}
...
...
@@ -126,13 +127,13 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/:job_id
*
* @param projectId the project ID to get the
specified
job for
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project ID
or path
to get the job for
* @param jobId the job ID to get
* @return a single job for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
getJob
(
in
t
projectId
,
int
jobId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"jobs"
,
jobId
);
public
Job
getJob
(
Objec
t
projectId
OrPath
,
int
jobId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -141,13 +142,13 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/:job_id
*
* @param projectId the project ID to get the
specified
job for
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project ID
or path
to get the job for
* @param jobId the job ID to get
* @return a single job for the specified project ID as an Optional intance
*/
public
Optional
<
Job
>
getOptionalJob
(
in
t
projectId
,
int
jobId
)
{
public
Optional
<
Job
>
getOptionalJob
(
Objec
t
projectId
OrPath
,
int
jobId
)
{
try
{
return
(
Optional
.
ofNullable
(
getJob
(
projectId
,
jobId
)));
return
(
Optional
.
ofNullable
(
getJob
(
projectId
OrPath
,
jobId
)));
}
catch
(
GitLabApiException
glae
)
{
return
(
GitLabApi
.
createOptionalFromException
(
glae
));
}
...
...
@@ -160,18 +161,18 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/artifacts/:ref_name/download?job=name
*
* @param projectId
the ID of
the project
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project
ID or path
* @param ref the ref from a repository
* @param jobName the name of the job to download the artifacts for
* @param directory the File instance of the directory to save the file to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the download of the specified artifacts file
* @throws GitLabApiException if any exception occurs
*/
public
File
downloadArtifactsFile
(
Integer
projectId
,
String
ref
,
String
jobName
,
File
directory
)
throws
GitLabApiException
{
public
File
downloadArtifactsFile
(
Object
projectId
OrPath
,
String
ref
,
String
jobName
,
File
directory
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"job"
,
jobName
,
true
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"jobs"
,
"artifacts"
,
ref
,
"download"
);
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
"artifacts"
,
ref
,
"download"
);
try
{
...
...
@@ -197,16 +198,66 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/artifacts/:ref_name/download?job=name
*
* @param projectId
the ID of
the project
* @param projectId
OrPath id, path of the project, or a Project instance holding
the project
ID or path
* @param ref the ref from a repository
* @param jobName the name of the job to download the artifacts for
* @return an InputStream to read the specified artifacts file from
* @throws GitLabApiException if any exception occurs
*/
public
InputStream
downloadArtifactsFile
(
Integer
projectId
,
String
ref
,
String
jobName
)
throws
GitLabApiException
{
public
InputStream
downloadArtifactsFile
(
Object
projectId
OrPath
,
String
ref
,
String
jobName
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"job"
,
jobName
,
true
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"jobs"
,
"artifacts"
,
ref
,
"download"
);
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"jobs"
,
"artifacts"
,
ref
,
"download"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
/**
* Download the job artifacts file for the specified job ID. The artifacts file will be saved in the
* specified directory with the following name pattern: job-{jobid}-artifacts.zip. If the file already
* exists in the directory it will be overwritten.
*
* GET /projects/:id/jobs/:job_id/artifacts
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the job ID to get the artifacts for
* @param directory the File instance of the directory to save the file to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the download of the specified job artifacts file
* @throws GitLabApiException if any exception occurs
*/
public
File
downloadArtifactsFile
(
Object
projectIdOrPath
,
Integer
jobId
,
File
directory
)
throws
GitLabApiException
{
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
null
,
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"jobs"
,
jobId
,
"artifacts"
);
try
{
if
(
directory
==
null
)
directory
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
));
String
filename
=
"job-"
+
jobId
+
"-artifacts.zip"
;
File
file
=
new
File
(
directory
,
filename
);
InputStream
in
=
response
.
readEntity
(
InputStream
.
class
);
Files
.
copy
(
in
,
file
.
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
return
(
file
);
}
catch
(
IOException
ioe
)
{
throw
new
GitLabApiException
(
ioe
);
}
}
/**
* Get an InputStream pointing to the job artifacts file for the specified job ID.
*
* GET /projects/:id/jobs/:job_id/artifacts
*
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the job ID to get the artifacts for
* @return an InputStream to read the specified job artifacts file
* @throws GitLabApiException if any exception occurs
*/
public
InputStream
downloadArtifactsFile
(
Object
projectIdOrPath
,
Integer
jobId
)
throws
GitLabApiException
{
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
null
,
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"jobs"
,
jobId
,
"artifacts"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
...
...
@@ -217,16 +268,16 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/:job_id/artifacts/*artifact_path
*
* @param projectId
The ID
of the project o
wned by the authenticated user
* @param jobId
T
he unique job identifier
* @param artifactPath Path to a file inside the artifacts archive
* @param projectId
OrPath id, path
of the project
,
o
r a Project instance holding the project ID or path
* @param jobId
t
he unique job identifier
* @param artifactPath
the
Path to a file inside the artifacts archive
* @param directory the File instance of the directory to save the file to, if null will use "java.io.tmpdir"
* @return a File instance pointing to the download of the specified artifacts file
* @throws GitLabApiException if any exception occurs
*/
public
File
downloadSingleArtifactsFile
(
Integer
projectId
,
Integer
jobId
,
Path
artifactPath
,
File
directory
)
throws
GitLabApiException
{
public
File
downloadSingleArtifactsFile
(
Object
projectId
OrPath
,
Integer
jobId
,
Path
artifactPath
,
File
directory
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"jobs"
,
jobId
,
"artifacts"
,
artifactPath
);
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"artifacts"
,
artifactPath
);
try
{
if
(
directory
==
null
)
...
...
@@ -251,14 +302,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/:job_id/artifacts/*artifact_path
*
* @param projectId
The ID
of the project o
wned by the authenticated user
* @param jobId
T
he unique job identifier
* @param artifactPath Path to a file inside the artifacts archive
* @param projectId
OrPath id, path
of the project
,
o
r a Project instance holding the project ID or path
* @param jobId
t
he unique job identifier
* @param artifactPath
the
Path to a file inside the artifacts archive
* @return an InputStream to read the specified artifacts file from
* @throws GitLabApiException if any exception occurs
*/
public
InputStream
downloadSingleArtifactsFile
(
Integer
projectId
,
Integer
jobId
,
Path
artifactPath
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"jobs"
,
jobId
,
"artifacts"
,
artifactPath
);
public
InputStream
downloadSingleArtifactsFile
(
Object
projectId
OrPath
,
Integer
jobId
,
Path
artifactPath
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"artifacts"
,
artifactPath
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
...
...
@@ -267,13 +318,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* GET /projects/:id/jobs/:id/trace
*
* @param projectId the project ID to get the specified job's trace for
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
* to get the specified job's trace for
* @param jobId the job ID to get the trace for
* @return a String containing the specified job's trace
* @throws GitLabApiException if any exception occurs during execution
*/
public
String
getTrace
(
in
t
projectId
,
int
jobId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"jobs"
,
jobId
,
"trace"
);
public
String
getTrace
(
Objec
t
projectId
OrPath
,
int
jobId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"trace"
);
return
(
response
.
readEntity
(
String
.
class
));
}
...
...
@@ -282,14 +334,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* POST /projects/:id/jobs/:job_id/cancel
*
* @param projectId
the project ID to cancel specified job
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the ID to cancel job
* @return job instance which just canceled
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
cancleJob
(
in
t
projectId
,
int
jobId
)
throws
GitLabApiException
{
public
Job
cancleJob
(
Objec
t
projectId
OrPath
,
int
jobId
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
null
;
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"jobs"
,
jobId
,
"cancel"
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"cancel"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -298,14 +350,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* POST /projects/:id/jobs/:job_id/retry
*
* @param projectId
the project ID to retry speficied job
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the ID to retry job
* @return job instance which just retried
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
retryJob
(
in
t
projectId
,
int
jobId
)
throws
GitLabApiException
{
public
Job
retryJob
(
Objec
t
projectId
OrPath
,
int
jobId
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
null
;
Response
response
=
post
(
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"jobs"
,
jobId
,
"retry"
);
Response
response
=
post
(
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"retry"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -314,14 +366,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* POST /projects/:id/jobs/:job_id/erase
*
* @param projectId
the project ID to erase specified job
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the ID to erase job
* @return job instance which just erased
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
eraseJob
(
in
t
projectId
,
int
jobId
)
throws
GitLabApiException
{
public
Job
eraseJob
(
Objec
t
projectId
OrPath
,
int
jobId
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
null
;
Response
response
=
post
(
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"jobs"
,
jobId
,
"erase"
);
Response
response
=
post
(
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"erase"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -330,14 +382,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* POST /projects/:id/jobs/:job_id/play
*
* @param projectId
the project ID to play specified job
* @param projectId
OrPath id, path of the project, or a Project instance holding the project ID or path
* @param jobId the ID to play job
* @return job instance which just played
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
playJob
(
in
t
projectId
,
int
jobId
)
throws
GitLabApiException
{
public
Job
playJob
(
Objec
t
projectId
OrPath
,
int
jobId
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
null
;
Response
response
=
post
(
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"jobs"
,
jobId
,
"play"
);
Response
response
=
post
(
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"jobs"
,
jobId
,
"play"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment