Skip to content
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

Stop using deprecated exception type #296

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hudson.plugins.promoted_builds;

import antlr.ANTLRException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.BulkChange;
import hudson.EnvVars;
Expand Down Expand Up @@ -249,409 +248,409 @@
try {
LabelExpression.parseExpression(assignedLabel);
return assignedLabel;
} catch (ANTLRException e) {
} catch (IllegalArgumentException e) {
// must be old label or host name that includes whitespace or other unsafe chars
return LabelAtom.escape(assignedLabel);
}
}

@Override public Label getAssignedLabel() {
// Really would like to run on the exact node that the promoted build ran on,
// not just the same label.. but at least this works if job is tied to one node:
if (assignedLabel == null) return getOwner().getAssignedLabel();

return Jenkins.get().getLabel(assignedLabel);
}

@Override public JDK getJDK() {
return getOwner().getJDK();
}

/**
* Gets the customWorkspace of the owner project.
*
* Support for FreeStyleProject only.
* @return customWorkspace
*/
@CheckForNull
public String getCustomWorkspace() {
AbstractProject<?, ?> p = getOwner();
if (p instanceof FreeStyleProject)
return ((FreeStyleProject) p).getCustomWorkspace();
return null;
}

/**
* Get the icon name, without the extension. It will always return a non null
* and non empty string, as <code>"star-gold"</code> is used for compatibility
* for older promotions configurations.
*
* @return the icon name
*/
public String getIcon() {
return getIcon(icon);
}

public String getIsVisible(){
return isVisible;
}

public boolean isVisible(){
if (isVisible == null) return true;

AbstractProject<?, ?> job = getOwner();

if (job == null) return true;

String expandedIsVisible = isVisible;
EnvVars environment = getDefaultParameterValuesAsEnvVars(job);
if (environment != null){
expandedIsVisible = environment.expand(expandedIsVisible);
}

if (expandedIsVisible == null){
return true;
}
return !expandedIsVisible.toLowerCase().equals("false");
}
private static EnvVars getDefaultParameterValuesAsEnvVars(AbstractProject owner) {
EnvVars envVars = null;
ParametersDefinitionProperty parametersDefinitionProperty = (ParametersDefinitionProperty)owner.getProperty(ParametersDefinitionProperty.class);
if (parametersDefinitionProperty!=null){
envVars = new EnvVars();
for (ParameterDefinition parameterDefinition: parametersDefinitionProperty.getParameterDefinitions()){
ParameterValue defaultParameterValue = parameterDefinition.getDefaultParameterValue();
if (defaultParameterValue!=null){
if (defaultParameterValue instanceof StringParameterValue){
envVars.put(parameterDefinition.getName(), (String)defaultParameterValue.getValue());
}
}
}
EnvVars.resolve(envVars);
}

return envVars;
}
/**
* Handle compatibility with pre-1.8 configs.
*
* @param sIcon
* the name of the icon used by this promotion; if null or empty,
* we return the gold icon for compatibility with previous releases
* @return the icon file name for this promotion
*/
@NonNull
private static String getIcon(@CheckForNull String sIcon) {
if ((sIcon == null) || sIcon.equals(""))
return "star-gold";
else
return sIcon;
}

/**
* Get the badges of conditions that were passed for this promotion for the build
* @param build The build to be checked
* @return List of generated promotion badges
*/
@NonNull
public List<PromotionBadge> getMetQualifications(AbstractBuild<?,?> build) {
List<PromotionBadge> badges = new ArrayList<PromotionBadge>();
for (PromotionCondition cond : conditions) {
PromotionBadge b = cond.isMet(this, build);

if (b != null)
badges.add(b);
}
return badges;
}

/**
* Get the conditions that have not been met for this promotion for the build
* @param build Build to be checked
* @return List of unmet promotion conditions
*/
@NonNull
public List<PromotionCondition> getUnmetConditions(AbstractBuild<?,?> build) {
List<PromotionCondition> unmetConditions = new ArrayList<PromotionCondition>();

for (PromotionCondition cond : conditions) {
if (cond.isMet(this, build) == null)
unmetConditions.add(cond);
}

return unmetConditions;
}

