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
a635acf6
Commit
a635acf6
authored
Jul 10, 2017
by
Greg Messner
Browse files
Finalized Job API support.
parent
280fd2ec
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/GitLabApi.java
View file @
a635acf6
...
...
@@ -13,8 +13,10 @@ import org.gitlab4j.api.models.Version;
*/
public
class
GitLabApi
{
public
static
final
int
DEFAULT_PER_PAGE
=
9999
;
/** GitLab4J default per page. GitLab will ignore anything over 100. */
public
static
final
int
DEFAULT_PER_PAGE
=
100
;
/** Specifies the version of the GitLab API to communicate with. */
public
enum
ApiVersion
{
V3
,
V4
;
...
...
src/main/java/org/gitlab4j/api/JobApi.java
View file @
a635acf6
package
org.gitlab4j.api
;
import
org.gitlab4j.api.models.Job
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.StandardCopyOption
;
import
java.util.List
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.GenericType
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response.Status
;
import
org.gitlab4j.api.models.Job
;
/**
* This class provides an entry point to all the GitLab API job calls.
*/
...
...
@@ -25,9 +33,8 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobsFromProjectId
(
int
projectId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"jobs"
);
public
List
<
Job
>
getJobs
(
int
projectId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
}
...
...
@@ -43,10 +50,8 @@ public class JobApi extends AbstractApi implements Constants {
* @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
>
getJobsFromProjectId
(
int
projectId
,
int
page
,
int
perPage
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getPageQueryParams
(
page
,
perPage
),
"projects"
,
projectId
,
"jobs"
);
public
List
<
Job
>
getJobs
(
int
projectId
,
int
page
,
int
perPage
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getPageQueryParams
(
page
,
perPage
),
"projects"
,
projectId
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
}
...
...
@@ -61,8 +66,7 @@ public class JobApi extends AbstractApi implements Constants {
* @return a Pager containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
Pager
<
Job
>
getJobsFromProjectId
(
int
projectId
,
int
itemsPerPage
)
throws
GitLabApiException
{
public
Pager
<
Job
>
getJobs
(
int
projectId
,
int
itemsPerPage
)
throws
GitLabApiException
{
return
(
new
Pager
<
Job
>(
this
,
Job
.
class
,
itemsPerPage
,
null
,
"projects"
,
projectId
,
"jobs"
));
}
...
...
@@ -76,14 +80,10 @@ public class JobApi extends AbstractApi implements Constants {
* @return a list containing the jobs for the specified project ID
* @throws GitLabApiException if any exception occurs during execution
*/
public
List
<
Job
>
getJobsFromProjectId
(
int
projectId
,
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"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
public
List
<
Job
>
getJobs
(
int
projectId
,
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"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{}));
}
/**
...
...
@@ -96,12 +96,10 @@ public class JobApi extends AbstractApi implements Constants {
* @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
>
getJobsFromPipelineId
(
int
projectId
,
int
pipelineId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"pipelines"
,
pipelineId
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
public
List
<
Job
>
getJobsForPipeline
(
int
projectId
,
int
pipelineId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"projects"
,
projectId
,
"pipelines"
,
pipelineId
,
"jobs"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{}));
}
/**
...
...
@@ -115,15 +113,10 @@ public class JobApi extends AbstractApi implements Constants {
* @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
>
getJobsFromPipelineId
(
int
projectId
,
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"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{
}));
public
List
<
Job
>
getJobsForPipeline
(
int
projectId
,
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"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Job
>>()
{}));
}
/**
...
...
@@ -141,20 +134,76 @@ public class JobApi extends AbstractApi implements Constants {
return
(
response
.
readEntity
(
Job
.
class
));
}
/**
* Download the artifacts file from the given reference name and job provided the job finished successfully.
* The file will be saved to the specified directory. If the file already exists in the directory it will
* be overwritten.
*
* GET /projects/:id/jobs/artifacts/:ref_name/download?job=name
*
* @param projectId the ID of the project
* @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
{
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"
);
try
{
if
(
directory
==
null
)
directory
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
));
String
filename
=
jobName
+
"-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 artifacts file from the given reference name and job
* provided the job finished successfully. The file will be saved to the specified directory.
* If the file already exists in the directory it will be overwritten.
*
* GET /projects/:id/jobs/artifacts/:ref_name/download?job=name
*
* @param projectId the ID of the project
* @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
{
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"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
/**
* Cancel specified job in a project.
*
* POST /projects/:id/jobs/:job_id/cancel
*
* @param projectId the project ID to cancel spe
fic
ied job
* @param projectId the project ID to cancel spe
cif
ied job
* @param jobId the ID to cancel job
* @return job instance which just canceled
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
cancleJob
(
int
projectId
,
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"
,
projectId
,
"jobs"
,
jobId
,
"cancel"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -170,8 +219,7 @@ public class JobApi extends AbstractApi implements Constants {
*/
public
Job
retryJob
(
int
projectId
,
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"
,
projectId
,
"jobs"
,
jobId
,
"retry"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -180,15 +228,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* POST /projects/:id/jobs/:job_id/erase
*
* @param projectId the project ID to erase spe
fic
ied job
* @param projectId the project ID to erase spe
cif
ied job
* @param jobId the ID to erase job
* @return job instance which just erased
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
eraseJob
(
int
projectId
,
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"
,
projectId
,
"jobs"
,
jobId
,
"erase"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
...
...
@@ -197,15 +244,14 @@ public class JobApi extends AbstractApi implements Constants {
*
* POST /projects/:id/jobs/:job_id/play
*
* @param projectId the project ID to play spe
fic
ied job
* @param projectId the project ID to play spe
cif
ied job
* @param jobId the ID to play job
* @return job instance which just played
* @throws GitLabApiException if any exception occurs during execution
*/
public
Job
playJob
(
int
projectId
,
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"
,
projectId
,
"jobs"
,
jobId
,
"play"
);
return
(
response
.
readEntity
(
Job
.
class
));
}
}
src/main/java/org/gitlab4j/api/RepositoryFileApi.java
View file @
a635acf6
...
...
@@ -187,7 +187,7 @@ public class RepositoryFileApi extends AbstractApi {
* @param projectId the ID of the project
* @param commitOrBranchName the commit or branch name to get the file contents for
* @param filepath the path of the file to get
* @return a
string with the file content for the specified file
* @return a
n InputStream to read the raw file from
* @throws GitLabApiException if any exception occurs
*/
public
InputStream
getRawFile
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
...
...
src/main/java/org/gitlab4j/api/models/Job.java
View file @
a635acf6
...
...
@@ -12,15 +12,15 @@ public class Job {
private
Integer
id
;
private
Commit
commit
;
private
String
coverage
;
private
Date
created
_a
t
;
private
Date
finished
_a
t
;
private
Date
created
A
t
;
private
Date
finished
A
t
;
private
String
name
;
private
Pipeline
pipeline
;
private
String
ref
;
private
Runner
runner
;
private
User
user
;
private
Date
started
_a
t
;
private
ArtifactsFile
artifacts
_f
ile
;
private
Date
started
A
t
;
private
ArtifactsFile
artifacts
F
ile
;
private
Boolean
tag
;
private
String
stage
;
private
JobStatus
status
;
...
...
@@ -41,20 +41,20 @@ public class Job {
this
.
commit
=
commit
;
}
public
Date
getCreated
_a
t
()
{
return
created
_a
t
;
public
Date
getCreated
A
t
()
{
return
created
A
t
;
}
public
void
setCreated
_a
t
(
Date
created
_a
t
)
{
this
.
created
_a
t
=
created
_a
t
;
public
void
setCreated
A
t
(
Date
created
A
t
)
{
this
.
created
A
t
=
created
A
t
;
}
public
Date
getFinished
_a
t
()
{
return
finished
_a
t
;
public
Date
getFinished
A
t
()
{
return
finished
A
t
;
}
public
void
setFinished
_a
t
(
Date
finished
_a
t
)
{
this
.
finished
_a
t
=
finished
_a
t
;
public
void
setFinished
A
t
(
Date
finished
A
t
)
{
this
.
finished
A
t
=
finished
A
t
;
}
public
String
getName
()
{
...
...
@@ -89,12 +89,12 @@ public class Job {
this
.
user
=
user
;
}
public
Date
getStarted
_a
t
()
{
return
started
_a
t
;
public
Date
getStarted
A
t
()
{
return
started
A
t
;
}
public
void
setStarted
_a
t
(
Date
started
_a
t
)
{
this
.
started
_a
t
=
started
_a
t
;
public
void
setStarted
A
t
(
Date
started
A
t
)
{
this
.
started
A
t
=
started
A
t
;
}
public
Boolean
getTag
()
{
...
...
@@ -129,12 +129,12 @@ public class Job {
this
.
coverage
=
coverage
;
}
public
ArtifactsFile
getArtifacts
_f
ile
()
{
return
artifacts
_f
ile
;
public
ArtifactsFile
getArtifacts
F
ile
()
{
return
artifacts
F
ile
;
}
public
void
setArtifacts
_f
ile
(
ArtifactsFile
artifacts
_f
ile
)
{
this
.
artifacts
_f
ile
=
artifacts
_f
ile
;
public
void
setArtifacts
F
ile
(
ArtifactsFile
artifacts
F
ile
)
{
this
.
artifacts
F
ile
=
artifacts
F
ile
;
}
public
Runner
getRunner
()
{
...
...
src/main/java/org/gitlab4j/api/webhook/PipelineEvent.java
View file @
a635acf6
...
...
@@ -6,6 +6,7 @@ import java.util.List;
import
javax.xml.bind.annotation.XmlAccessType
;
import
javax.xml.bind.annotation.XmlAccessorType
;
import
org.gitlab4j.api.models.ArtifactsFile
;
import
org.gitlab4j.api.models.User
;
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
...
...
@@ -171,29 +172,6 @@ public class PipelineEvent implements Event {
public
void
setArtifactsFile
(
ArtifactsFile
artifactsFile
)
{
this
.
artifactsFile
=
artifactsFile
;
}
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
public
static
class
ArtifactsFile
{
private
String
file
;
private
Long
size
;
public
String
getFile
()
{
return
file
;
}
public
void
setFile
(
String
file
)
{
this
.
file
=
file
;
}
public
Long
getSize
()
{
return
size
;
}
public
void
setSize
(
Long
size
)
{
this
.
size
=
size
;
}
}
}
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
...
...
src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
View file @
a635acf6
...
...
@@ -7,6 +7,7 @@ import java.io.InputStreamReader;
import
java.util.List
;
import
org.gitlab4j.api.GitLabApi
;
import
org.gitlab4j.api.models.ArtifactsFile
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.Commit
;
import
org.gitlab4j.api.models.CompareResults
;
...
...
@@ -14,6 +15,7 @@ import org.gitlab4j.api.models.Diff;
import
org.gitlab4j.api.models.Event
;
import
org.gitlab4j.api.models.Group
;
import
org.gitlab4j.api.models.Issue
;
import
org.gitlab4j.api.models.Job
;
import
org.gitlab4j.api.models.Key
;
import
org.gitlab4j.api.models.Member
;
import
org.gitlab4j.api.models.MergeRequest
;
...
...
@@ -146,6 +148,28 @@ public class TestGitLabApiBeans {
}
}
@Test
public
void
testJob
()
{
try
{
Job
job
=
makeFakeApiCall
(
Job
.
class
,
"job"
);
assertTrue
(
compareJson
(
job
,
"job"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Test
public
void
testArtifactsFile
()
{
try
{
ArtifactsFile
artifactFile
=
makeFakeApiCall
(
ArtifactsFile
.
class
,
"artifacts-file"
);
assertTrue
(
compareJson
(
artifactFile
,
"artifacts-file"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Test
public
void
testProjectHook
()
{
...
...
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