1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.bugzilla;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.maven.model.IssueManagement;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.project.MavenProject;
25 import org.apache.maven.settings.Server;
26 import org.apache.maven.settings.Settings;
27 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
28 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
29
30 import de.smartics.maven.issue.command.CommandTarget;
31 import de.smartics.maven.issue.util.LogLevel;
32 import de.smartics.maven.issue.util.MavenProjectWrapper;
33 import de.smartics.maven.issue.util.SettingsDecrypter;
34
35
36
37
38 public abstract class AbstractIssueMojo extends AbstractMojo
39 {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 private MavenProject project;
57
58
59
60
61
62
63
64
65
66 protected Settings settings;
67
68
69
70
71
72
73
74
75 private SecDispatcher securityDispatcher;
76
77
78
79
80
81
82
83
84 private String settingsSecurityLocation;
85
86
87
88
89
90 private SettingsDecrypter settingsDecrypter;
91
92
93
94
95
96
97
98
99
100
101
102
103 protected String issueServerId;
104
105
106
107
108
109
110 protected IssueServer server;
111
112
113
114
115 protected Console console;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 protected String verbose;
132
133
134
135
136
137
138
139
140
141
142
143
144 protected static final class IssueServer extends Server
145 {
146
147
148
149
150
151
152 private static final long serialVersionUID = 1L;
153
154
155
156
157 private final String url;
158
159 private IssueServer(final SettingsDecrypter settingsDecrypter,
160 final Server server, final String url) throws MojoExecutionException
161 {
162 this.url = url;
163
164 if (server != null)
165 {
166 this.setId(server.getId());
167 this.setUsername(server.getUsername());
168
169 try
170 {
171 final String plain = server.getPassword();
172 final String decrypted = settingsDecrypter.decrypt(plain);
173
174 this.setPassword(decrypted);
175 this.setPassphrase(server.getPassphrase());
176 this.setPrivateKey(server.getPrivateKey());
177
178 this.setDirectoryPermissions(server.getDirectoryPermissions());
179 this.setFilePermissions(server.getFilePermissions());
180 this.setConfiguration(server.getConfiguration());
181
182 this.setSourceLevel(server.getSourceLevel());
183 }
184 catch (final SecDispatcherException e)
185 {
186 throw new MojoExecutionException(
187 "Cannot decrypt '" + server.getId()
188 + "' server password for user name '" + server.getUsername()
189 + "'.", e);
190 }
191 }
192 }
193
194
195
196
197
198
199 public String getUrl()
200 {
201 return url;
202 }
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216 protected final MavenProject getProject()
217 {
218 return new MavenProjectWrapper(project);
219 }
220
221
222
223
224
225
226
227
228
229
230 protected final IssueServer getIssueServer() throws MojoExecutionException
231 {
232 final IssueManagement issueManagement = project.getIssueManagement();
233 if (issueManagement == null)
234 {
235 throw new MojoExecutionException(
236 "No issue management specified. Please provide issue information"
237 + " within the 'issueManagement' block of your POM.");
238 }
239
240 final Server server = getIssueServerFromSettings(issueManagement);
241 final String ciUrl = issueManagement.getUrl();
242 return new IssueServer(settingsDecrypter, server, ciUrl);
243 }
244
245
246
247
248
249
250
251 @Override
252 public void execute() throws MojoExecutionException, MojoFailureException
253
254 {
255 final Log log = getLog();
256
257 settingsDecrypter =
258 new SettingsDecrypter(securityDispatcher, settingsSecurityLocation);
259
260 server = getIssueServer();
261 final String url = server.getUrl();
262 final LogLevel logLevel = new LogLevel(verbose);
263 final CommandTarget target = new CommandTarget(url);
264
265 console = new Console(target, log, logLevel);
266 }
267
268 private Server getIssueServerFromSettings(
269 final IssueManagement issueManagement) throws MojoExecutionException
270 {
271 final String issueUrl = issueManagement.getUrl();
272 if (StringUtils.isBlank(issueUrl))
273 {
274 throw new MojoExecutionException(
275 "No issue URL specified. Please provide issue information within the"
276 + " 'issueManagement/url' block of your POM.");
277 }
278
279 Server server;
280 if (StringUtils.isNotBlank(issueServerId))
281 {
282 server = settings.getServer(issueServerId);
283 if (server != null)
284 {
285 return server;
286 }
287 }
288
289 final String ciName = issueManagement.getSystem();
290 if (StringUtils.isNotBlank(ciName))
291 {
292 server = settings.getServer(ciName);
293 if (server != null)
294 {
295 return server;
296 }
297 }
298
299 server = settings.getServer(issueUrl);
300
301 return server;
302 }
303
304
305
306
307
308
309
310 protected final boolean isToSkip()
311 {
312 return !project.isExecutionRoot();
313 }
314
315
316
317 }