22
33import hudson .Extension ;
44import hudson .ExtensionList ;
5- import hudson .Util ;
65import hudson .model .Descriptor ;
6+ import hudson .model .PersistentDescriptor ;
77import hudson .util .ListBoxModel ;
88import io .jenkins .plugins .gitlabserverconfig .servers .helpers .GitLabPersonalAccessTokenCreator ;
99import java .util .ArrayList ;
1010import java .util .Collections ;
11- import java .util .Iterator ;
1211import java .util .List ;
12+ import java .util .Map ;
13+ import java .util .concurrent .ConcurrentHashMap ;
14+ import java .util .function .Function ;
15+ import java .util .function .Predicate ;
16+ import java .util .stream .Collectors ;
1317import javax .annotation .CheckForNull ;
1418import javax .annotation .Nonnull ;
1519import jenkins .model .GlobalConfiguration ;
1620import jenkins .model .Jenkins ;
17- import net .sf .json .JSONObject ;
1821import org .apache .commons .lang .StringUtils ;
19- import org .kohsuke .stapler .StaplerRequest ;
2022import org .slf4j .Logger ;
2123import org .slf4j .LoggerFactory ;
2224
25+ import static hudson .Util .fixNull ;
26+
2327/**
2428 * Represents the global configuration of GitLab servers.
2529 */
2630@ Extension
27- public class GitLabServers extends GlobalConfiguration {
31+ public class GitLabServers extends GlobalConfiguration implements PersistentDescriptor {
2832
2933 private static final Logger LOGGER = LoggerFactory .getLogger (GitLabServers .class );
3034
@@ -35,20 +39,17 @@ public class GitLabServers extends GlobalConfiguration {
3539 private List <GitLabServer > servers ;
3640
3741 /**
38- * Constructor.
39- */
40- public GitLabServers () {
41- load ();
42- }
43-
44- /**
45- * {@inheritDoc}
42+ * Helper function to get predicate to filter servers
43+ * based on their names
44+ *
45+ * @param keyExtractor the Function to filter
46+ * @param <T> In this case it is server
47+ * @return a predicate to filter servers list
4648 */
47- @ Override
48- public boolean configure (StaplerRequest req , JSONObject json ) throws FormException {
49- servers = req .bindJSONToList (GitLabServer .class , json .get ("servers" ));
50- save ();
51- return super .configure (req , json );
49+ private static <T > Predicate <T > distinctByKey (
50+ Function <? super T , ?> keyExtractor ) {
51+ Map <Object , Boolean > seen = new ConcurrentHashMap <>();
52+ return t -> seen .putIfAbsent (keyExtractor .apply (t ), Boolean .TRUE ) == null ;
5253 }
5354
5455 /**
@@ -61,15 +62,15 @@ public static GitLabServers get() {
6162 }
6263
6364 /**
64- * Populates a {@link ListBoxModel} with the endpoints .
65+ * Populates a {@link ListBoxModel} with the servers .
6566 *
66- * @return A {@link ListBoxModel} with all the endpoints
67+ * @return A {@link ListBoxModel} with all the servers
6768 */
6869 public ListBoxModel getServerItems () {
6970 ListBoxModel result = new ListBoxModel ();
70- for (GitLabServer endpoint : getServers ()) {
71- String serverUrl = endpoint .getServerUrl ();
72- String displayName = endpoint .getName ();
71+ for (GitLabServer server : getServers ()) {
72+ String serverUrl = server .getServerUrl ();
73+ String displayName = server .getName ();
7374 result .add (StringUtils .isBlank (displayName ) ? serverUrl : displayName + " (" + serverUrl + ")" , serverUrl );
7475 }
7576 return result ;
@@ -103,74 +104,70 @@ public List<Descriptor> actions() {
103104 }
104105
105106 /**
106- * Sets the list of endpoints.
107+ * Sets the list of GitLab Servers
107108 *
108- * @param endpoints the list of endpoints.
109+ * @param servers the list of endpoints.
109110 */
110- public void setServers (@ CheckForNull List <? extends GitLabServer > endpoints ) {
111+ public void setServers (@ CheckForNull List <? extends GitLabServer > servers ) {
111112 Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
112- servers = new ArrayList <>(Util .fixNull (endpoints ));
113+ this .servers = fixNull (servers ).stream ()
114+ .filter (distinctByKey (GitLabServer ::getName )).collect (Collectors .toList ());
115+ save ();
113116 }
114117
115118 /**
116- * Adds an endpoint
119+ * Adds an server
117120 * Checks if the GitLab Server name is unique
118121 *
119- * @param endpoint the endpoint to add.
122+ * @param server the server to add.
120123 * @return {@code true} if the list of endpoints was modified
121124 */
122- public boolean addServer (@ Nonnull GitLabServer endpoint ) {
123- List <GitLabServer > endpoints = new ArrayList <>(getServers ());
124- for (GitLabServer ep : endpoints ) {
125- if (Util .fixNull (ep .getName ()).equals (Util .fixNull (endpoint .getName ()))) {
126- return false ;
127- }
125+ public boolean addServer (@ Nonnull GitLabServer server ) {
126+ List <GitLabServer > servers = new ArrayList <>(getServers ());
127+ GitLabServer s = servers .stream ()
128+ .filter (server1 -> server1 .getName ().equals (server .getName ()))
129+ .findAny ()
130+ .orElse (null );
131+ if (s != null ) {
132+ return false ;
128133 }
129- endpoints .add (endpoint );
130- setServers (endpoints );
134+ servers .add (server );
135+ setServers (servers );
131136 return true ;
132137 }
133138
134139 /**
135140 * Updates an existing endpoint (or adds if missing)
136141 * Checks if the GitLab Server name is matched
137142 *
138- * @param endpoint the endpoint to update.
143+ * @param server the server to update.
144+ * @return {@code true} if the list of endpoints was modified
139145 */
140- public void updateServer (@ Nonnull GitLabServer endpoint ) {
141- List <GitLabServer > endpoints = new ArrayList <>(getServers ());
142- boolean found = false ;
143- for (int i = 0 ; i < endpoints .size (); i ++) {
144- GitLabServer ep = endpoints .get (i );
145- if (Util .fixNull (ep .getName ()).equals (Util .fixNull (endpoint .getName ()))) {
146- endpoints .set (i , endpoint );
147- found = true ;
148- break ;
149- }
150- }
151- if (!found ) {
152- endpoints .add (endpoint );
146+ public boolean updateServer (@ Nonnull GitLabServer server ) {
147+ List <GitLabServer > servers = new ArrayList <>(getServers ());
148+ if (!servers .contains (server )) {
149+ return false ;
153150 }
154- setServers (endpoints );
151+ servers = servers .stream ()
152+ .map (oldServer -> oldServer .getName ().equals (server .getName ()) ? server : oldServer )
153+ .collect (Collectors .toList ());
154+ setServers (servers );
155+ return true ;
155156 }
156157
157158 /**
158- * Removes an endpoint.
159+ * Removes a server entry
159160 * Checks if the GitLab Server name is matched
160161 *
161- * @param name the server URL to remove.
162+ * @param name the server name to remove.
162163 * @return {@code true} if the list of endpoints was modified
163164 */
164165 public boolean removeServer (@ CheckForNull String name ) {
165- boolean modified = false ;
166- List <GitLabServer > endpoints = new ArrayList <>(getServers ());
167- for (Iterator <GitLabServer > iterator = endpoints .iterator (); iterator .hasNext (); ) {
168- if (Util .fixNull (name ).equals (Util .fixNull (iterator .next ().getName ()))) {
169- iterator .remove ();
170- modified = true ;
171- }
166+ List <GitLabServer > servers = new ArrayList <>(getServers ());
167+ boolean removed = servers .removeIf (s -> s .getName ().equals (name ));
168+ if (removed ) {
169+ setServers (servers );
172170 }
173- setServers (endpoints );
174- return modified ;
171+ return removed ;
175172 }
176173}
0 commit comments