From b3fc76a3e4c7c7ddd363ecbbc326ffa44da8c83e Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Mon, 27 Mar 2023 16:58:07 +0200 Subject: [PATCH] chore: Improve GitLab/GitHub contributor output --- .../cmd/GitHubContributorListCommand.java | 19 +++++-------------- .../github/helper/GitHubRepoDescriptor.java | 15 +++++++++------ ...bstractGitLabBaseRequestOutputCommand.java | 2 +- .../AbstractGitLabJsonNodeOutputCommand.java | 2 +- ....java => AbstractGitLabOutputCommand.java} | 2 +- .../cmd/GitLabContributorListCommand.java | 16 ++++------------ .../helper/GitLabProjectDescriptor.java | 15 +++++++++++++-- .../cli/scm/i18n/ScmMessages.properties | 7 +++++-- 8 files changed, 39 insertions(+), 39 deletions(-) rename fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/{AbstractLabHubOutputCommand.java => AbstractGitLabOutputCommand.java} (86%) diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/github/contributor/cmd/GitHubContributorListCommand.java b/fcli-scm/src/main/java/com/fortify/cli/scm/github/contributor/cmd/GitHubContributorListCommand.java index 4082d3926a..5ecafc4a03 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/github/contributor/cmd/GitHubContributorListCommand.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/github/contributor/cmd/GitHubContributorListCommand.java @@ -47,12 +47,10 @@ import com.fortify.cli.scm.github.cli.util.GitHubPagingHelper; import com.fortify.cli.scm.github.helper.GitHubRepoDescriptor; -import io.micronaut.core.annotation.ReflectiveAccess; import kong.unirest.GetRequest; import kong.unirest.HttpRequest; import kong.unirest.UnirestInstance; import lombok.Data; -import lombok.EqualsAndHashCode; import lombok.Getter; import picocli.CommandLine.Command; import picocli.CommandLine.Mixin; @@ -82,7 +80,7 @@ private static final class GitHubContributorProcessor extends AbstractGitHubRepo @Override protected void processRepo(UnirestInstance unirest, ProgressHelperMixin progressHelper, JsonNode repoNode) { - ExtendedGitHubRepoDescriptor repoDescriptor = JsonHelper.treeToValue(repoNode, ExtendedGitHubRepoDescriptor.class); + GitHubRepoDescriptor repoDescriptor = JsonHelper.treeToValue(repoNode, GitHubRepoDescriptor.class); progressHelper.writeI18nProgress("loading.repository", repoDescriptor.getFullName()); String since = PERIOD_HELPER.getCurrentOffsetDateTimeMinusPeriod(lastPeriod) .format(DateTimeFormatter.ISO_INSTANT); @@ -104,18 +102,18 @@ protected void processRepo(UnirestInstance unirest, ProgressHelperMixin progress } private GetRequest getCommitsRequest(UnirestInstance unirest, GitHubRepoDescriptor descriptor) { - return unirest.get("/repos/{org}/{repo}/commits") - .routeParam("org", descriptor.getOwnerName()) + return unirest.get("/repos/{owner}/{repo}/commits") + .routeParam("owner", descriptor.getOwnerName()) .routeParam("repo", descriptor.getRepoName()); } - private void handleRepoDataFailure(UnexpectedHttpResponseException e, ResultData resultData, ExtendedGitHubRepoDescriptor repoDescriptor) { + private void handleRepoDataFailure(UnexpectedHttpResponseException e, ResultData resultData, GitHubRepoDescriptor repoDescriptor) { String msg = "Error loading commit data for repository: "+repoDescriptor.getFullName(); resultData.getWarnings().add(msg); LOG.debug(msg, e); } - private void collectDataForCommit(ResultData resultData, CollectedAuthors collectedAuthors, ExtendedGitHubRepoDescriptor repoDescriptor, JsonNode commit) { + private void collectDataForCommit(ResultData resultData, CollectedAuthors collectedAuthors, GitHubRepoDescriptor repoDescriptor, JsonNode commit) { ObjectNode author = getAuthor(commit); if ( !collectedAuthors.contains(author) ) { collectedAuthors.add(author); @@ -185,11 +183,4 @@ private static final class ResultData { private ArrayNode results = JsonHelper.getObjectMapper().createArrayNode(); private List warnings = new ArrayList<>(); } - - @ReflectiveAccess - @Data @EqualsAndHashCode(callSuper = true) - private static final class ExtendedGitHubRepoDescriptor extends GitHubRepoDescriptor { - private int size; - private String html_url; - } } diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/github/helper/GitHubRepoDescriptor.java b/fcli-scm/src/main/java/com/fortify/cli/scm/github/helper/GitHubRepoDescriptor.java index f4b7bf1937..6bb28a7591 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/github/helper/GitHubRepoDescriptor.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/github/helper/GitHubRepoDescriptor.java @@ -1,9 +1,10 @@ package com.fortify.cli.scm.github.helper; -import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.fortify.cli.common.json.JsonNodeHolder; import io.micronaut.core.annotation.ReflectiveAccess; @@ -14,19 +15,21 @@ @ReflectiveAccess @Data @EqualsAndHashCode(callSuper=true) -@JsonPropertyOrder({"owner_name","repo_name","full_name","visibility","fork"}) public class GitHubRepoDescriptor extends JsonNodeHolder { - @JsonProperty("owner_name") + private static final Logger LOG = LoggerFactory.getLogger(GitHubRepoDescriptor.class); + @Getter(onMethod_=@JsonProperty("owner_name")) private String ownerName; @Setter(onMethod_=@JsonProperty("name")) @Getter(onMethod_=@JsonProperty("repo_name")) private String repoName; @JsonProperty("full_name") private String fullName; + private String html_url; private String visibility; private boolean fork; @JsonProperty("owner") - private void unpackOwner(Map owner) { - ownerName = owner.get("login"); + private void setOwner(ObjectNode owner) { + LOG.debug("Unpacking owner: "+owner); + this.ownerName = owner.get("login").asText(); } } diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabBaseRequestOutputCommand.java b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabBaseRequestOutputCommand.java index 403aba62f9..5de831f09f 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabBaseRequestOutputCommand.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabBaseRequestOutputCommand.java @@ -5,7 +5,7 @@ import kong.unirest.HttpRequest; import kong.unirest.UnirestInstance; -public abstract class AbstractGitLabBaseRequestOutputCommand extends AbstractLabHubOutputCommand implements IBaseRequestSupplier { +public abstract class AbstractGitLabBaseRequestOutputCommand extends AbstractGitLabOutputCommand implements IBaseRequestSupplier { @Override public final HttpRequest getBaseRequest() { try ( var unirest = getProductHelper().createUnirestInstance() ) { diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabJsonNodeOutputCommand.java b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabJsonNodeOutputCommand.java index 4fb856c86e..f7bd2987af 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabJsonNodeOutputCommand.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabJsonNodeOutputCommand.java @@ -5,7 +5,7 @@ import kong.unirest.UnirestInstance; -public abstract class AbstractGitLabJsonNodeOutputCommand extends AbstractLabHubOutputCommand implements IJsonNodeSupplier { +public abstract class AbstractGitLabJsonNodeOutputCommand extends AbstractGitLabOutputCommand implements IJsonNodeSupplier { @Override public final JsonNode getJsonNode() { try ( var unirest = getProductHelper().createUnirestInstance() ) { diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractLabHubOutputCommand.java b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabOutputCommand.java similarity index 86% rename from fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractLabHubOutputCommand.java rename to fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabOutputCommand.java index 49fd7ff629..12b31dd3dd 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractLabHubOutputCommand.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/cli/cmd/AbstractGitLabOutputCommand.java @@ -7,7 +7,7 @@ import lombok.Getter; import picocli.CommandLine.Mixin; -public abstract class AbstractLabHubOutputCommand extends AbstractOutputCommand +public abstract class AbstractGitLabOutputCommand extends AbstractOutputCommand implements IProductHelperSupplier { @Getter @Mixin GitLabProductHelperMixin productHelper; diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/contributor/cmd/GitLabContributorListCommand.java b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/contributor/cmd/GitLabContributorListCommand.java index 909158cadd..ee125e1f43 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/contributor/cmd/GitLabContributorListCommand.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/contributor/cmd/GitLabContributorListCommand.java @@ -47,12 +47,10 @@ import com.fortify.cli.scm.gitlab.cli.util.GitLabPagingHelper; import com.fortify.cli.scm.gitlab.helper.GitLabProjectDescriptor; -import io.micronaut.core.annotation.ReflectiveAccess; import kong.unirest.GetRequest; import kong.unirest.HttpRequest; import kong.unirest.UnirestInstance; import lombok.Data; -import lombok.EqualsAndHashCode; import lombok.Getter; import picocli.CommandLine.Command; import picocli.CommandLine.Mixin; @@ -82,7 +80,7 @@ private static final class GitLabContributorProcessor extends AbstractGitLabProj @Override protected void processProject(UnirestInstance unirest, ProgressHelperMixin progressHelper, JsonNode projectNode) { - ExtendedGitLabProjectDescriptor projectDescriptor = JsonHelper.treeToValue(projectNode, ExtendedGitLabProjectDescriptor.class); + GitLabProjectDescriptor projectDescriptor = JsonHelper.treeToValue(projectNode, GitLabProjectDescriptor.class); progressHelper.writeI18nProgress("loading.project", projectDescriptor.getProjectFullPath()); String since = PERIOD_HELPER.getCurrentOffsetDateTimeMinusPeriod(lastPeriod) .format(DateTimeFormatter.ISO_INSTANT); @@ -103,18 +101,18 @@ protected void processProject(UnirestInstance unirest, ProgressHelperMixin progr } } - private GetRequest getCommitsRequest(UnirestInstance unirest, ExtendedGitLabProjectDescriptor descriptor) { + private GetRequest getCommitsRequest(UnirestInstance unirest, GitLabProjectDescriptor descriptor) { return unirest.get("/api/v4/projects/{id}/repository/commits") .routeParam("id", descriptor.getProjectId()); } - private void handleRepoDataFailure(UnexpectedHttpResponseException e, ResultData resultData, ExtendedGitLabProjectDescriptor repoDescriptor) { + private void handleRepoDataFailure(UnexpectedHttpResponseException e, ResultData resultData, GitLabProjectDescriptor repoDescriptor) { String msg = "Error loading commit data for project: "+repoDescriptor.getProjectFullPath(); resultData.getWarnings().add(msg); LOG.debug(msg, e); } - private void collectDataForCommit(ResultData resultData, CollectedAuthors collectedAuthors, ExtendedGitLabProjectDescriptor projectDescriptor, JsonNode commit) { + private void collectDataForCommit(ResultData resultData, CollectedAuthors collectedAuthors, GitLabProjectDescriptor projectDescriptor, JsonNode commit) { ObjectNode author = getAuthor(commit); if ( !collectedAuthors.contains(author) ) { collectedAuthors.add(author); @@ -180,10 +178,4 @@ private static final class ResultData { private ArrayNode results = JsonHelper.getObjectMapper().createArrayNode(); private List warnings = new ArrayList<>(); } - - @ReflectiveAccess - @Data @EqualsAndHashCode(callSuper = true) - private static final class ExtendedGitLabProjectDescriptor extends GitLabProjectDescriptor { - private String web_url; - } } diff --git a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/helper/GitLabProjectDescriptor.java b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/helper/GitLabProjectDescriptor.java index bc08b16cfb..4c8afd8aca 100644 --- a/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/helper/GitLabProjectDescriptor.java +++ b/fcli-scm/src/main/java/com/fortify/cli/scm/gitlab/helper/GitLabProjectDescriptor.java @@ -1,7 +1,10 @@ package com.fortify.cli.scm.gitlab.helper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.databind.JsonNode; import com.fortify.cli.common.json.JsonNodeHolder; import io.micronaut.core.annotation.ReflectiveAccess; @@ -12,13 +15,21 @@ @ReflectiveAccess @Data @EqualsAndHashCode(callSuper=true) -@JsonPropertyOrder({"project_id","full_path","project_name", "visibility"}) public class GitLabProjectDescriptor extends JsonNodeHolder { + private static final Logger LOG = LoggerFactory.getLogger(GitLabProjectDescriptor.class); @Setter(onMethod_={@JsonProperty("id")}) @Getter(onMethod_={@JsonProperty("project_id")}) private String projectId; @Setter(onMethod_={@JsonProperty("name")}) @Getter(onMethod_={@JsonProperty("project_name")}) private String projectName; @Setter(onMethod_={@JsonProperty("path_with_namespace")}) @Getter(onMethod_={@JsonProperty("project_full_path")}) private String projectFullPath; + private String web_url; private String visibility; + private boolean fork = false; + + @JsonProperty("forked_from_project") + private void unpackForkedFromProject(JsonNode fork) { + LOG.debug("Unpacking fork: "+fork); + this.fork = true; + } } diff --git a/fcli-scm/src/main/resources/com/fortify/cli/scm/i18n/ScmMessages.properties b/fcli-scm/src/main/resources/com/fortify/cli/scm/i18n/ScmMessages.properties index 8f8fc9ce5d..0844687da6 100644 --- a/fcli-scm/src/main/resources/com/fortify/cli/scm/i18n/ScmMessages.properties +++ b/fcli-scm/src/main/resources/com/fortify/cli/scm/i18n/ScmMessages.properties @@ -19,6 +19,8 @@ fcli.scm.github-contributor.list.search.1 = --search 'org:myOrg topic:scanned-by fcli.scm.github-contributor.list.loading.organizations = Loading organizations fcli.scm.github-contributor.list.loading.repositories = Loading repositories for organization %s fcli.scm.github-contributor.list.loading.repository = Loading data for repository %s +fcli.scm.github-contributor.list.output.header.htmlUrl = Repository + # GitLab fcli.scm.gitlab.gitlab-url = GitLab URL, i.e. https://gitlab.com or the URL to your private GitLab instance. @@ -39,11 +41,12 @@ fcli.scm.gitlab-contributor.list.topic = Load data for the repositories matching fcli.scm.gitlab-contributor.list.loading.groups = Loading groups fcli.scm.gitlab-contributor.list.loading.projects = Loading projects for group %s fcli.scm.gitlab-contributor.list.loading.project = Loading data for project %s +fcli.scm.gitlab-contributor.list.output.header.webUrl = Repository ################################################################################################################# # The following are technical properties that shouldn't be internationalized #################################### ################################################################################################################# # Table output columns configuration -fcli.scm.github-contributor.output.table.options = full_name,visibility,fork,author.name,lastCommit -fcli.scm.gitlab-contributor.output.table.options = web_url,visibility,author.name,lastCommit +fcli.scm.github-contributor.output.table.options = html_url,visibility,fork,author.name,lastCommit +fcli.scm.gitlab-contributor.output.table.options = web_url,visibility,fork,author.name,lastCommit