Skip to content
Permalink
Browse files

[FIXED JENKINS-24626] Added a new build selector "Downstream of"

  • Loading branch information...
ikedam committed Sep 18, 2014
1 parent a7ce03c commit 1515de100de0d52575f9c13cdccbf9c20a5813c4
@@ -0,0 +1,134 @@
/*
* The MIT License
*
* Copyright (c) 2014 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.plugins.copyartifact;

import java.util.logging.Logger;

import jenkins.model.Jenkins;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Run;

/**
* Select a build which is a downstream of a specified build.
*/
public class DownstreamBuildSelector extends BuildSelector {
private static final Logger LOGGER = Logger.getLogger(DownstreamBuildSelector.class.getName());
private final String upstreamProjectName;
private final String upstreamBuildNumber;

/**
* @param upstreamProjectName
* @param upstreamBuildNumber
*/
@DataBoundConstructor
public DownstreamBuildSelector(String upstreamProjectName, String upstreamBuildNumber) {
this.upstreamProjectName = upstreamProjectName;
this.upstreamBuildNumber = upstreamBuildNumber;
}

/**
* @return upstream project name. May include variable expression.
*/
public String getUpstreamProjectName() {
return upstreamProjectName;
}

/**
* @return upstream build number. May include variable expression.
*/
public String getUpstreamBuildNumber() {
return upstreamBuildNumber;
}

@Override
protected boolean isSelectable(Run<?, ?> run, EnvVars env) {
if (!(run instanceof AbstractBuild<?,?>)) {
LOGGER.warning(String.format("Only applicable to AbstractBuild: but is %s.", run.getClass().getName()));
return false;
}

String projectName = env.expand(getUpstreamProjectName());
String buildNumber = env.expand(getUpstreamBuildNumber());

if (StringUtils.isBlank(projectName)) {
LOGGER.warning("Upstream project name gets empty.");
return false;
}

if (StringUtils.isBlank(buildNumber)) {
LOGGER.warning("Upstream build number gets empty.");
return false;
}

AbstractProject<?,?> upstreamProject = Jenkins.getInstance().getItem(
projectName,
run.getParent(),
AbstractProject.class
);
if (upstreamProject == null) {
LOGGER.warning(String.format("Upstream project '%s' is not found.", upstreamProject));
return false;
}
AbstractBuild<?,?> upstreamBuild = ((AbstractBuild<?,?>)run).getUpstreamRelationshipBuild(upstreamProject);
if (upstreamBuild == null) {
LOGGER.fine(String.format("No upstream build of project '%s' is found for build %s-%s.", upstreamProject, run.getParent().getFullName(), run.getDisplayName()));
return false;
}

try {
int number = Integer.parseInt(buildNumber);
if (number == upstreamBuild.getNumber()) {
// build number matches.
return true;
}
} catch (NumberFormatException e) {
// Ignore. Nothing to do.
}

if (buildNumber.equals(upstreamBuild.getDisplayName())) {
// build name matches.
return true;
}

LOGGER.fine(String.format("build %s-%s doesn't match %s.", run.getParent().getFullName(), run.getDisplayName(), buildNumber));
return false;
}

@Extension
public static final class DescriptorImpl extends Descriptor<BuildSelector> {
@Override
public String getDisplayName() {
return Messages.DownstreamBuildSelector_DisplayName();
}
}
}
@@ -0,0 +1,32 @@
<!--
The MIT License
Copyright (c) 2014 IKEDA Yasuyuki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%Upstream Project Name}" field="upstreamProjectName">
<f:textbox />
</f:entry>
<f:entry title="${%Upstream Project Number}" field="upstreamBuildNumber">
<f:textbox />
</f:entry>
</j:jelly>
@@ -24,3 +24,4 @@ BuildSelectorParameter.DisplayName=Build selector for Copy Artifact
WorkspaceSelector.DisplayName=Copy from WORKSPACE of latest completed build
CopyArtifactPermissionProperty.DisplayName=Permission to Copy Artifact
CopyArtifactPermissionProperty.MissingProject=Unable to find project: {0}
DownstreamBuildSelector.DisplayName=Downstream build of
@@ -18,3 +18,4 @@ BuildSelectorParameter.DisplayName=Copy Artifact\u7528\u30d3\u30eb\u30c9\u30bb\u
WorkspaceSelector.DisplayName=\u6700\u65b0\u306e\u5b8c\u4e86\u3057\u305f\u30d3\u30eb\u30c9\u306e\u30ef\u30fc\u30af\u30b9\u30da\u30fc\u30b9\u304b\u3089\u30b3\u30d4\u30fc
CopyArtifactPermissionProperty.DisplayName=\u6210\u679c\u7269\u306e\u30b3\u30d4\u30fc\u306e\u8a31\u53ef
CopyArtifactPermissionProperty.MissingProject=\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093: {0}
DownstreamBuildSelector.DisplayName=\u6307\u5b9a\u30d3\u30eb\u30c9\u306e\u4e0b\u6d41\u30d3\u30eb\u30c9

0 comments on commit 1515de1

Please sign in to comment.
You can’t perform that action at this time.