Skip to content

Commit

Permalink
add gitlab project avatar & display name. add gitlab group descri… (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen authored Aug 30, 2019
1 parent 224bdba commit 7aec8d7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -330,36 +331,35 @@ protected List<Action> 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<Action> 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<Action> 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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -186,7 +187,7 @@ public HashMap<String, AccessLevel> 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<>();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -535,22 +536,24 @@ protected Set<String> retrieveRevisions(@NonNull TaskListener listener)
@NonNull
@Override
protected List<Action> retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener) {
LOGGER.info(String.format("e, l..%s", Thread.currentThread().getName()));
List<Action> 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;
}
Expand All @@ -565,7 +568,7 @@ protected List<Action> 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();
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7aec8d7

Please sign in to comment.