forked from Praqma/pretested-integration-plugin
/
AbstractSCMBridge.java
185 lines (157 loc) · 7.27 KB
/
AbstractSCMBridge.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.jenkinsci.plugins.pretestedintegration;
import hudson.plugins.git.Branch;
import hudson.plugins.git.util.BuildData;
import org.jenkinsci.plugins.pretestedintegration.exceptions.EstablishWorkspaceException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegationFailedExeception;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NextCommitFailureException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.kohsuke.stapler.DataBoundConstructor;
import jenkins.model.Jenkins;
import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.Launcher;
import hudson.model.BuildListener;
import hudson.model.Describable;
import hudson.model.AbstractBuild;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.pretestedintegration.exceptions.CommitChangesFailureException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.DeleteIntegratedBranchException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.RollbackFailureException;
public abstract class AbstractSCMBridge implements Describable<AbstractSCMBridge>, ExtensionPoint {
protected String branch;
public final IntegrationStrategy integrationStrategy;
final static String LOG_PREFIX = "[PREINT-SCM] ";
@DataBoundConstructor
public AbstractSCMBridge(IntegrationStrategy integrationStrategy) {
this.integrationStrategy = integrationStrategy;
}
public String getBranch() {
return branch;
}
public Descriptor<AbstractSCMBridge> getDescriptor() {
return (SCMBridgeDescriptor<?>) Jenkins.getInstance().getDescriptorOrDie(getClass());
}
public static DescriptorExtensionList<AbstractSCMBridge, SCMBridgeDescriptor<AbstractSCMBridge>> all() {
return Jenkins.getInstance().<AbstractSCMBridge, SCMBridgeDescriptor<AbstractSCMBridge>>getDescriptorList(AbstractSCMBridge.class);
}
public static List<SCMBridgeDescriptor<?>> getDescriptors() {
List<SCMBridgeDescriptor<?>> list = new ArrayList<SCMBridgeDescriptor<?>>();
for (SCMBridgeDescriptor<?> d : all()) {
list.add(d);
}
return list;
}
public static List<IntegrationStrategyDescriptor<?>> getBehaviours() {
List<IntegrationStrategyDescriptor<?>> list = new ArrayList<IntegrationStrategyDescriptor<?>>();
for(IntegrationStrategyDescriptor<?> descr : IntegrationStrategy.all()) {
list.add(descr);
}
return list;
}
/**
* This function is called after the SCM plugin has updated the workspace
* with remote changes. When this function has been run, the workspace must
* be ready to perform a build and tests. The integration branch must be
* checked out, and the given commit must be merged into it.
*
* @param build
* @param launcher
* @param listener
* @param commit This commit represents the code that must be checked out.
*
* @throws IntegationFailedExeception
* @throws EstablishWorkspaceException
* @throws NothingToDoException
*/
public void prepareWorkspace(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, Commit<?> commit) throws EstablishWorkspaceException, NothingToDoException, IntegationFailedExeception {
mergeChanges(build, launcher, listener, commit);
}
/**
* Default is to use the selected integration strategy
* @param build
* @param launcher
* @param listener
* @param commit
* @throws NothingToDoException
* @throws IntegationFailedExeception
*/
protected void mergeChanges(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, Commit<?> commit) throws NothingToDoException, IntegationFailedExeception {
integrationStrategy.integrate(build, launcher, listener, this, commit);
}
public abstract void ensureBranch(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, String branch) throws EstablishWorkspaceException;
/**
* Override this to associate an integrated commit with a pointer with the starting point before merge. This is used to roll back in case of integraion failure
* @param build
* @param launcher
* @param listener
* @return
*/
protected Commit<?> determineIntegrationHead(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) {
return null;
}
/**
* Calculate and return the next commit from the argument
*
* @param build
* @param launcher
* @param listener
* @param commit
* @return The next pending commit. If no commit is pending null is
* returned.
*
* @throws NextCommitFailureException
*/
public Commit<?> nextCommit(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, Commit<?> commit) throws NextCommitFailureException {
return null;
}
public void commit(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws CommitChangesFailureException {
//nop
}
public void rollback(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws RollbackFailureException {
//nop
}
public void deleteIntegratedBranch(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws DeleteIntegratedBranchException {
//nop
}
public void updateBuildDescription(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
//nop
}
public Result getRequiredResult() {
return Result.SUCCESS;
}
/**
* This is called after the build has run. If the build was successful, the
* changes should be committed, otherwise the workspace is cleared as before
* the changes
*
* @param build The status of the build.
* @param launcher
* @param listener
*
* @throws IOException A repository could not be reached.
*/
public void handlePostBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException {
Result result = build.getResult();
updateBuildDescription(build, launcher, listener);
BuildData gitBuildData = build.getAction(BuildData.class);
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();
String devBranchName = gitDataBranch.getName();
if (devBranchName.isEmpty() || devBranchName.contains("master")) {
listener.getLogger().println(LOG_PREFIX + "Using the master branch for development is not allowed.");
build.setResult(Result.FAILURE);
}
if (result != null && result.isBetterOrEqualTo(getRequiredResult())) {
listener.getLogger().println(LOG_PREFIX + "Commiting changes");
commit(build, launcher, listener);
listener.getLogger().println(LOG_PREFIX + "Deleting development branch");
deleteIntegratedBranch(build, launcher, listener);
}
}
private static final Logger logger = Logger.getLogger(AbstractSCMBridge.class.getName());
}