Search in sources :

Example 1 with JobProperty

use of hudson.model.JobProperty in project branch-api-plugin by jenkinsci.

the class BranchProjectFactory method decorate.

/**
 * Decorates the project in with all the {@link JobDecorator} instances.
 * NOTE: This method should suppress saving the project and only affect the in-memory state.
 * NOTE: Override if the default strategy is not appropriate for the specific project type.
 *
 * @param project the project.
 * @return the project for nicer method chaining
 */
@SuppressWarnings({ "ConstantConditions", "unchecked" })
public P decorate(P project) {
    if (!isProject(project)) {
        return project;
    }
    Branch branch = getBranch(project);
    // HACK ALERT
    // ==========
    // We don't want to trigger a save, so we will do some trickery to inject the new values
    // it would be better if Core gave us some hooks to do this
    BulkChange bc = new BulkChange(project);
    try {
        List<BranchProperty> properties = new ArrayList<>(branch.getProperties());
        Collections.sort(properties, DescriptorOrder.reverse(BranchProperty.class));
        for (BranchProperty property : properties) {
            JobDecorator<P, R> decorator = property.jobDecorator((Class) project.getClass());
            if (decorator != null) {
                // if Project then we can feed the publishers and build wrappers
                if (project instanceof Project && decorator instanceof ProjectDecorator) {
                    DescribableList<Publisher, Descriptor<Publisher>> publishersList = ((Project) project).getPublishersList();
                    DescribableList<BuildWrapper, Descriptor<BuildWrapper>> buildWrappersList = ((Project) project).getBuildWrappersList();
                    List<Publisher> publishers = ((ProjectDecorator) decorator).publishers(publishersList.toList());
                    List<BuildWrapper> buildWrappers = ((ProjectDecorator) decorator).buildWrappers(buildWrappersList.toList());
                    publishersList.replaceBy(publishers);
                    buildWrappersList.replaceBy(buildWrappers);
                }
                // we can always feed the job properties... but just not as easily as we'd like
                List<JobProperty<? super P>> jobProperties = decorator.jobProperties(project.getAllProperties());
                // both removal and addition
                for (JobProperty<? super P> p : project.getAllProperties()) {
                    project.removeProperty(p);
                }
                for (JobProperty<? super P> p : jobProperties) {
                    project.addProperty(p);
                }
                // now apply the final layer
                decorator.project(project);
            }
        }
    } catch (IOException e) {
    // should be safe to ignore as the BulkChange suppresses the save operation.
    } finally {
        bc.abort();
    }
    return project;
}
Also used : ArrayList(java.util.ArrayList) BulkChange(hudson.BulkChange) Publisher(hudson.tasks.Publisher) IOException(java.io.IOException) Project(hudson.model.Project) Descriptor(hudson.model.Descriptor) JobProperty(hudson.model.JobProperty) BuildWrapper(hudson.tasks.BuildWrapper)

Aggregations

BulkChange (hudson.BulkChange)1 Descriptor (hudson.model.Descriptor)1 JobProperty (hudson.model.JobProperty)1 Project (hudson.model.Project)1 BuildWrapper (hudson.tasks.BuildWrapper)1 Publisher (hudson.tasks.Publisher)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1