Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vcs.branch and vcs commit message to VCS build-info details #591

Merged
merged 3 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,23 @@ 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();
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 + "\nWith the following error: " + e.getMessage());
return "";
}

return latestCommit.getFullMessage();
}

/**
* This class is used in the process of extracting Vcs revision from the .git configuration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ 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<String> args) throws IOException, InterruptedException {
Expand All @@ -55,6 +57,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<String> 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<String> 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";
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
talarian1 marked this conversation as resolved.
Show resolved Hide resolved
implementation project(':build-info-client')
implementation project(':build-info-api')

Expand Down