From 5488b7b6b0e91295986f4cacf01865451c99b82e Mon Sep 17 00:00:00 2001 From: Tal Arian Date: Mon, 20 Dec 2021 18:31:17 +0200 Subject: [PATCH 1/3] Add vcs.branch and vcs commit message to VCS build-info details --- .../clientConfiguration/util/GitUtils.java | 36 +++++++++++++++++++ .../util/GitUtilsTest.java | 20 +++++++++++ build.gradle | 1 + 3 files changed, 57 insertions(+) diff --git a/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java b/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java index 405c87a5f..f44a59394 100644 --- a/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java +++ b/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java @@ -1,6 +1,11 @@ package org.jfrog.build.extractor.clientConfiguration.util; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.jfrog.build.extractor.ci.Vcs; import org.jfrog.build.api.util.Log; @@ -43,6 +48,8 @@ public static Vcs extractVcs(File workingDir, Log log) throws IOException { Vcs vcs = new Vcs(); vcs.setRevision(extractVcsRevision(dotGit, log)); vcs.setUrl(extractVcsUrl(dotGit, log)); + vcs.setMessage(extractVcsMessage(dotGit, log)); + vcs.setBranch(extractVcsBranch(dotGit, log)); return vcs; } @@ -173,6 +180,35 @@ private static RevisionOrRef getRevisionOrBranchPath(File dotGit) throws IOExcep return result; } + private static String extractVcsBranch(File dotGit, Log log) throws IOException { + RevisionOrRef revisionOrRef = getRevisionOrBranchPath(dotGit); + // If found ref, returns the name after the last "/" as the git branch name. + if (StringUtils.isNotBlank(revisionOrRef.ref)) { + String[] splitArr = revisionOrRef.ref.split("/"); + return splitArr[splitArr.length - 1]; + } + log.warn("Failed fetching git branch from git directory: " + dotGit); + return ""; + } + + private static String extractVcsMessage(File dotGit, Log log) throws IOException { + FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder(); + Repository repository = repositoryBuilder.setGitDir(dotGit) + .readEnvironment() + .findGitDir() + .setMustExist(true) + .build(); + RevCommit latestCommit; + try { + latestCommit = new Git(repository).log().setMaxCount(1).call().iterator().next(); + } catch (GitAPIException e) { + log.warn("Failed fetching commit message from git directory: " + dotGit); + return ""; + } + + return latestCommit.getFullMessage(); + } + /** * This class is used in the process of extracting Vcs revision from the .git configuration */ diff --git a/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java b/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java index 81fb4e783..413dd5a94 100644 --- a/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java +++ b/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java @@ -29,6 +29,9 @@ private void testReadGitConfig() throws IOException, InterruptedException { Assert.assertNotNull(vcs); Assert.assertEquals(vcs.getUrl(), getGitUrlWithExecutor(curDir, testLog)); Assert.assertEquals(vcs.getRevision(), getGitRevisionWithExecutor(curDir, testLog)); + Assert.assertEquals(vcs.getBranch(), getGitBranchWithExecutor(curDir, testLog)); + Assert.assertEquals(vcs.getMessage(), getGitMessageWithExecutor(curDir, testLog)); + } private String getGitFieldWithExecutor(File execDir, Log log, List args) throws IOException, InterruptedException { @@ -55,6 +58,23 @@ private String getGitRevisionWithExecutor(File execDir, Log log) throws IOExcept return getGitFieldWithExecutor(execDir, log, args); } + private String getGitBranchWithExecutor(File execDir, Log log) throws IOException, InterruptedException { + List args = new ArrayList<>(); + args.add("rev-parse"); + args.add("--abbrev-ref"); + args.add("HEAD"); + return getGitFieldWithExecutor(execDir, log, args); + } + + private String getGitMessageWithExecutor(File execDir, Log log) throws IOException, InterruptedException { + // git log -1 --pretty=%B + List args = new ArrayList<>(); + args.add("log"); + args.add("-1"); + args.add("--pretty=%B"); + return getGitFieldWithExecutor(execDir, log, args); + } + @Test private void testExtractVcsWithSubmodule() throws IOException, URISyntaxException { String parentDotGitResource = "git_submodule_parent_.git_suffix"; diff --git a/build.gradle b/build.gradle index 757d70574..a0f8116fd 100644 --- a/build.gradle +++ b/build.gradle @@ -266,6 +266,7 @@ project('build-info-extractor') { apply plugin: 'java-test-fixtures' description = 'JFrog Build-Info Extractor' dependencies { + implementation 'org.eclipse.jgit:org.eclipse.jgit:5.1.16.202106041830-r' implementation project(':build-info-client') implementation project(':build-info-api') From e269965287103d6dba357757794bfc9a9017504a Mon Sep 17 00:00:00 2001 From: Tal Arian Date: Mon, 27 Dec 2021 15:41:35 +0200 Subject: [PATCH 2/3] Add support for vcs branch and vcs message --- .../extractor/GradleBuildInfoExtractor.java | 4 +- .../listener/ArtifactoryBuildListener.java | 4 +- .../maven/BuildInfoModelPropertyResolver.java | 6 +-- .../build/extractor/ci/BuildInfoFields.java | 2 + .../ArtifactoryClientConfiguration.java | 49 +++++++------------ 5 files changed, 23 insertions(+), 42 deletions(-) diff --git a/build-info-extractor-gradle/src/main/groovy/org/jfrog/gradle/plugin/artifactory/extractor/GradleBuildInfoExtractor.java b/build-info-extractor-gradle/src/main/groovy/org/jfrog/gradle/plugin/artifactory/extractor/GradleBuildInfoExtractor.java index aa9318f3c..547f367fd 100644 --- a/build-info-extractor-gradle/src/main/groovy/org/jfrog/gradle/plugin/artifactory/extractor/GradleBuildInfoExtractor.java +++ b/build-info-extractor-gradle/src/main/groovy/org/jfrog/gradle/plugin/artifactory/extractor/GradleBuildInfoExtractor.java @@ -131,17 +131,15 @@ public BuildInfo extract(Project rootProject) { bib.url(buildUrl); } String vcsRevision = clientConf.info.getVcsRevision(); - Vcs vcs = new Vcs(); if (StringUtils.isNotBlank(vcsRevision)) { - vcs.setRevision(vcsRevision); bib.vcsRevision(vcsRevision); } String vcsUrl = clientConf.info.getVcsUrl(); if (StringUtils.isNotBlank(vcsUrl)) { - vcs.setUrl(vcsUrl); bib.vcsUrl(vcsUrl); } + Vcs vcs = new Vcs(vcsUrl, vcsRevision, clientConf.info.getVcsBranch(), clientConf.info.getVcsMessage()); if (!vcs.isEmpty()) { bib.vcs(Arrays.asList(vcs)); } diff --git a/build-info-extractor-ivy/src/main/java/org/jfrog/build/extractor/listener/ArtifactoryBuildListener.java b/build-info-extractor-ivy/src/main/java/org/jfrog/build/extractor/listener/ArtifactoryBuildListener.java index c343a7a88..dc3835cb3 100644 --- a/build-info-extractor-ivy/src/main/java/org/jfrog/build/extractor/listener/ArtifactoryBuildListener.java +++ b/build-info-extractor-ivy/src/main/java/org/jfrog/build/extractor/listener/ArtifactoryBuildListener.java @@ -298,17 +298,15 @@ private void doDeploy(BuildEvent event) { if (StringUtils.isNotBlank(buildUrl)) { builder.url(buildUrl); } - Vcs vcs = new Vcs(); String vcsRevision = clientConf.info.getVcsRevision(); if (StringUtils.isNotBlank(vcsRevision)) { - vcs.setRevision(vcsRevision); builder.vcsRevision(vcsRevision); } String vcsUrl = clientConf.info.getVcsUrl(); if (StringUtils.isNotBlank(vcsUrl)) { - vcs.setUrl(vcsUrl); builder.vcsUrl(vcsUrl); } + Vcs vcs = new Vcs(vcsUrl, vcsRevision, clientConf.info.getVcsBranch(), clientConf.info.getVcsMessage()); if (!vcs.isEmpty()) { builder.vcs(Arrays.asList(vcs)); } diff --git a/build-info-extractor-maven3/src/main/java/org/jfrog/build/extractor/maven/BuildInfoModelPropertyResolver.java b/build-info-extractor-maven3/src/main/java/org/jfrog/build/extractor/maven/BuildInfoModelPropertyResolver.java index 0a1388b25..3d74b7066 100644 --- a/build-info-extractor-maven3/src/main/java/org/jfrog/build/extractor/maven/BuildInfoModelPropertyResolver.java +++ b/build-info-extractor-maven3/src/main/java/org/jfrog/build/extractor/maven/BuildInfoModelPropertyResolver.java @@ -50,7 +50,7 @@ public BuildInfoMavenBuilder resolveProperties(ExecutionEvent event, Artifactory BuildInfoMavenBuilder builder = resolveCoreProperties(event, clientConf). artifactoryPrincipal(clientConf.publisher.getName()).artifactoryPluginVersion(clientConf.info.getArtifactoryPluginVersion()). principal(clientConf.info.getPrincipal()).parentName( - clientConf.info.getParentBuildName()). + clientConf.info.getParentBuildName()). parentNumber(clientConf.info.getParentBuildNumber()); String buildUrl = clientConf.info.getBuildUrl(); @@ -58,17 +58,15 @@ public BuildInfoMavenBuilder resolveProperties(ExecutionEvent event, Artifactory builder.url(buildUrl); } - Vcs vcs = new Vcs(); String vcsRevision = clientConf.info.getVcsRevision(); if (StringUtils.isNotBlank(vcsRevision)) { - vcs.setRevision(vcsRevision); builder.vcsRevision(vcsRevision); } String vcsUrl = clientConf.info.getVcsUrl(); if (StringUtils.isNotBlank(vcsUrl)) { - vcs.setUrl(vcsUrl); builder.vcsUrl(vcsUrl); } + Vcs vcs = new Vcs(vcsUrl, vcsRevision, clientConf.info.getVcsBranch(), clientConf.info.getVcsMessage()); if (!vcs.isEmpty()) { builder.vcs(Arrays.asList(vcs)); } diff --git a/build-info-extractor/src/main/java/org/jfrog/build/extractor/ci/BuildInfoFields.java b/build-info-extractor/src/main/java/org/jfrog/build/extractor/ci/BuildInfoFields.java index ba736a1e3..ccb1f6a63 100644 --- a/build-info-extractor/src/main/java/org/jfrog/build/extractor/ci/BuildInfoFields.java +++ b/build-info-extractor/src/main/java/org/jfrog/build/extractor/ci/BuildInfoFields.java @@ -14,6 +14,8 @@ public interface BuildInfoFields { String BUILD_PARENT_NUMBER = "build.parentNumber"; String VCS_REVISION = "vcs.revision"; String VCS_URL = "vcs.url"; + String VCS_BRANCH = "vcs.branch"; + String VCS_MESSAGE = "vcs.message"; String PRINCIPAL = "principal"; String BUILD_URL = "buildUrl"; String BUILD_AGENT_NAME = "buildAgent.name"; diff --git a/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ArtifactoryClientConfiguration.java b/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ArtifactoryClientConfiguration.java index 8936114e2..149e70691 100644 --- a/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ArtifactoryClientConfiguration.java +++ b/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/ArtifactoryClientConfiguration.java @@ -29,38 +29,7 @@ import static org.jfrog.build.extractor.ci.BuildInfoConfigProperties.EXPORT_FILE; import static org.jfrog.build.extractor.ci.BuildInfoConfigProperties.INCLUDE_ENV_VARS; import static org.jfrog.build.extractor.ci.BuildInfoConfigProperties.PROPERTIES_FILE; -import static org.jfrog.build.extractor.ci.BuildInfoFields.AGENT_NAME; -import static org.jfrog.build.extractor.ci.BuildInfoFields.AGENT_VERSION; -import static org.jfrog.build.extractor.ci.BuildInfoFields.ARTIFACTORY_PLUGIN_VERSION; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BACKWARD_COMPATIBLE_DEPLOYABLE_ARTIFACTS; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_AGENT_NAME; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_AGENT_VERSION; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_NAME; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_NUMBER; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_NUMBERS_NOT_TO_DELETE; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_PARENT_NAME; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_PARENT_NUMBER; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_PROJECT; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_RETENTION_ASYNC; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_RETENTION_COUNT; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_RETENTION_DAYS; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_RETENTION_MINIMUM_DATE; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_ROOT; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_STARTED; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_TIMESTAMP; -import static org.jfrog.build.extractor.ci.BuildInfoFields.BUILD_URL; -import static org.jfrog.build.extractor.ci.BuildInfoFields.DELETE_BUILD_ARTIFACTS; -import static org.jfrog.build.extractor.ci.BuildInfoFields.DEPLOYABLE_ARTIFACTS; -import static org.jfrog.build.extractor.ci.BuildInfoFields.ENVIRONMENT_PREFIX; -import static org.jfrog.build.extractor.ci.BuildInfoFields.GENERATED_BUILD_INFO; -import static org.jfrog.build.extractor.ci.BuildInfoFields.INCREMENTAL; -import static org.jfrog.build.extractor.ci.BuildInfoFields.MIN_CHECKSUM_DEPLOY_SIZE_KB; -import static org.jfrog.build.extractor.ci.BuildInfoFields.PRINCIPAL; -import static org.jfrog.build.extractor.ci.BuildInfoFields.RELEASE_COMMENT; -import static org.jfrog.build.extractor.ci.BuildInfoFields.RELEASE_ENABLED; -import static org.jfrog.build.extractor.ci.BuildInfoFields.RUN_PARAMETERS; -import static org.jfrog.build.extractor.ci.BuildInfoFields.VCS_REVISION; -import static org.jfrog.build.extractor.ci.BuildInfoFields.VCS_URL; +import static org.jfrog.build.extractor.ci.BuildInfoFields.*; import static org.jfrog.build.extractor.ci.BuildInfoProperties.BUILD_INFO_ISSUES_TRACKER_PREFIX; import static org.jfrog.build.extractor.ci.BuildInfoProperties.BUILD_INFO_PREFIX; import static org.jfrog.build.extractor.ci.IssuesTrackerFields.AFFECTED_ISSUES; @@ -1096,6 +1065,22 @@ public void setVcsUrl(String vcsUrl) { setStringValue(VCS_URL, vcsUrl); } + public String getVcsBranch() { + return getStringValue(VCS_BRANCH); + } + + public void setVcsBranch(String vcsBranch) { + setStringValue(VCS_BRANCH, vcsBranch); + } + + public String getVcsMessage() { + return getStringValue(VCS_MESSAGE); + } + + public void setVcsMessage(String vcsMessage) { + setStringValue(VCS_MESSAGE, vcsMessage); + } + public String getAgentName() { return getStringValue(AGENT_NAME); } From eb61f76b76ca86212b88765eb5f087d3ca2d23d9 Mon Sep 17 00:00:00 2001 From: Tal Arian Date: Tue, 28 Dec 2021 18:59:31 +0200 Subject: [PATCH 3/3] Fix PR Comments --- .../build/extractor/clientConfiguration/util/GitUtils.java | 2 +- .../build/extractor/clientConfiguration/util/GitUtilsTest.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java b/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java index f44a59394..723a9d040 100644 --- a/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java +++ b/build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtils.java @@ -202,7 +202,7 @@ private static String extractVcsMessage(File dotGit, Log log) throws IOException try { latestCommit = new Git(repository).log().setMaxCount(1).call().iterator().next(); } catch (GitAPIException e) { - log.warn("Failed fetching commit message from git directory: " + dotGit); + log.warn("Failed fetching commit message from git directory: " + dotGit + "\nWith the following error: " + e.getMessage()); return ""; } diff --git a/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java b/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java index 413dd5a94..7dc8372ce 100644 --- a/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java +++ b/build-info-extractor/src/test/java/org/jfrog/build/extractor/clientConfiguration/util/GitUtilsTest.java @@ -31,7 +31,6 @@ private void testReadGitConfig() throws IOException, InterruptedException { Assert.assertEquals(vcs.getRevision(), getGitRevisionWithExecutor(curDir, testLog)); Assert.assertEquals(vcs.getBranch(), getGitBranchWithExecutor(curDir, testLog)); Assert.assertEquals(vcs.getMessage(), getGitMessageWithExecutor(curDir, testLog)); - } private String getGitFieldWithExecutor(File execDir, Log log, List args) throws IOException, InterruptedException {