1
1
package com .cloudbees .jenkins ;
2
2
3
3
import com .google .common .base .Charsets ;
4
- import com .google .common .base .Function ;
5
4
import hudson .Extension ;
6
5
import hudson .Util ;
6
+ import hudson .XmlFile ;
7
7
import hudson .console .AnnotatedLargeText ;
8
8
import hudson .model .AbstractProject ;
9
9
import hudson .model .Action ;
10
10
import hudson .model .Item ;
11
11
import hudson .model .Project ;
12
12
import hudson .triggers .Trigger ;
13
13
import hudson .triggers .TriggerDescriptor ;
14
- import hudson .util .FormValidation ;
15
14
import hudson .util .SequentialExecutionQueue ;
16
15
import hudson .util .StreamTaskListener ;
17
16
import jenkins .model .Jenkins ;
18
17
import jenkins .model .Jenkins .MasterComputer ;
19
- import net .sf .json .JSONObject ;
20
- import org .apache .commons .codec .binary .Base64 ;
21
18
import org .apache .commons .jelly .XMLOutput ;
22
- import org .jenkinsci .main .modules .instance_identity .InstanceIdentity ;
19
+ import org .jenkinsci .plugins .github .GitHubPlugin ;
20
+ import org .jenkinsci .plugins .github .config .GitHubPluginConfig ;
21
+ import org .jenkinsci .plugins .github .deprecated .Credential ;
23
22
import org .jenkinsci .plugins .github .internal .GHPluginConfigException ;
23
+ import org .jenkinsci .plugins .github .migration .Migrator ;
24
24
import org .kohsuke .stapler .DataBoundConstructor ;
25
- import org .kohsuke . stapler . QueryParameter ;
26
- import org .kohsuke . stapler . StaplerRequest ;
25
+ import org .slf4j . Logger ;
26
+ import org .slf4j . LoggerFactory ;
27
27
28
- import javax .inject .Inject ;
29
28
import java .io .File ;
30
29
import java .io .IOException ;
31
30
import java .io .PrintStream ;
32
- import java .net .HttpURLConnection ;
33
31
import java .net .MalformedURLException ;
34
32
import java .net .URL ;
35
- import java .security .interfaces .RSAPublicKey ;
36
33
import java .text .DateFormat ;
37
- import java .util .ArrayList ;
38
34
import java .util .Collection ;
39
35
import java .util .Collections ;
40
36
import java .util .Date ;
41
37
import java .util .List ;
42
38
import java .util .Set ;
43
- import java . util . logging . Level ;
44
- import java . util . logging . Logger ;
39
+
40
+ import static org . apache . commons . lang3 . StringUtils . isEmpty ;
45
41
46
42
/**
47
43
* Triggers a build when we receive a GitHub post-commit webhook.
@@ -85,17 +81,17 @@ private boolean runPolling() {
85
81
return result ;
86
82
} catch (Error e ) {
87
83
e .printStackTrace (listener .error ("Failed to record SCM polling" ));
88
- LOGGER .log ( Level . SEVERE , "Failed to record SCM polling" ,e );
84
+ LOGGER .error ( "Failed to record SCM polling" , e );
89
85
throw e ;
90
86
} catch (RuntimeException e ) {
91
87
e .printStackTrace (listener .error ("Failed to record SCM polling" ));
92
- LOGGER .log ( Level . SEVERE , "Failed to record SCM polling" ,e );
88
+ LOGGER .error ( "Failed to record SCM polling" , e );
93
89
throw e ;
94
90
} finally {
95
91
listener .close ();
96
92
}
97
93
} catch (IOException e ) {
98
- LOGGER .log ( Level . SEVERE , "Failed to record SCM polling" ,e );
94
+ LOGGER .error ( "Failed to record SCM polling" , e );
99
95
}
100
96
return false ;
101
97
}
@@ -107,13 +103,13 @@ public void run() {
107
103
try {
108
104
cause = new GitHubPushCause (getLogFile (), pushBy );
109
105
} catch (IOException e ) {
110
- LOGGER .log ( Level . WARNING , "Failed to parse the polling log" ,e );
106
+ LOGGER .warn ( "Failed to parse the polling log" , e );
111
107
cause = new GitHubPushCause (pushBy );
112
108
}
113
109
if (job .scheduleBuild (cause )) {
114
- LOGGER .info ("SCM changes detected in " + job .getName ()+ ". Triggering " + name );
110
+ LOGGER .info ("SCM changes detected in " + job .getName () + ". Triggering " + name );
115
111
} else {
116
- LOGGER .info ("SCM changes detected in " + job .getName ()+ ". Job is already in the queue" );
112
+ LOGGER .info ("SCM changes detected in " + job .getName () + ". Job is already in the queue" );
117
113
}
118
114
}
119
115
}
@@ -138,7 +134,7 @@ public Set<GitHubRepositoryName> getGitHubRepositories() {
138
134
@ Override
139
135
public void start (AbstractProject <?, ?> project , boolean newInstance ) {
140
136
super .start (project , newInstance );
141
- if (newInstance && getDescriptor ().isManageHook ()) {
137
+ if (newInstance && GitHubPlugin . configuration ().isManageHooks ()) {
142
138
registerHooks ();
143
139
}
144
140
}
@@ -154,14 +150,13 @@ public void registerHooks() {
154
150
GitHubWebHook .get ().registerHookFor (job );
155
151
}
156
152
157
-
158
153
@ Override
159
154
public void stop () {
160
155
if (job == null ) {
161
156
return ;
162
157
}
163
158
164
- if (getDescriptor ().isManageHook ()) {
159
+ if (GitHubPlugin . configuration ().isManageHooks ()) {
165
160
Cleaner cleaner = Cleaner .get ();
166
161
if (cleaner != null ) {
167
162
cleaner .onStop (job );
@@ -219,19 +214,11 @@ public void writeLogTo(XMLOutput out) throws IOException {
219
214
220
215
@ Extension
221
216
public static class DescriptorImpl extends TriggerDescriptor {
222
- private static final Logger LOGGER = Logger .getLogger (DescriptorImpl .class .getName ());
223
217
private transient final SequentialExecutionQueue queue = new SequentialExecutionQueue (MasterComputer .threadPoolForRemoting );
224
218
225
- private boolean manageHook ;
226
- private String hookUrl ;
227
- private volatile List <Credential > credentials = new ArrayList <Credential >();
228
-
229
- @ Inject
230
- private transient InstanceIdentity identity ;
219
+ private transient String hookUrl ;
231
220
232
- public DescriptorImpl () {
233
- load ();
234
- }
221
+ private transient List <Credential > credentials ;
235
222
236
223
@ Override
237
224
public boolean isApplicable (Item item ) {
@@ -245,91 +232,80 @@ public String getDisplayName() {
245
232
246
233
/**
247
234
* True if Jenkins should auto-manage hooks.
235
+ *
236
+ * @deprecated Use {@link GitHubPluginConfig#isManageHooks()} instead
248
237
*/
238
+ @ Deprecated
249
239
public boolean isManageHook () {
250
- return manageHook ;
251
- }
252
-
253
- public void setManageHook (boolean v ) {
254
- manageHook = v ;
255
- save ();
240
+ return GitHubPlugin .configuration ().isManageHooks ();
256
241
}
257
242
258
243
/**
259
244
* Returns the URL that GitHub should post.
245
+ *
246
+ * @deprecated use {@link GitHubPluginConfig#getHookUrl()} instead
260
247
*/
248
+ @ Deprecated
261
249
public URL getHookUrl () throws GHPluginConfigException {
262
- try {
263
- return hookUrl != null
264
- ? new URL (hookUrl )
265
- : new URL (Jenkins .getInstance ().getRootUrl () + GitHubWebHook .get ().getUrlName () + '/' );
266
- } catch (MalformedURLException e ) {
267
- throw new GHPluginConfigException (
268
- "Mailformed GH hook url in global configuration (%s)" , e .getMessage ()
269
- );
270
- }
271
- }
272
-
273
- public boolean hasOverrideURL () {
274
- return hookUrl != null ;
250
+ return GitHubPlugin .configuration ().getHookUrl ();
275
251
}
276
252
253
+ /**
254
+ * @return null after migration
255
+ * @deprecated use {@link GitHubPluginConfig#getConfigs()} instead.
256
+ */
257
+ @ Deprecated
277
258
public List <Credential > getCredentials () {
278
259
return credentials ;
279
260
}
280
261
281
- @ Override
282
- public boolean configure (StaplerRequest req , JSONObject json ) throws FormException {
283
- JSONObject hookMode = json .getJSONObject ("hookMode" );
284
- manageHook = "auto" .equals (hookMode .getString ("value" ));
285
- if (hookMode .optBoolean ("hasHookUrl" )) {
286
- hookUrl = hookMode .optString ("hookUrl" );
287
- } else {
288
- hookUrl = null ;
262
+ /**
263
+ * Used only for migration
264
+ *
265
+ * @return null after migration
266
+ * @deprecated use {@link GitHubPluginConfig#getHookUrl()}
267
+ */
268
+ @ Deprecated
269
+ public URL getDeprecatedHookUrl () {
270
+ if (isEmpty (hookUrl )) {
271
+ return null ;
289
272
}
290
- credentials = req .bindJSONToList (Credential .class , hookMode .get ("credentials" ));
291
- save ();
292
- return true ;
293
- }
294
-
295
- public FormValidation doCheckHookUrl (@ QueryParameter String value ) {
296
273
try {
297
- HttpURLConnection con = (HttpURLConnection ) new URL (value ).openConnection ();
298
- con .setRequestMethod ("POST" );
299
- con .setRequestProperty (GitHubWebHook .URL_VALIDATION_HEADER , "true" );
300
- con .connect ();
301
- if (con .getResponseCode ()!=200 ) {
302
- return FormValidation .error ("Got " +con .getResponseCode ()+" from " +value );
303
- }
304
- String v = con .getHeaderField (GitHubWebHook .X_INSTANCE_IDENTITY );
305
- if (v == null ) {
306
- // people might be running clever apps that's not Jenkins, and that's OK
307
- return FormValidation .warning ("It doesn't look like " + value + " is talking to any Jenkins. Are you running your own app?" );
308
- }
309
- RSAPublicKey key = identity .getPublic ();
310
- String expected = new String (Base64 .encodeBase64 (key .getEncoded ()));
311
- if (!expected .equals (v )) {
312
- // if it responds but with a different ID, that's more likely wrong than correct
313
- return FormValidation .error (value +" is connecting to different Jenkins instances" );
314
- }
315
-
316
- return FormValidation .ok ();
317
- } catch (IOException e ) {
318
- return FormValidation .error (e ,"Failed to test a connection to " +value );
274
+ return new URL (hookUrl );
275
+ } catch (MalformedURLException e ) {
276
+ LOGGER .warn ("Mailformed hook url skipped while migration ({})" , e .getMessage ());
277
+ return null ;
319
278
}
279
+ }
320
280
281
+ /**
282
+ * Used to cleanup after migration
283
+ */
284
+ public void clearDeprecatedHookUrl () {
285
+ this .hookUrl = null ;
321
286
}
322
287
323
- @ SuppressWarnings ("unused" )
324
- public FormValidation doReRegister () {
325
- if (!manageHook ) {
326
- return FormValidation .warning ("Works only when Jenkins manages hooks" );
327
- }
288
+ /**
289
+ * Used to cleanup after migration
290
+ */
291
+ public void clearCredentials () {
292
+ this .credentials = null ;
293
+ }
328
294
329
- List <AbstractProject > registered = GitHubWebHook .get ().reRegisterAllHooks ();
295
+ /**
296
+ * @deprecated use {@link GitHubPluginConfig#isOverrideHookURL()}
297
+ */
298
+ @ Deprecated
299
+ public boolean hasOverrideURL () {
300
+ return GitHubPlugin .configuration ().isOverrideHookURL ();
301
+ }
330
302
331
- LOGGER .log (Level .INFO , "Called registerHooks() for {0} jobs" , registered .size ());
332
- return FormValidation .ok ("Called re-register hooks for %s jobs" , registered .size ());
303
+ /**
304
+ * Uses global xstream to enable migration alias used in {@link Migrator#enableCompatibilityAliases()}
305
+ */
306
+ @ Override
307
+ protected XmlFile getConfigFile () {
308
+ return new XmlFile (Jenkins .XSTREAM2 , super .getConfigFile ().getFile ());
333
309
}
334
310
335
311
public static DescriptorImpl get () {
@@ -346,5 +322,5 @@ public static boolean allowsHookUrlOverride() {
346
322
*/
347
323
public static boolean ALLOW_HOOKURL_OVERRIDE = !Boolean .getBoolean (GitHubPushTrigger .class .getName () + ".disableOverride" );
348
324
349
- private static final Logger LOGGER = Logger .getLogger (GitHubPushTrigger .class . getName () );
325
+ private static final Logger LOGGER = LoggerFactory .getLogger (GitHubPushTrigger .class );
350
326
}
0 commit comments