4
4
import com .google .common .base .Predicate ;
5
5
import com .google .common .base .Predicates ;
6
6
import hudson .Extension ;
7
- import hudson .model . AbstractDescribableImpl ;
7
+ import hudson .XmlFile ;
8
8
import hudson .model .AbstractProject ;
9
9
import hudson .model .Descriptor ;
10
10
import hudson .util .FormValidation ;
11
+ import jenkins .model .GlobalConfiguration ;
11
12
import jenkins .model .Jenkins ;
13
+ import net .sf .json .JSONObject ;
12
14
import org .apache .commons .codec .binary .Base64 ;
13
15
import org .jenkinsci .main .modules .instance_identity .InstanceIdentity ;
14
16
import org .jenkinsci .plugins .github .GitHubPlugin ;
15
17
import org .jenkinsci .plugins .github .internal .GHPluginConfigException ;
18
+ import org .jenkinsci .plugins .github .migration .Migrator ;
16
19
import org .kohsuke .github .GitHub ;
17
- import org .kohsuke .stapler .DataBoundConstructor ;
18
- import org .kohsuke .stapler .DataBoundSetter ;
19
20
import org .kohsuke .stapler .QueryParameter ;
21
+ import org .kohsuke .stapler .StaplerRequest ;
20
22
import org .slf4j .Logger ;
21
23
import org .slf4j .LoggerFactory ;
22
24
30
32
import java .util .Collections ;
31
33
import java .util .List ;
32
34
35
+ import static java .lang .String .format ;
33
36
import static org .jenkinsci .plugins .github .config .GitHubServerConfig .allowedToManageHooks ;
34
37
import static org .jenkinsci .plugins .github .config .GitHubServerConfig .loginToGithub ;
35
38
import static org .jenkinsci .plugins .github .util .FluentIterableWrapper .from ;
41
44
* @author lanwen (Merkushev Kirill)
42
45
* @since TODO
43
46
*/
44
- public class GitHubPluginConfig extends AbstractDescribableImpl <GitHubPluginConfig > {
47
+ @ Extension
48
+ public class GitHubPluginConfig extends GlobalConfiguration {
45
49
private static final Logger LOGGER = LoggerFactory .getLogger (GitHubPluginConfig .class );
50
+ public static final String GITHUB_PLUGIN_CONFIGURATION_ID = "github-plugin-configuration" ;
51
+
52
+ /**
53
+ * Helps to avoid null in {@link GitHubPlugin#configuration()}
54
+ */
55
+ public static final GitHubPluginConfig EMPTY_CONFIG =
56
+ new GitHubPluginConfig (Collections .<GitHubServerConfig >emptyList ());
46
57
47
58
private List <GitHubServerConfig > configs = new ArrayList <GitHubServerConfig >();
48
59
private URL hookUrl ;
49
60
private transient boolean overrideHookUrl ;
50
61
51
- @ DataBoundConstructor
62
+ /**
63
+ * Used to get current instance identity.
64
+ * It compared with same value when testing hook url availability in {@link #doCheckHookUrl(String)}
65
+ */
66
+ @ Inject
67
+ @ SuppressWarnings ("unused" )
68
+ private transient InstanceIdentity identity ;
69
+
52
70
public GitHubPluginConfig () {
71
+ load ();
53
72
}
54
73
55
- public List <GitHubServerConfig > getConfigs ( ) {
56
- return configs ;
74
+ public GitHubPluginConfig ( List <GitHubServerConfig > configs ) {
75
+ this . configs = configs ;
57
76
}
58
77
59
- @ DataBoundSetter
78
+ @ SuppressWarnings ( "unused" )
60
79
public void setConfigs (List <GitHubServerConfig > configs ) {
61
80
this .configs = configs ;
62
81
}
63
82
83
+ public List <GitHubServerConfig > getConfigs () {
84
+ return configs ;
85
+ }
86
+
64
87
public boolean isManageHooks () {
65
88
return from (getConfigs ()).filter (allowedToManageHooks ()).first ().isPresent ();
66
89
}
67
90
68
- @ DataBoundSetter
69
91
public void setHookUrl (URL hookUrl ) {
70
92
if (overrideHookUrl ) {
71
93
this .hookUrl = hookUrl ;
@@ -74,7 +96,6 @@ public void setHookUrl(URL hookUrl) {
74
96
}
75
97
}
76
98
77
- @ DataBoundSetter
78
99
public void setOverrideHookUrl (boolean overrideHookUrl ) {
79
100
this .overrideHookUrl = overrideHookUrl ;
80
101
}
@@ -111,60 +132,78 @@ public List<Descriptor> actions() {
111
132
return Collections .singletonList (Jenkins .getInstance ().getDescriptor (GitHubTokenCredentialsCreator .class ));
112
133
}
113
134
114
- @ Extension
115
- public static class GitHubPluginConfigDescriptor extends Descriptor <GitHubPluginConfig > {
135
+ /**
136
+ * To avoid long class name as id in xml tag name and config file
137
+ */
138
+ @ Override
139
+ public String getId () {
140
+ return GITHUB_PLUGIN_CONFIGURATION_ID ;
141
+ }
116
142
117
- /**
118
- * Used to get current instance identity. It compared with same value when testing hook url availability
119
- */
120
- @ Inject
121
- @ SuppressWarnings ("unused" )
122
- private transient InstanceIdentity identity ;
143
+ /**
144
+ * @return config file with global {@link com.thoughtworks.xstream.XStream} instance
145
+ * with enabled aliases in {@link Migrator#enableAliases()}
146
+ */
147
+ @ Override
148
+ protected XmlFile getConfigFile () {
149
+ return new XmlFile (Jenkins .XSTREAM2 , super .getConfigFile ().getFile ());
150
+ }
123
151
124
- @ Override
125
- public String getDisplayName () {
126
- return "GitHub Plugin Configuration" ;
152
+ @ Override
153
+ public boolean configure (StaplerRequest req , JSONObject json ) throws FormException {
154
+ try {
155
+ req .bindJSON (this , json );
156
+ } catch (Exception e ) {
157
+ throw new FormException (
158
+ format ("Mailformed GitHub Plugin configuration (%s)" , e .getMessage ()), e , "github-configuration" );
127
159
}
160
+ save ();
161
+ return true ;
162
+ }
128
163
129
- @ SuppressWarnings ("unused" )
130
- public FormValidation doReRegister () {
131
- if (!GitHubPlugin .configuration ().isManageHooks ()) {
132
- return FormValidation .warning ("Works only when Jenkins manages hooks (one ore more creds specified)" );
133
- }
134
-
135
- List <AbstractProject > registered = GitHubWebHook .get ().reRegisterAllHooks ();
164
+ @ Override
165
+ public String getDisplayName () {
166
+ return "GitHub Plugin Configuration" ;
167
+ }
136
168
137
- LOGGER .info ("Called registerHooks() for {} jobs" , registered .size ());
138
- return FormValidation .ok ("Called re-register hooks for %s jobs" , registered .size ());
169
+ @ SuppressWarnings ("unused" )
170
+ public FormValidation doReRegister () {
171
+ if (!GitHubPlugin .configuration ().isManageHooks ()) {
172
+ return FormValidation .warning ("Works only when Jenkins manages hooks (one ore more creds specified)" );
139
173
}
140
174
141
- @ SuppressWarnings ("unused" )
142
- public FormValidation doCheckHookUrl (@ QueryParameter String value ) {
143
- try {
144
- HttpURLConnection con = (HttpURLConnection ) new URL (value ).openConnection ();
145
- con .setRequestMethod ("POST" );
146
- con .setRequestProperty (GitHubWebHook .URL_VALIDATION_HEADER , "true" );
147
- con .connect ();
148
- if (con .getResponseCode () != 200 ) {
149
- return FormValidation .error ("Got %d from %s" , con .getResponseCode (), value );
150
- }
151
- String v = con .getHeaderField (GitHubWebHook .X_INSTANCE_IDENTITY );
152
- if (v == null ) {
153
- // people might be running clever apps that's not Jenkins, and that's OK
154
- return FormValidation .warning ("It doesn't look like %s is talking to any Jenkins. " +
155
- "Are you running your own app?" , value );
156
- }
157
- RSAPublicKey key = identity .getPublic ();
158
- String expected = new String (Base64 .encodeBase64 (key .getEncoded ()));
159
- if (!expected .equals (v )) {
160
- // if it responds but with a different ID, that's more likely wrong than correct
161
- return FormValidation .error ("%s is connecting to different Jenkins instances" , value );
162
- }
163
-
164
- return FormValidation .ok ();
165
- } catch (IOException e ) {
166
- return FormValidation .error (e , "Failed to test a connection to %s" , value );
175
+ List <AbstractProject > registered = GitHubWebHook .get ().reRegisterAllHooks ();
176
+
177
+ LOGGER .info ("Called registerHooks() for {} jobs" , registered .size ());
178
+ return FormValidation .ok ("Called re-register hooks for %s jobs" , registered .size ());
179
+ }
180
+
181
+ @ SuppressWarnings ("unused" )
182
+ public FormValidation doCheckHookUrl (@ QueryParameter String value ) {
183
+ try {
184
+ HttpURLConnection con = (HttpURLConnection ) new URL (value ).openConnection ();
185
+ con .setRequestMethod ("POST" );
186
+ con .setRequestProperty (GitHubWebHook .URL_VALIDATION_HEADER , "true" );
187
+ con .connect ();
188
+ if (con .getResponseCode () != 200 ) {
189
+ return FormValidation .error ("Got %d from %s" , con .getResponseCode (), value );
167
190
}
191
+ String v = con .getHeaderField (GitHubWebHook .X_INSTANCE_IDENTITY );
192
+ if (v == null ) {
193
+ // people might be running clever apps that's not Jenkins, and that's OK
194
+ return FormValidation .warning ("It doesn't look like %s is talking to any Jenkins. "
195
+ + "Are you running your own app?" , value );
196
+ }
197
+ RSAPublicKey key = identity .getPublic ();
198
+ String expected = new String (Base64 .encodeBase64 (key .getEncoded ()));
199
+ if (!expected .equals (v )) {
200
+ // if it responds but with a different ID, that's more likely wrong than correct
201
+ return FormValidation .error ("%s is connecting to different Jenkins instances" , value );
202
+ }
203
+
204
+ return FormValidation .ok ();
205
+ } catch (IOException e ) {
206
+ return FormValidation .error (e , "Failed to test a connection to %s" , value );
168
207
}
169
208
}
170
209
}
0 commit comments