Skip to content

Commit 7aec8d7

Browse files
authored
add gitlab project avatar & display name. add gitlab group descri… (#40)
1 parent 224bdba commit 7aec8d7

File tree

4 files changed

+52
-52
lines changed

4 files changed

+52
-52
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import hudson.util.FormValidation;
1919
import hudson.util.ListBoxModel;
2020
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar;
21+
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabGroup;
2122
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink;
2223
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabOwner;
2324
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabUser;
@@ -284,7 +285,7 @@ public void visitSources(@NonNull final SCMSourceObserver observer)
284285
}
285286
}
286287
observer.getListener().getLogger().format("%n%d projects were processed%n", count);
287-
} catch (GitLabApiException | NoSuchFieldException e) {
288+
} catch (GitLabApiException e) {
288289
e.printStackTrace();
289290
}
290291
}
@@ -330,36 +331,35 @@ protected List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
330331
SCMNavigatorEvent event,
331332
@NonNull TaskListener listener) throws IOException, InterruptedException {
332333
LOGGER.info("retrieving actions..");
333-
try {
334+
if (gitlabOwner == null) {
334335
GitLabApi gitLabApi = apiBuilder(serverName);
335-
if (gitlabOwner == null) {
336-
gitlabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner);
337-
}
338-
String fullName = gitlabOwner.getFullName();
339-
String webUrl = gitlabOwner.getWebUrl();
340-
String avatarUrl = gitlabOwner.getAvatarUrl();
341-
List<Action> result = new ArrayList<>();
342-
result.add(new ObjectMetadataAction(
343-
Util.fixEmpty(fullName),
344-
null,
345-
webUrl)
346-
);
347-
if (StringUtils.isNotBlank(avatarUrl)) {
348-
result.add(new GitLabAvatar(avatarUrl));
349-
}
350-
result.add(GitLabLink.toGroup(webUrl));
351-
if (StringUtils.isBlank(webUrl)) {
352-
listener.getLogger().println("Web URL unspecified");
353-
} else {
354-
listener.getLogger().printf("%s URL: %s%n", gitlabOwner.getWord(),
355-
HyperlinkNote
356-
.encodeTo(webUrl, StringUtils.defaultIfBlank(fullName, webUrl)));
357-
}
358-
return result;
359-
} catch (NoSuchFieldException e) {
360-
e.printStackTrace();
361-
throw new IOException("Server Not found");
336+
gitlabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner);
337+
}
338+
String fullName = gitlabOwner.getFullName();
339+
String webUrl = gitlabOwner.getWebUrl();
340+
String avatarUrl = gitlabOwner.getAvatarUrl();
341+
String description = null;
342+
if (gitlabOwner instanceof GitLabGroup) {
343+
description = ((GitLabGroup) gitlabOwner).getDescription();
344+
}
345+
List<Action> result = new ArrayList<>();
346+
result.add(new ObjectMetadataAction(
347+
Util.fixEmpty(fullName),
348+
description,
349+
webUrl)
350+
);
351+
if (StringUtils.isNotBlank(avatarUrl)) {
352+
result.add(new GitLabAvatar(avatarUrl));
353+
}
354+
result.add(GitLabLink.toGroup(webUrl));
355+
if (StringUtils.isBlank(webUrl)) {
356+
listener.getLogger().println("Web URL unspecified");
357+
} else {
358+
listener.getLogger().printf("%s URL: %s%n", gitlabOwner.getWord(),
359+
HyperlinkNote
360+
.encodeTo(webUrl, StringUtils.defaultIfBlank(fullName, webUrl)));
362361
}
362+
return result;
363363
}
364364

365365
@Override
@@ -400,12 +400,9 @@ public static FormValidation doCheckProjectOwner(@QueryParameter String projectO
400400
gitLabApi = apiBuilder(serverName);
401401
GitLabOwner gitLabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner);
402402
return FormValidation.ok(projectOwner + " is a valid " + gitLabOwner.getWord());
403-
} catch (NoSuchFieldException e) {
404-
e.printStackTrace();
405403
} catch (IllegalStateException e) {
406404
return FormValidation.error(e, e.getMessage());
407405
}
408-
return FormValidation.ok("");
409406
}
410407

