diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java index 25e6a2e9..b7b2ba9a 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java @@ -18,6 +18,7 @@ import hudson.util.FormValidation; import hudson.util.ListBoxModel; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar; +import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabGroup; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabOwner; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabUser; @@ -284,7 +285,7 @@ public void visitSources(@NonNull final SCMSourceObserver observer) } } observer.getListener().getLogger().format("%n%d projects were processed%n", count); - } catch (GitLabApiException | NoSuchFieldException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } } @@ -330,36 +331,35 @@ protected List retrieveActions(@NonNull SCMNavigatorOwner owner, SCMNavigatorEvent event, @NonNull TaskListener listener) throws IOException, InterruptedException { LOGGER.info("retrieving actions.."); - try { + if (gitlabOwner == null) { GitLabApi gitLabApi = apiBuilder(serverName); - if (gitlabOwner == null) { - gitlabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner); - } - String fullName = gitlabOwner.getFullName(); - String webUrl = gitlabOwner.getWebUrl(); - String avatarUrl = gitlabOwner.getAvatarUrl(); - List result = new ArrayList<>(); - result.add(new ObjectMetadataAction( - Util.fixEmpty(fullName), - null, - webUrl) - ); - if (StringUtils.isNotBlank(avatarUrl)) { - result.add(new GitLabAvatar(avatarUrl)); - } - result.add(GitLabLink.toGroup(webUrl)); - if (StringUtils.isBlank(webUrl)) { - listener.getLogger().println("Web URL unspecified"); - } else { - listener.getLogger().printf("%s URL: %s%n", gitlabOwner.getWord(), - HyperlinkNote - .encodeTo(webUrl, StringUtils.defaultIfBlank(fullName, webUrl))); - } - return result; - } catch (NoSuchFieldException e) { - e.printStackTrace(); - throw new IOException("Server Not found"); + gitlabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner); + } + String fullName = gitlabOwner.getFullName(); + String webUrl = gitlabOwner.getWebUrl(); + String avatarUrl = gitlabOwner.getAvatarUrl(); + String description = null; + if (gitlabOwner instanceof GitLabGroup) { + description = ((GitLabGroup) gitlabOwner).getDescription(); + } + List result = new ArrayList<>(); + result.add(new ObjectMetadataAction( + Util.fixEmpty(fullName), + description, + webUrl) + ); + if (StringUtils.isNotBlank(avatarUrl)) { + result.add(new GitLabAvatar(avatarUrl)); + } + result.add(GitLabLink.toGroup(webUrl)); + if (StringUtils.isBlank(webUrl)) { + listener.getLogger().println("Web URL unspecified"); + } else { + listener.getLogger().printf("%s URL: %s%n", gitlabOwner.getWord(), + HyperlinkNote + .encodeTo(webUrl, StringUtils.defaultIfBlank(fullName, webUrl))); } + return result; } @Override @@ -400,12 +400,9 @@ public static FormValidation doCheckProjectOwner(@QueryParameter String projectO gitLabApi = apiBuilder(serverName); GitLabOwner gitLabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner); return FormValidation.ok(projectOwner + " is a valid " + gitLabOwner.getWord()); - } catch (NoSuchFieldException e) { - e.printStackTrace(); } catch (IllegalStateException e) { return FormValidation.error(e, e.getMessage()); } - return FormValidation.ok(""); } @NonNull diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java index d475ed76..6a78d68b 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java @@ -18,6 +18,7 @@ import hudson.scm.SCM; import hudson.security.ACL; import hudson.util.ListBoxModel; +import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink; import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer; @@ -69,6 +70,7 @@ import jenkins.scm.impl.form.NamedArrayList; import jenkins.scm.impl.trait.Discovery; import jenkins.scm.impl.trait.Selection; +import org.apache.commons.lang.StringUtils; import org.gitlab4j.api.Constants; import org.gitlab4j.api.GitLabApi; import org.gitlab4j.api.GitLabApiException; @@ -95,7 +97,6 @@ import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.commitUriTemplate; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.getServerUrlFromName; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.mergeRequestUriTemplate; -import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.projectUriTemplate; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.splitPath; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.tagUriTemplate; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabIcons.ICON_GITLAB; @@ -186,7 +187,7 @@ public HashMap getMembers() { for (Member m : gitLabApi.getProjectApi().getAllMembers(projectPath)) { members.put(m.getUsername(), m.getAccessLevel()); } - } catch (GitLabApiException | NoSuchFieldException e) { + } catch (GitLabApiException e) { e.printStackTrace(); return new HashMap<>(); } @@ -267,7 +268,7 @@ protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener list head.getClass().getName()); return null; } - } catch (GitLabApiException | NoSuchFieldException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } return super.retrieve(head, listener); @@ -507,7 +508,7 @@ public SCMSourceCriteria.Probe create( .format("%n%d tags were processed (query completed)%n", count); } } - } catch (GitLabApiException | NoSuchFieldException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } } @@ -535,22 +536,24 @@ protected Set retrieveRevisions(@NonNull TaskListener listener) @NonNull @Override protected List retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener) { - LOGGER.info(String.format("e, l..%s", Thread.currentThread().getName())); List result = new ArrayList<>(); if (gitlabProject == null) { try { GitLabApi gitLabApi = apiBuilder(serverName); listener.getLogger().format("Looking up project %s%n", projectPath); gitlabProject = gitLabApi.getProjectApi().getProject(projectPath); - result.add(new ObjectMetadataAction(null, gitlabProject.getDescription(), - gitlabProject.getWebUrl())); - } catch (GitLabApiException | NoSuchFieldException e) { - e.printStackTrace(); + } catch (GitLabApiException e) { + throw new IllegalStateException("Failed to retrieve project", e); } } - String projectUrl = projectUriTemplate(serverName) - .set("project", splitPath(projectPath)) - .expand(); + String projectUrl = gitlabProject.getWebUrl(); + result.add(new ObjectMetadataAction(gitlabProject.getNameWithNamespace(), + gitlabProject.getDescription(), + projectUrl)); + String avatarUrl = gitlabProject.getAvatarUrl(); + if (StringUtils.isNotBlank(avatarUrl)) { + result.add(new GitLabAvatar(avatarUrl)); + } result.add(GitLabLink.toProject(projectUrl)); return result; } @@ -565,7 +568,7 @@ protected List retrieveActions(@NonNull SCMHead head, SCMHeadEvent event GitLabApi gitLabApi = apiBuilder(serverName); listener.getLogger().format("Looking up project %s%n", projectPath); gitlabProject = gitLabApi.getProjectApi().getProject(projectPath); - } catch (GitLabApiException | NoSuchFieldException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } } @@ -724,7 +727,7 @@ public SCMFile getRoot() { return fs != null ? fs.getRoot() : null; } }; - } catch (InterruptedException | NoSuchFieldException | GitLabApiException e) { + } catch (InterruptedException | GitLabApiException e) { throw new IOException(e); } } @@ -850,7 +853,7 @@ public ListBoxModel doFillProjectPathItems(@AncestorInPath SCMSourceOwner contex } } return result; - } catch (GitLabApiException | NoSuchFieldException e) { + } catch (GitLabApiException e) { e.printStackTrace(); return new StandardListBoxModel() .includeEmptyValue(); diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java index 6062ca4f..dc6756cb 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java @@ -11,7 +11,7 @@ public class GitLabHelper { - public static GitLabApi apiBuilder(String serverName) throws NoSuchFieldException { + public static GitLabApi apiBuilder(String serverName) { GitLabServer server = GitLabServers.get().findServer(serverName); if (server != null) { PersonalAccessToken credentials = server.getCredentials(); @@ -20,7 +20,7 @@ public static GitLabApi apiBuilder(String serverName) throws NoSuchFieldExceptio } return new GitLabApi(server.getServerUrl(), GitLabServer.EMPTY_TOKEN); } - throw new NoSuchFieldException( + throw new IllegalStateException( String.format("No server found with the name: %s", serverName)); } diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java index b202317b..8f50f3ac 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java @@ -157,7 +157,7 @@ private static void logComment(Run build, TaskListener listener) { symbol + buildName + note + suffix ); } - } catch (NoSuchFieldException | GitLabApiException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } } @@ -253,7 +253,7 @@ private static void sendNotifications(Run build, TaskListener listener) { state, status); listener.getLogger().format("[GitLab Pipeline Status] Notified%n"); - } catch (NoSuchFieldException | GitLabApiException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } } @@ -352,7 +352,7 @@ public void run() { state, status); LOGGER.log(Level.INFO, "{0} Notified", job.getFullName()); - } catch (NoSuchFieldException | GitLabApiException e) { + } catch (GitLabApiException e) { e.printStackTrace(); } } catch (IOException | InterruptedException e) {