Search in sources :

Example 1 with Artifact

use of com.offbytwo.jenkins.model.Artifact in project gogs-webhook-plugin by jenkinsci.

the class GogsWebHook_IT method loadMarkerArtifactAsProperty.

/**
 * Loads the marker file of the last build (archived during the build)
 *
 * @param jenkins the jenkins instance we want to load from
 * @return the marker file loaded as a property file (so that it can be easily queried)
 * @throws IOException        Something unexpected went wrong when querying the Jenkins server
 * @throws URISyntaxException Something uunexpected went wrong loading the marker as a property
 */
private Properties loadMarkerArtifactAsProperty(JenkinsServer jenkins) throws IOException, URISyntaxException {
    JobWithDetails detailedJob = jenkins.getJob(JENKINS_JOB_NAME);
    BuildWithDetails lastBuild = detailedJob.getLastBuild().details();
    int buildNbr = lastBuild.getNumber();
    boolean isBuilding = lastBuild.isBuilding();
    log.info("BuildNbr we are examining: " + buildNbr);
    List<Artifact> artifactList = lastBuild.getArtifacts();
    assertEquals("Not the expected number of artifacts", 1, artifactList.size());
    Artifact markerArtifact = artifactList.get(0);
    String markerArtifactFileName = markerArtifact.getFileName();
    assertEquals("The artifact is not the expected one", "marker.txt", markerArtifactFileName);
    InputStream markerArtifactInputStream = lastBuild.details().downloadArtifact(markerArtifact);
    String markerAsText = IOUtils.toString(markerArtifactInputStream, Charset.defaultCharset());
    log.info("\n" + markerAsText);
    StringReader reader = new StringReader(markerAsText);
    Properties markerAsProperty = new Properties();
    markerAsProperty.load(reader);
    // check if the marker matches the build number we expect.
    String buildNbrFromMarker = markerAsProperty.getProperty("BUILD_NUMBER");
    String buildNbrFromQery = String.valueOf(buildNbr);
    assertEquals("The build number from the marker does not match the last build number", buildNbrFromMarker, buildNbrFromQery);
    return markerAsProperty;
}
Also used : BuildWithDetails(com.offbytwo.jenkins.model.BuildWithDetails) JobWithDetails(com.offbytwo.jenkins.model.JobWithDetails) Properties(java.util.Properties) Artifact(com.offbytwo.jenkins.model.Artifact)

Aggregations

Artifact (com.offbytwo.jenkins.model.Artifact)1 BuildWithDetails (com.offbytwo.jenkins.model.BuildWithDetails)1 JobWithDetails (com.offbytwo.jenkins.model.JobWithDetails)1 Properties (java.util.Properties)1