forked from vaimr/pipeline-multibranch-defaults-plugin
/
DefaultsBinder.java
90 lines (78 loc) · 3.63 KB
/
DefaultsBinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.jenkinsci.plugins.workflow.multibranch;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.*;
import jenkins.model.Jenkins;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.groovy.GroovyScript;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinitionDescriptor;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import java.io.File;
import java.util.List;
/**
* Checks out the local default version of {@link WorkflowBranchProjectFactory#SCRIPT} in order if exist:
* 1. From module checkout
* 1. From task workspace directory
* 2. From global jenkins managed files
*/
class DefaultsBinder extends FlowDefinition {
@Override
public FlowExecution create(FlowExecutionOwner handle, TaskListener listener, List<? extends Action> actions) throws Exception {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("inappropriate context");
}
FilePath workspacePath = jenkins.getWorkspaceFor(((WorkflowRun) handle.getExecutable()).getParent());
if (workspacePath == null || workspacePath.child(WorkflowBranchDefProjectFactory.SCRIPT).exists()) {
throw new IllegalStateException("inappropriate context");
}
Queue.Executable executable = handle.getExecutable();
File rootDir;
if (executable != null) {
WorkflowJob workflowJob = ((WorkflowJob) handle.getExecutable().getParent());
rootDir = workflowJob.getParent().getRootDir();
if (rootDir != null) {
File localConfig = new File(rootDir + File.separator + WorkflowBranchDefProjectFactory.SCRIPT);
if (localConfig.exists()) {
return new CpsFlowDefinition(FileUtils.readFileToString(localConfig, "utf-8"), false).
create(handle, listener, actions);
}
}
}
ConfigProvider configProvider = ConfigProvider.getByIdOrNull(GroovyScript.class.getName());
if (configProvider != null) {
Config config = configProvider.getConfigById(WorkflowBranchDefProjectFactory.SCRIPT);
if (config != null) {
return new CpsFlowDefinition(config.content, false).create(handle, listener, actions);
}
}
throw new IllegalArgumentException(WorkflowBranchDefProjectFactory.SCRIPT + " not found");
}
@Extension
public static class DescriptorImpl extends FlowDefinitionDescriptor {
@Override
public String getDisplayName() {
return "Pipeline script from default " + WorkflowBranchProjectFactory.SCRIPT;
}
}
/**
* Want to display this in the r/o configuration for a branch project, but not offer it on standalone jobs or in any other context.
*/
@Extension
public static class HideMeElsewhere extends DescriptorVisibilityFilter {
@Override
public boolean filter(Object context, Descriptor descriptor) {
if (descriptor instanceof DescriptorImpl) {
return context instanceof WorkflowJob && ((WorkflowJob) context).getParent() instanceof WorkflowMultiBranchDefProject;
}
return true;
}
}
}