411408
@NonNull

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import hudson.scm.SCM;
1919
import hudson.security.ACL;
2020
import hudson.util.ListBoxModel;
21+
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar;
2122
import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink;
2223
import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken;
2324
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
@@ -69,6 +70,7 @@
6970
import jenkins.scm.impl.form.NamedArrayList;
7071
import jenkins.scm.impl.trait.Discovery;
7172
import jenkins.scm.impl.trait.Selection;
73+
import org.apache.commons.lang.StringUtils;
7274
import org.gitlab4j.api.Constants;
7375
import org.gitlab4j.api.GitLabApi;
7476
import org.gitlab4j.api.GitLabApiException;
@@ -95,7 +97,6 @@
9597
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.commitUriTemplate;
9698
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.getServerUrlFromName;
9799
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.mergeRequestUriTemplate;
98-
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.projectUriTemplate;
99100
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.splitPath;
100101
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.tagUriTemplate;
101102
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabIcons.ICON_GITLAB;
@@ -186,7 +187,7 @@ public HashMap<String, AccessLevel> getMembers() {
186187
for (Member m : gitLabApi.getProjectApi().getAllMembers(projectPath)) {
187188
members.put(m.getUsername(), m.getAccessLevel());
188189
}
189-
} catch (GitLabApiException | NoSuchFieldException e) {
190+
} catch (GitLabApiException e) {
190191
e.printStackTrace();
191192
return new HashMap<>();
192193
}
@@ -267,7 +268,7 @@ protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener list
267268
head.getClass().getName());
268269
return null;
269270
}
270-
} catch (GitLabApiException | NoSuchFieldException e) {
271+
} catch (GitLabApiException e) {
271272
e.printStackTrace();
272273
}
273274
return super.retrieve(head, listener);
@@ -507,7 +508,7 @@ public SCMSourceCriteria.Probe create(
507508
.format("%n%d tags were processed (query completed)%n", count);
508509
}
509510
}
510-
} catch (GitLabApiException | NoSuchFieldException e) {
511+
} catch (GitLabApiException e) {
511512
e.printStackTrace();
512513
}
513514
}
@@ -535,22 +536,24 @@ protected Set<String> retrieveRevisions(@NonNull TaskListener listener)
535536
@NonNull
536537
@Override
537538
protected List<Action> retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener) {
538-
LOGGER.info(String.format("e, l..%s", Thread.currentThread().getName()));
539539
List<Action> result = new ArrayList<>();
540540
if (gitlabProject == null) {
541541
try {
542542
GitLabApi gitLabApi = apiBuilder(serverName);
543543
listener.getLogger().format("Looking up project %s%n", projectPath);
544544
gitlabProject = gitLabApi.getProjectApi().getProject(projectPath);
545-
result.add(new ObjectMetadataAction(null, gitlabProject.getDescription(),
546-
gitlabProject.getWebUrl()));
547-
} catch (GitLabApiException | NoSuchFieldException e) {
548-
e.printStackTrace();
545+
} catch (GitLabApiException e) {
546+
throw new IllegalStateException("Failed to retrieve project", e);
549547
}
550548
}
551-
String projectUrl = projectUriTemplate(serverName)
552-
.set("project", splitPath(projectPath))
553-
.expand();
549+
String projectUrl = gitlabProject.getWebUrl();
550+
result.add(new ObjectMetadataAction(gitlabProject.getNameWithNamespace(),
551+
gitlabProject.getDescription(),
552+
projectUrl));
553+
String avatarUrl = gitlabProject.getAvatarUrl();
554+
if (StringUtils.isNotBlank(avatarUrl)) {
555+
result.add(new GitLabAvatar(avatarUrl));
556+
}
554557
result.add(GitLabLink.toProject(projectUrl));
555558
return result;
556559
}
@@ -565,7 +568,7 @@ protected List<Action> retrieveActions(@NonNull SCMHead head, SCMHeadEvent event
565568
GitLabApi gitLabApi = apiBuilder(serverName);
566569
listener.getLogger().format("Looking up project %s%n", projectPath);
567570
gitlabProject = gitLabApi.getProjectApi().getProject(projectPath);
568-
} catch (GitLabApiException | NoSuchFieldException e) {
571+
} catch (GitLabApiException e) {
569572
e.printStackTrace();
570573
}
571574
}
@@ -724,7 +727,7 @@ public SCMFile getRoot() {
724727
return fs != null ? fs.getRoot() : null;
725728
}
726729
};
727-
} catch (InterruptedException | NoSuchFieldException | GitLabApiException e) {
730+
} catch (InterruptedException | GitLabApiException e) {
728731
throw new IOException(e);
729732
}
730733
}
@@ -850,7 +853,7 @@ public ListBoxModel doFillProjectPathItems(@AncestorInPath SCMSourceOwner contex
850853
}
851854
}
852855
return result;
853-
} catch (GitLabApiException | NoSuchFieldException e) {
856+
} catch (GitLabApiException e) {
854857
e.printStackTrace();
855858
return new StandardListBoxModel()
856859
.includeEmptyValue();

src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class GitLabHelper {
1313

14-
public static GitLabApi apiBuilder(String serverName) throws NoSuchFieldException {
14+
public static GitLabApi apiBuilder(String serverName) {
1515
GitLabServer server = GitLabServers.get().findServer(serverName);
1616
if (server != null) {
1717
PersonalAccessToken credentials = server.getCredentials();
@@ -20,7 +20,7 @@ public static GitLabApi apiBuilder(String serverName) throws NoSuchFieldExceptio
2020
}
2121
return new GitLabApi(server.getServerUrl(), GitLabServer.EMPTY_TOKEN);
2222
}
23-
throw new NoSuchFieldException(
23+
throw new IllegalStateException(
2424
String.format("No server found with the name: %s", serverName));
2525
}
2626

src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private static void logComment(Run<?, ?> build, TaskListener listener) {
157157
symbol + buildName + note + suffix
158158
);
159159
}
160-
} catch (NoSuchFieldException | GitLabApiException e) {
160+
} catch (GitLabApiException e) {
161161
e.printStackTrace();
162162
}
163163
}
@@ -253,7 +253,7 @@ private static void sendNotifications(Run<?, ?> build, TaskListener listener) {
253253
state,
254254
status);
255255
listener.getLogger().format("[GitLab Pipeline Status] Notified%n");
256-
} catch (NoSuchFieldException | GitLabApiException e) {
256+
} catch (GitLabApiException e) {
257257
e.printStackTrace();
258258
}
259259
}
@@ -352,7 +352,7 @@ public void run() {
352352
state,
353353
status);
354354
LOGGER.log(Level.INFO, "{0} Notified", job.getFullName());
355-
} catch (NoSuchFieldException | GitLabApiException e) {
355+
} catch (GitLabApiException e) {
356356
e.printStackTrace();
357357
}
358358
} catch (IOException | InterruptedException e) {

0 commit comments

Comments
 (0)