Skip to content

Commit 1047d7b

Browse files
committed
Added downloadArtifactsFile() methods that take an ArtifactsFile instance.
1 parent 689fda1 commit 1047d7b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.ws.rs.core.Response;
1616
import javax.ws.rs.core.Response.Status;
1717

18+
import org.gitlab4j.api.models.ArtifactsFile;
1819
import org.gitlab4j.api.models.Job;
1920

2021
/**
@@ -261,6 +262,60 @@ public InputStream downloadArtifactsFile(Object projectIdOrPath, Integer jobId)
261262
return (response.readEntity(InputStream.class));
262263
}
263264

265+
/**
266+
* Download a single artifact file from within the job's artifacts archive.
267+
*
268+
* Only a single file is going to be extracted from the archive and streamed to a client.
269+
*
270+
* GET /projects/:id/jobs/:job_id/artifacts/*artifact_path
271+
*
272+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
273+
* @param jobId the unique job identifier
274+
* @param artifactsFile an ArtifactsFile instance for the artifact to download
275+
* @param directory the File instance of the directory to save the file to, if null will use "java.io.tmpdir"
276+
* @return a File instance pointing to the download of the specified artifacts file
277+
* @throws GitLabApiException if any exception occurs
278+
*/
279+
public File downloadArtifactsFile(Object projectIdOrPath, Integer jobId, ArtifactsFile artifactsFile, File directory) throws GitLabApiException {
280+
281+
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
282+
"projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "artifacts", artifactsFile.getFilename());
283+
try {
284+
285+
if (directory == null)
286+
directory = new File(System.getProperty("java.io.tmpdir"));
287+
288+
String filename = artifactsFile.getFilename();
289+
File file = new File(directory, filename);
290+
291+
InputStream in = response.readEntity(InputStream.class);
292+
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
293+
return (file);
294+
295+
} catch (IOException ioe) {
296+
throw new GitLabApiException(ioe);
297+
}
298+
}
299+
300+
/**
301+
* Download a single artifact file from within the job's artifacts archive.
302+
*
303+
* Only a single file is going to be extracted from the archive and streamed to a client.
304+
*
305+
* GET /projects/:id/jobs/:job_id/artifacts/*artifact_path
306+
*
307+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
308+
* @param jobId the unique job identifier
309+
* @param artifactsFile an ArtifactsFile instance for the artifact to download
310+
* @return an InputStream to read the specified artifacts file from
311+
* @throws GitLabApiException if any exception occurs
312+
*/
313+
public InputStream downloadArtifactsFile(Object projectIdOrPath, Integer jobId, ArtifactsFile artifactsFile) throws GitLabApiException {
314+
Response response = get(Response.Status.OK, getDefaultPerPageParam(),
315+
"projects", getProjectIdOrPath(projectIdOrPath), "jobs", jobId, "artifacts", artifactsFile.getFilename());
316+
return (response.readEntity(InputStream.class));
317+
}
318+
264319
/**
265320
* Download a single artifact file from within the job's artifacts archive.
266321
*

0 commit comments

Comments
 (0)