/**
* Checks if all the conditions to promote a build is met.
*
* @param build Build to be checked
* @return
* {@code null} if promotion conditions are not met.
* otherwise returns a list of badges that record how the promotion happened.
*/
@CheckForNull
public Status isMet(AbstractBuild<?,?> build) {
List<PromotionBadge> badges = new ArrayList<PromotionBadge>();
for (PromotionCondition cond : conditions) {
PromotionBadge b = cond.isMet(this, build);
if(b==null)
return null;
badges.add(b);
}
return new Status(this,badges);
}

/**
* @deprecated
* Use {@link #considerPromotion2(AbstractBuild)}
*/
@Deprecated
public boolean considerPromotion(AbstractBuild<?,?> build) throws IOException {
return considerPromotion2(build)!=null;
}

/**
* Checks if the build is promotable, and if so, promote it.
*
* @param build Build to be promoted
* @return
* {@code null} if the build was not promoted, otherwise Future that kicks in when the build is completed.
* @throws IOException
*/
@CheckForNull
public Future<Promotion> considerPromotion2(AbstractBuild<?, ?> build) throws IOException {
LOGGER.fine("Considering the promotion of "+build+" via "+getName()+" without parmeters");
// If the build has manual approvals, use the parameters from it
List<ParameterValue> params = new ArrayList<ParameterValue>();
List<ManualApproval> approvals = build.getActions(ManualApproval.class);
for (ManualApproval approval : approvals) {
if (approval.name.equals(getName())) {
LOGGER.fine("Getting parameters from existing manual promotion");
params = approval.badge.getParameterValues();
LOGGER.finer("Using paramters: "+params.toString());
}
}

return considerPromotion2(build, params);
}

@CheckForNull
public Future<Promotion> considerPromotion2(AbstractBuild<?,?> build, List<ParameterValue> params) throws IOException {
if (!isActive())
return null; // not active

PromotedBuildAction a = build.getAction(PromotedBuildAction.class);

// if it's already promoted, no need to do anything.
if(a!=null && a.contains(this))
return null;

LOGGER.fine("Considering the promotion of "+build+" via "+getName()+" with parameters");
Status qualification = isMet(build);
if(qualification==null)
return null; // not this time

LOGGER.fine("Promotion condition of "+build+" is met: "+qualification);
Future<Promotion> f = promote2(build, new UserCause(), qualification, params); // TODO: define promotion cause
if (f==null)
LOGGER.warning(build+" qualifies for a promotion but the queueing failed.");
return f;
}

public void promote(AbstractBuild<?,?> build, Cause cause, PromotionBadge... badges) throws IOException {
promote2(build,cause,new Status(this,Arrays.asList(badges)));
}

/**
* @deprecated
* Use {@link #promote2(AbstractBuild, Cause, Status)}
*/
@Deprecated
public void promote(AbstractBuild<?,?> build, Cause cause, Status qualification) throws IOException {
promote2(build,cause,qualification);
}

/**
* Promote the given build by using the given qualification.
*
* @param build Build to promote
* @param cause Why the build is promoted?
* @param qualification Initial promotion status
* @return Future to track the completion of the promotion.
* @throws IOException Promotion failure
*/
public Future<Promotion> promote2(AbstractBuild<?,?> build, Cause cause, Status qualification) throws IOException {
return promote2(build, cause, qualification, null);
}

/**
* Promote the given build by using the given qualification.
*
* @param build Build to promote
* @param cause Why the build is promoted?
* @param qualification Initial promotion status
* @param params Promotion parameters
* @return Future to track the completion of the promotion.
* @throws IOException Promotion failure
*/
public Future<Promotion> promote2(AbstractBuild<?,?> build, Cause cause, Status qualification, List<ParameterValue> params) throws IOException {
PromotedBuildAction a = build.getAction(PromotedBuildAction.class);
// build is qualified for a promotion.
if(a!=null) {
a.add(qualification);
} else {
build.addAction(new PromotedBuildAction(build,qualification));
build.save();
}

// schedule promotion activity.
return scheduleBuild2(build,cause, params);
}

/**
* @deprecated
* You need to be using {@link #scheduleBuild(AbstractBuild)}
*/
@Deprecated
public boolean scheduleBuild() {
return super.scheduleBuild();
}

public boolean scheduleBuild(@NonNull AbstractBuild<?,?> build) {
return scheduleBuild(build,new UserCause());
}

