Skip to content

Commit 76ecbbf

Browse files
jetersenbaymac
authored andcommitted
[JENKINS-57747] Add validation for GitLab Server Url, take two (#18)
* added server validation * removed unused imports * removed extra separation between imports * removed extra line * added a newline
1 parent 2a4a912 commit 76ecbbf

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServer.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
/**
4242
* Represents a GitLab Server instance.
4343
*/
44-
4544
public class GitLabServer extends AbstractDescribableImpl<GitLabServer> {
4645

4746
private static final Logger LOGGER = LoggerFactory.getLogger(GitLabServer.class);
@@ -50,6 +49,7 @@ public class GitLabServer extends AbstractDescribableImpl<GitLabServer> {
5049
* Used as default server URL for the serverUrl field
5150
*/
5251
public static final String GITLAB_SERVER_URL = "https://gitlab.com";
52+
5353
/**
5454
* Used as default token value if no any credentials found by given credentialsId.
5555
*/
@@ -220,15 +220,20 @@ public static FormValidation doCheckServerUrl(@QueryParameter String value) {
220220
try {
221221
new URL(value);
222222
} catch (MalformedURLException e) {
223-
LOGGER.error("Incorrect server url - %s", value);
224-
return FormValidation.error("Malformed GitLab url (%s)", e.getMessage());
223+
LOGGER.error("Incorrect url - %s", value);
224+
return FormValidation.error("Malformed url (%s)", e.getMessage());
225225
}
226-
// TODO:[JENKINS-57747] Add support for GitLab Ultimate (self hosted) and Gold (saas)
227226
if (GITLAB_SERVER_URL.equals(value)) {
228-
LOGGER.info("Server URL is fine - %s", value);
227+
LOGGER.info("Community version of GitLab - %s", value);
228+
}
229+
GitLabApi gitLabApi = new GitLabApi(value, "");
230+
try {
231+
gitLabApi.getProjectApi().getProjects(1, 1);
232+
return FormValidation.ok();
233+
} catch (GitLabApiException e) {
234+
LOGGER.info("Unable to validate serverUrl - %s", value);
235+
return FormValidation.error(Messages.GitLabServer_invalidUrl(value));
229236
}
230-
LOGGER.info("Unable to validate serverUrl - %s", value);
231-
return FormValidation.ok();
232237
}
233238

234239
@NonNull

src/main/resources/io/jenkins/plugins/gitlabserverconfig/servers/Messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ GitLabServer.displayName=GitLab Server
33
GitLabServer.hookManagementAs=Hook management will be performed as <code>{0} </code>
44
GitLabServer.validateInterrupted=Interrupted while trying to validate credentials
55
GitLabServer.versionInterrupted=Interrupted while trying to determine server version
6-
GitLabServer.invalidUrl=Invalid URL: <code>{0}</code>
6+
GitLabServer.invalidUrl=Invalid GitLab Server URL: <code>{0}</code>
77
GitLabServer.serverVersion=GitLab Version: <code>{0}</code>
88
GitLabServer.failedValidation=Failed to validate the account <code>{0}</code>
99
GitLabServer.credentialsNotResolved=Cannot resolve suitable credentials with id: <code>{0}</code>

0 commit comments

Comments
 (0)