Skip to content

Commit 75cbedf

Browse files
committed
Add JobApi#getJob(String)
1 parent fe14e34 commit 75cbedf

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

gitlab4j-api/src/main/java/org/gitlab4j/api/JobApi.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -292,18 +292,38 @@ public Stream<Job> getJobsStream(Object projectIdOrPath, long pipelineId, Boolea
292292
}
293293

294294
/**
295-
* Retrieve the job corresponding to the <code>$CI_JOB_TOKEN</code> environment variable (Using a {@link org.gitlab4j.models.Constants.TokenType#JOB_TOKEN} authentication).
295+
* Retrieve the job corresponding to the <code>$CI_JOB_TOKEN</code> environment variable (Using a {@link TokenType#JOB_TOKEN} authentication).
296296
*
297297
* <pre><code>GitLab Endpoint: GET /job</code></pre>
298298
*
299-
* @return a single job
299+
* @return a single job corresponding to the token used for the authentication
300300
* @throws GitLabApiException if any exception occurs during execution
301301
*/
302302
public Job getJob() throws GitLabApiException {
303+
TokenType tokenType = getApiClient().getTokenType();
304+
if (tokenType != TokenType.JOB_TOKEN) {
305+
throw new IllegalStateException(
306+
"This method can only be called with a " + TokenType.JOB_TOKEN + " authentication");
307+
}
303308
Response response = get(Response.Status.OK, null, "job");
304309
return (response.readEntity(Job.class));
305310
}
306311

312+
/**
313+
* Retrieve the job corresponding to the <code>$CI_JOB_TOKEN</code> environment variable.
314+
*
315+
* <pre><code>GitLab Endpoint: GET /job?job_token=${ciJobToken}"</code></pre>
316+
*
317+
* @return a single job corresponding to the passed token
318+
* @throws GitLabApiException if any exception occurs during execution
319+
*/
320+
public Job getJob(final String ciJobToken) throws GitLabApiException {
321+
GitLabApiForm formData = new GitLabApiForm().withParam("job_token", ciJobToken, true);
322+
323+
Response response = get(Response.Status.OK, formData.asMap(), "job");
324+
return (response.readEntity(Job.class));
325+
}
326+
307327
/**
308328
* Get single job in a project.
309329
*

0 commit comments

Comments
 (0)