/**
* @param build Target build
* @param cause Promotion cause
* @return {@code true} if scheduling is successful
* @deprecated
* Use {@link #scheduleBuild2(AbstractBuild, Cause)}
*/
@Deprecated
public boolean scheduleBuild(@NonNull AbstractBuild<?,?> build, @NonNull Cause cause) {
return scheduleBuild2(build,cause)!=null;
}

/**
* Schedules the promotion.
* @param build Target build
* @param cause Promotion cause
* @param params Parameters to be passed
* @return Future result or {@code null} if the promotion cannot be scheduled
*/
@CheckForNull
public Future<Promotion> scheduleBuild2(@NonNull AbstractBuild<?,?> build,
Cause cause, @CheckForNull List<ParameterValue> params) {
List<Action> actions = new ArrayList<Action>();
actions.add(Promotion.PromotionParametersAction.buildFor(build, params));
actions.add(new PromotionTargetAction(build));

// remember what build we are promoting
return super.scheduleBuild2(0, cause, actions.toArray(new Action[actions.size()]));
}


@Override
public void doBuild(StaplerRequest req, StaplerResponse rsp, @QueryParameter TimeDuration delay) throws IOException, ServletException {
throw HttpResponses.error(404, "Promotion processes may not be built directly");
}

public Future<Promotion> scheduleBuild2(@NonNull AbstractBuild<?,?> build, @NonNull Cause cause) {
return scheduleBuild2(build, cause, null);
}

public boolean isInQueue(@NonNull AbstractBuild<?,?> build) {
for (Item item : Jenkins.get().getQueue().getItems(this))
if (item.getAction(PromotionTargetAction.class).resolve(this)==build)
return true;
return false;
}

//
// these are dummy implementations to implement abstract methods.
// need to think about what the implications are.
//
@Override
public boolean isFingerprintConfigured() {
return false;
}

@Override
protected void buildDependencyGraph(DependencyGraph graph) {
throw new UnsupportedOperationException();
}

public static List<Descriptor<? extends BuildStep>> getAll() {
List<Descriptor<? extends BuildStep>> list = new ArrayList<Descriptor<? extends BuildStep>>();
addTo(Builder.all(), list);
addTo(Publisher.all(), list);
return list;
}

private static void addTo(List<? extends Descriptor<? extends BuildStep>> source, List<Descriptor<? extends BuildStep>> list) {
for (Descriptor<? extends BuildStep> d : source) {
if (d instanceof BuildStepDescriptor) {
BuildStepDescriptor bsd = (BuildStepDescriptor) d;
if(bsd.isApplicable(PromotionProcess.class))
list.add(d);
}
}
}

public Permalink asPermalink() {
return new Permalink() {
@Override
public String getDisplayName() {
return Messages.PromotionProcess_PermalinkDisplayName(PromotionProcess.this.getDisplayName());
}

@Override
public String getId() {
return PromotionProcess.this.getName();
}

@Override
public Run<?, ?> resolve(Job<?, ?> job) {
String id = getId();
for( Run<?,?> build : job.getBuilds() ) {
PromotedBuildAction a = build.getAction(PromotedBuildAction.class);
if(a!=null && a.contains(id))
return build;
}
return null;
}
};
}

public DescriptorImpl getDescriptor() {
return (DescriptorImpl)Jenkins.get().getDescriptorOrDie(getClass());
}

@Override
public String getShortUrl() {
// Must be overridden since JobPropertyImpl.getUrlChildPrefix is "" not "process" as you might expect (also see e50f0f5 in 1.519)
return "process/" + Util.rawEncode(getName()) + '/';
}

public boolean isActive() {
return !isDisabled();
}

@Extension
public static class DescriptorImpl extends Descriptor<PromotionProcess> {
@Override
public String getDisplayName() {
return "Promotion Process";
}

public FormValidation doCheckLabelString(@QueryParameter String value) {
if (Util.fixEmpty(value)==null)
return FormValidation.ok(); // nothing typed yet
try {
Label.parseExpression(value);
} catch (ANTLRException e) {
} catch (IllegalArgumentException e) {

Check warning on line 653 in src/main/java/hudson/plugins/promoted_builds/PromotionProcess.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 251-653 are not covered by tests
return FormValidation.error(e,
Messages.JobPropertyImpl_LabelString_InvalidBooleanExpression(e.getMessage()));
}
Expand Down