-
Notifications
You must be signed in to change notification settings - Fork 9
AGDIGGER-60 - fetching and saving artifacts api #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.redhat.digkins; | ||
|
|
||
| import com.redhat.digkins.services.JobService; | ||
| import com.redhat.digkins.services.TriggerBuildService; | ||
| import com.redhat.digkins.util.DiggerClientException; | ||
| import org.apache.commons.io.IOUtils; | ||
|
|
||
| import java.io.*; | ||
|
|
||
| /** | ||
| */ | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) throws DiggerClientException, IOException { | ||
| DiggerClient client = DiggerClient.builder() | ||
| .createJobService(new JobService()) | ||
| .triggerBuildService(new TriggerBuildService(10000, 100)) | ||
| .withAuth("https://jenkins-digger2.osm3.feedhenry.net", "admin", "Vu8ysYH5f2dJiLgL") | ||
| .build(); | ||
| //client.createJob("wtr-java-tests2", "https://github.com/wtrocki/helloworld-android-gradle", "master"); | ||
| //client.build("wtr-java-tests2"); | ||
| InputStream inputStream = client.fetchArtifact("wtr-java-tests2", 1, "app-debug.apk"); | ||
| if(inputStream != null){ | ||
| File targetFile = new File("artifact.tmp"); | ||
| OutputStream outStream = new FileOutputStream(targetFile); | ||
|
|
||
| byte[] buffer = new byte[8 * 1024]; | ||
| int bytesRead; | ||
| while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
| outStream.write(buffer, 0, bytesRead); | ||
| } | ||
| IOUtils.closeQuietly(inputStream); | ||
| IOUtils.closeQuietly(outStream); | ||
| } | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/redhat/digkins/services/ArtifactsService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package com.redhat.digkins.services; | ||
|
|
||
| import com.offbytwo.jenkins.JenkinsServer; | ||
| import com.offbytwo.jenkins.model.Artifact; | ||
| import com.offbytwo.jenkins.model.Build; | ||
| import com.offbytwo.jenkins.model.BuildWithDetails; | ||
| import com.offbytwo.jenkins.model.JobWithDetails; | ||
| import com.redhat.digkins.util.DiggerClientException; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.*; | ||
| import java.net.URISyntaxException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Service used to retrieve artifacts | ||
| */ | ||
| public class ArtifactsService { | ||
|
|
||
| /** | ||
| */ | ||
| public ArtifactsService() { | ||
| } | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ArtifactsService.class); | ||
| private static int DEFAULT_BUFFER = 8 * 1024; | ||
|
|
||
| /** | ||
| * Save artifact for specified location for specific job, build number and artifact name. | ||
| * If name would be an regular expression method would return stream for the first match. | ||
| * | ||
| * @param jobName name of the job | ||
| * @param buildNumber job build number | ||
| * @param artifactName name of the artifact to fetch - can be regexp for example *.apk | ||
| * @param outputFile file (location) used to save artifact | ||
| * @throws DiggerClientException when problem with fetching artifacts from jenkins | ||
| * @throws IOException when one of the files cannot be saved | ||
| */ | ||
| public void saveArtifact(JenkinsServer jenkins, String jobName, int buildNumber, String artifactName, File outputFile) throws DiggerClientException, IOException { | ||
| InputStream inputStream = streamArtifact(jenkins, jobName, buildNumber, artifactName); | ||
| if (inputStream != null) { | ||
| OutputStream outStream = new FileOutputStream(outputFile); | ||
| byte[] buffer = new byte[DEFAULT_BUFFER]; | ||
| int bytesRead; | ||
| while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
| outStream.write(buffer, 0, bytesRead); | ||
| } | ||
| IOUtils.closeQuietly(inputStream); | ||
| IOUtils.closeQuietly(outStream); | ||
| } else { | ||
| throw new DiggerClientException("Cannot fetch artifacts from jenkins"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get artifact inputstream for specific job, build number and artifact name. | ||
| * If name would be an regular expression method would return stream for the first match. | ||
| * | ||
| * @param jobName name of the job | ||
| * @param buildNumber job build number | ||
| * @param artifactName name of the artifact to fetch - can be regexp for example *.apk | ||
| * @return InputStream with file contents that can be saved or piped to socket | ||
| * @throws DiggerClientException when problem with fetching artifacts from jenkins | ||
| */ | ||
| public InputStream streamArtifact(JenkinsServer jenkins, String jobName, int buildNumber, String artifactName) throws DiggerClientException { | ||
| try { | ||
| JobWithDetails job = jenkins.getJob(jobName); | ||
| if (job == null) { | ||
| LOG.error("Cannot fetch job from jenkins {0}", jobName); | ||
| throw new DiggerClientException("Cannot fetch job from jenkins"); | ||
| } | ||
| Build build = job.getBuildByNumber(buildNumber); | ||
| BuildWithDetails buildWithDetails = build.details(); | ||
| List<Artifact> artifacts = buildWithDetails.getArtifacts(); | ||
| for (Artifact artifact : artifacts) { | ||
| if (artifact.getFileName().matches(artifactName)) { | ||
| LOG.debug("Streaming artifact {0}", artifactName); | ||
| return buildWithDetails.downloadArtifact(artifact); | ||
| } | ||
| } | ||
| } catch (URISyntaxException e) { | ||
| LOG.error("Invalid job name {0}", jobName, e); | ||
| throw new DiggerClientException(e); | ||
| } catch (IOException e) { | ||
| LOG.error("Problem when fetching artifacts for {0} {1} {2}", jobName, buildNumber, artifactName, e); | ||
| throw new DiggerClientException(e); | ||
| } | ||
| LOG.debug("Cannot find build for ", jobName, buildNumber, artifactName); | ||
| return null; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