forked from Praqma/pretested-integration-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPretestedIntegrationPostCheckout.java
More file actions
201 lines (182 loc) · 9.01 KB
/
PretestedIntegrationPostCheckout.java
File metadata and controls
201 lines (182 loc) · 9.01 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.jenkinsci.plugins.pretestedintegration;
import com.tikal.jenkins.plugins.multijob.MultiJobProject;
import hudson.AbortException;
import hudson.Extension;
import hudson.Launcher;
import hudson.matrix.*;
import hudson.model.*;
import hudson.plugins.git.GitSCM;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.pretestedintegration.scm.git.GitBridge;
import org.jenkinsci.plugins.pretestedintegration.scm.git.PretestedIntegrationAsGitPluginExt;
import org.kohsuke.stapler.DataBoundConstructor;
/**
* The publisher determines what will happen when the build has been run.
* Depending on the chosen SCM, a more specific function will be called.
*/
public class PretestedIntegrationPostCheckout extends Recorder implements Serializable, MatrixAggregatable {
private static final Logger LOGGER = Logger.getLogger(PretestedIntegrationPostCheckout.class.getName());
/**
* Constructor for PretestedIntegrationPostCheckout
*/
@DataBoundConstructor
public PretestedIntegrationPostCheckout() {
}
/**
* Gets the SCM Bridge of the BuildWrapper of this project.
*
* @param build the Build whose project to get the SCM Bridge of.
* @return the SCM Bridge of the BuildWrapper of this project.
* @throws AbortException When used outside of FreeStyle projects.
*/
private AbstractSCMBridge getScmBridge(AbstractBuild<?, ?> build, BuildListener listener) throws AbortException {
AbstractProject<?, ?> proj = build.getProject();
GitSCM client = (GitSCM)build.getProject().getScm();
if ( ! ( client instanceof GitSCM ) ) {
throw new AbortException("Unsupported SCM type.");
}
PretestedIntegrationAsGitPluginExt pretestedGitPluginExt = client.getExtensions().get(PretestedIntegrationAsGitPluginExt.class) ;
if ( pretestedGitPluginExt != null ) {
if (proj instanceof FreeStyleProject ) {
FreeStyleProject p = (FreeStyleProject) build.getProject();
PretestedIntegrationBuildWrapper wrapper = p.getBuildWrappersList().get(PretestedIntegrationBuildWrapper.class);
if ( wrapper != null ) {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "BuildWrapper also configured,but skip as GitExtension prevails");
}
} else if (proj instanceof MultiJobProject ) {
MultiJobProject p = (MultiJobProject) build.getProject();
PretestedIntegrationBuildWrapper wrapper = p.getBuildWrappersList().get(PretestedIntegrationBuildWrapper.class);
if ( wrapper != null ) {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "BuildWrapper also configured, but skip as GitExtension prevails");
}
}
return pretestedGitPluginExt.getGitBridge();
}
if (proj instanceof FreeStyleProject ) {
FreeStyleProject p = (FreeStyleProject) build.getProject();
PretestedIntegrationBuildWrapper wrapper = p.getBuildWrappersList().get(PretestedIntegrationBuildWrapper.class);
return wrapper.scmBridge;
} else if (proj instanceof MatrixProject) {
MatrixProject p = (MatrixProject)proj.getParent();
PretestedIntegrationBuildWrapper wrapper = p.getBuildWrappersList().get(PretestedIntegrationBuildWrapper.class);
return wrapper.scmBridge;
} else if (proj instanceof MultiJobProject ) {
MultiJobProject p = (MultiJobProject)proj.getParent();
PretestedIntegrationBuildWrapper wrapper = p.getBuildWrappersList().get(PretestedIntegrationBuildWrapper.class);
return wrapper.scmBridge;
} else {
throw new AbortException("Unsupported job type.");
}
}
/**
* Calls the SCM-specific function according to the chosen SCM.
*
* @param build The Build
* @param launcher The Launcher
* @param listener The BuildListener
* @return boolean True on success.
* @throws InterruptedException
* @throws IOException
*/
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
AbstractProject<?, ?> proj = build.getProject();
GitSCM client = (GitSCM)build.getProject().getScm();
if ( ! (client instanceof GitSCM) ) {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "Unsupported SCM type: expects Git");
return false;
}
GitBridge bridge = (GitBridge)getScmBridge(build, listener);
if ( client.getExtensions().get(PretestedIntegrationAsGitPluginExt.class ) != null ) {
if (proj instanceof MatrixConfiguration) {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "MatrixConfiguration/sub - skipping publisher - leaving it to root job");
} else {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "Performing pre-verified post build steps");
try {
bridge.handlePostBuild(build, launcher, listener);
} catch (NullPointerException | IllegalArgumentException e) {
listener.getLogger().println(String.format("Caught %s during post-checkout. Failing build.", e.getClass().getSimpleName()));
e.printStackTrace(listener.getLogger());
bridge.updateBuildDescription((Run)build);
throw new AbortException("Unexpected error. Trace written to log.");
} catch (IOException e) {
//All our known errors are IOExceptions. Just print the message, log the error.
listener.getLogger().println(e.getMessage());
LOGGER.log(Level.SEVERE, "IOException in post checkout", e);
bridge.updateBuildDescription(build, launcher, listener);
throw new AbortException(e.getMessage());
}
}
if ( build.getResult() == Result.SUCCESS ) {
}
bridge.updateBuildDescription((Run)build);
return true;
} else { /* */
if (proj instanceof MatrixConfiguration) {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "MatrixConfiguration/sub - skipping publisher - leaving it to root job");
bridge.updateBuildDescription(build, launcher, listener);
} else {
listener.getLogger().println(PretestedIntegrationBuildWrapper.LOG_PREFIX + "Performing pre-verified post build steps");
try {
bridge.handlePostBuild(build, launcher, listener);
} catch (NullPointerException | IllegalArgumentException e) {
listener.getLogger().println(String.format("Caught %s during post-checkout. Failing build.", e.getClass().getSimpleName()));
e.printStackTrace(listener.getLogger());
throw new AbortException("Unexpected error. Trace written to log.");
} catch (IOException e) {
//All our known errors are IOExceptions. Just print the message, log the error.
listener.getLogger().println(e.getMessage());
LOGGER.log(Level.SEVERE, "IOException in post checkout", e);
throw new AbortException(e.getMessage());
}
}
bridge.updateBuildDescription(build, launcher, listener);
return true;
}
}
/**
* For a matrix project, push should only happen once.
*/
public MatrixAggregator createAggregator(MatrixBuild build, Launcher launcher, BuildListener listener) {
return new MatrixAggregator(build,launcher,listener) {
@Override
public boolean endBuild() throws InterruptedException, IOException {
return PretestedIntegrationPostCheckout.this.perform(build,launcher,listener);
}
};
}
/**
* {@inheritDoc}
*/
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}
/**
* Descriptor Implementation for PretestedIntegrationPostCheckout
*/
@Extension(ordinal = Integer.MIN_VALUE)
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "Praqma Git Phlow - Update remote repository";
}
/**
* {@inheritDoc}
*/
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
}
}