From 75cbedfdff9b21231c1284448e99b04a0ed5ffa3 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Mon, 10 Mar 2025 09:49:08 +0100 Subject: [PATCH] Add JobApi#getJob(String) --- .../main/java/org/gitlab4j/api/JobApi.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/gitlab4j-api/src/main/java/org/gitlab4j/api/JobApi.java b/gitlab4j-api/src/main/java/org/gitlab4j/api/JobApi.java index 5b4e30ba..92222efb 100644 --- a/gitlab4j-api/src/main/java/org/gitlab4j/api/JobApi.java +++ b/gitlab4j-api/src/main/java/org/gitlab4j/api/JobApi.java @@ -292,18 +292,38 @@ public Stream getJobsStream(Object projectIdOrPath, long pipelineId, Boolea } /** - * Retrieve the job corresponding to the $CI_JOB_TOKEN environment variable (Using a {@link org.gitlab4j.models.Constants.TokenType#JOB_TOKEN} authentication). + * Retrieve the job corresponding to the $CI_JOB_TOKEN environment variable (Using a {@link TokenType#JOB_TOKEN} authentication). * *
GitLab Endpoint: GET /job
* - * @return a single job + * @return a single job corresponding to the token used for the authentication * @throws GitLabApiException if any exception occurs during execution */ public Job getJob() throws GitLabApiException { + TokenType tokenType = getApiClient().getTokenType(); + if (tokenType != TokenType.JOB_TOKEN) { + throw new IllegalStateException( + "This method can only be called with a " + TokenType.JOB_TOKEN + " authentication"); + } Response response = get(Response.Status.OK, null, "job"); return (response.readEntity(Job.class)); } + /** + * Retrieve the job corresponding to the $CI_JOB_TOKEN environment variable. + * + *
GitLab Endpoint: GET /job?job_token=${ciJobToken}"
+ * + * @return a single job corresponding to the passed token + * @throws GitLabApiException if any exception occurs during execution + */ + public Job getJob(final String ciJobToken) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm().withParam("job_token", ciJobToken, true); + + Response response = get(Response.Status.OK, formData.asMap(), "job"); + return (response.readEntity(Job.class)); + } + /** * Get single job in a project. *