Skip to content

Commit

Permalink
TS-38561 Split long method and update JavaDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonymFx committed May 22, 2024
1 parent c8f3507 commit 8f94782
Showing 1 changed file with 82 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,10 @@ static List<Pair<String, Properties>> findGitPropertiesInArchive(
}

/**
* TODO
*
* @param gitProperties
* @param entryName
* @param jarFile
* @return
* @throws InvalidGitPropertiesException
* Returns the CommitInfo (revision & branch + timestmap) from a git properties file. The revision can be either in
* {@link #GIT_PROPERTIES_GIT_COMMIT_ID} or {@link #GIT_PROPERTIES_GIT_COMMIT_ID_FULL}. The branch and timestamp in
* {@link #GIT_PROPERTIES_GIT_BRANCH} + {@link #GIT_PROPERTIES_GIT_COMMIT_TIME} or in
* {@link #GIT_PROPERTIES_TEAMSCALE_TIMESTAMP}
*/
public static CommitInfo getCommitInfoFromGitProperties(
Properties gitProperties, String entryName, File jarFile) throws InvalidGitPropertiesException {
Expand All @@ -344,36 +341,59 @@ public static CommitInfo getCommitInfoFromGitProperties(
}

/**
* Returns a value from a git properties file for the Git SHA1. This can be either in
* {@link #GIT_PROPERTIES_GIT_COMMIT_ID} or {@link #GIT_PROPERTIES_GIT_COMMIT_ID_FULL}.
* TODO Update
* Same as {@link #getCommitInfoFromGitProperties(Properties, String, File)} but with the option to provide a
* DateTimeFormatter that is used to parse the times in {@link #GIT_PROPERTIES_GIT_COMMIT_TIME} and
* {@link #GIT_PROPERTIES_TEAMSCALE_TIMESTAMP}
*/
public static CommitInfo getCommitInfoFromGitProperties(
Properties gitProperties, String entryName, File jarFile,
DateTimeFormatter dateTimeFormatter) throws InvalidGitPropertiesException {
// TODO: clean this up (split method)

// Get Revision
String revision = getRevisionFromGitProperties(gitProperties);

// Get branch and timestamp from git.commit.branch and git.commit.id
CommitDescriptor commitDescriptor = getCommitDescriptorFromDefaultGitPropertyValues(gitProperties, entryName,
jarFile,
dateTimeFormatter);
// When read from these properties, we should prefer to upload to the revision
boolean preferCommitDescriptorOverRevision = false;


// Get branch and timestamp from teamscale.timestamp (TS-38561)
CommitDescriptor teamscaleTimestampBasedCommitDescriptor = getCommitDescriptorFromTeamscaleTimestampProperty(
gitProperties,
entryName, jarFile, dateTimeFormatter);
if (teamscaleTimestampBasedCommitDescriptor != null) {
// In this case, as we specifically set this property, we should prefer branch and timestamp to the revision
preferCommitDescriptorOverRevision = true;
commitDescriptor = teamscaleTimestampBasedCommitDescriptor;
}

if (StringUtils.isEmpty(revision) && commitDescriptor == null) {
throw new InvalidGitPropertiesException(
"No entry or invalid value for '" + GIT_PROPERTIES_GIT_COMMIT_ID + "', '" + GIT_PROPERTIES_GIT_COMMIT_ID_FULL +
"' and " + GIT_PROPERTIES_TEAMSCALE_TIMESTAMP + "'\n" +
"In " + entryName + " in " + jarFile + "." +
"\nContents of " + GIT_PROPERTIES_FILE_NAME + ":\n" + gitProperties);
}

CommitInfo commitInfo = new CommitInfo(revision, commitDescriptor);
commitInfo.preferCommitDescriptorOverRevision = preferCommitDescriptorOverRevision;
return commitInfo;
}

private static String getRevisionFromGitProperties(Properties gitProperties) {
String revision = gitProperties.getProperty(GIT_PROPERTIES_GIT_COMMIT_ID);
if (StringUtils.isEmpty(revision)) {
revision = gitProperties.getProperty(GIT_PROPERTIES_GIT_COMMIT_ID_FULL);
}
return revision;
}

CommitDescriptor commitDescriptor = null;
String gitBranch = gitProperties.getProperty(GIT_PROPERTIES_GIT_BRANCH);
String gitTime = gitProperties.getProperty(GIT_PROPERTIES_GIT_COMMIT_TIME);
if (!StringUtils.isEmpty(gitBranch) && !StringUtils.isEmpty(gitTime)) {
long gitTimestamp = 0;
try {
gitTimestamp = ZonedDateTime.parse(gitTime, dateTimeFormatter).toInstant().toEpochMilli();
} catch (DateTimeParseException e) {
throw new InvalidGitPropertiesException(
"Could not parse the timestamp in property '" + GIT_PROPERTIES_GIT_COMMIT_TIME + "'." +
"\nIn " + entryName + " in " + jarFile + "." +
"\nContents of " + GIT_PROPERTIES_FILE_NAME + ":\n" + gitProperties, e);
}
commitDescriptor = new CommitDescriptor(gitBranch, gitTimestamp);
}

boolean preferCommitDescriptorOverRevision = false;
private static CommitDescriptor getCommitDescriptorFromTeamscaleTimestampProperty(Properties gitProperties,
String entryName, File jarFile,
DateTimeFormatter dateTimeFormatter) throws InvalidGitPropertiesException {
String teamscaleTimestampProperty = gitProperties.getProperty(GIT_PROPERTIES_TEAMSCALE_TIMESTAMP);
if (!StringUtils.isEmpty(teamscaleTimestampProperty)) {
String[] split = teamscaleTimestampProperty.split(":");
Expand All @@ -389,36 +409,44 @@ public static CommitInfo getCommitInfoFromGitProperties(
String teamscaleTimestampRegex = "\\d*(?:p\\d*)?";
Matcher teamscaleTimestampMatcher = Pattern.compile(teamscaleTimestampRegex).matcher(timestamp);
if (teamscaleTimestampMatcher.matches()) {
commitDescriptor = new CommitDescriptor(branch, timestamp);
} else {
long epochTimestamp = 0;
try {
epochTimestamp = ZonedDateTime.parse(timestamp, dateTimeFormatter).toInstant().toEpochMilli();
} catch (DateTimeParseException e) {
throw new InvalidGitPropertiesException(
"Cannot parse commit time '" + timestamp + "' in the '" + GIT_PROPERTIES_TEAMSCALE_TIMESTAMP +
"' property. It needs to be in the date formats '" + GIT_PROPERTIES_DEFAULT_MAVEN_DATE_FORMAT +
"' or '" + GIT_PROPERTIES_DEFAULT_GRADLE_DATE_FORMAT + "' or match the Teamscale timestamp format '"
+ teamscaleTimestampRegex + "'." +
"\nIn " + entryName + " in " + jarFile + "." +
"\nContents of " + GIT_PROPERTIES_FILE_NAME + ":\n" + gitProperties, e);
}
return new CommitDescriptor(branch, timestamp);
}

commitDescriptor = new CommitDescriptor(branch, epochTimestamp);
preferCommitDescriptorOverRevision = true;
long epochTimestamp = 0;
try {
epochTimestamp = ZonedDateTime.parse(timestamp, dateTimeFormatter).toInstant().toEpochMilli();
} catch (DateTimeParseException e) {
throw new InvalidGitPropertiesException(
"Cannot parse commit time '" + timestamp + "' in the '" + GIT_PROPERTIES_TEAMSCALE_TIMESTAMP +
"' property. It needs to be in the date formats '" + GIT_PROPERTIES_DEFAULT_MAVEN_DATE_FORMAT +
"' or '" + GIT_PROPERTIES_DEFAULT_GRADLE_DATE_FORMAT + "' or match the Teamscale timestamp format '"
+ teamscaleTimestampRegex + "'." +
"\nIn " + entryName + " in " + jarFile + "." +
"\nContents of " + GIT_PROPERTIES_FILE_NAME + ":\n" + gitProperties, e);
}
}

if (StringUtils.isEmpty(revision) && commitDescriptor == null) {
throw new InvalidGitPropertiesException(
"No entry or invalid value for '" + GIT_PROPERTIES_GIT_COMMIT_ID + "', '" + GIT_PROPERTIES_GIT_COMMIT_ID_FULL +
"' and " + GIT_PROPERTIES_TEAMSCALE_TIMESTAMP + "'\n" +
"In " + entryName + " in " + jarFile + "." +
"\nContents of " + GIT_PROPERTIES_FILE_NAME + ":\n" + gitProperties);
return new CommitDescriptor(branch, epochTimestamp);
}
return null;

Check warning on line 430 in agent/src/main/java/com/teamscale/jacoco/agent/commit_resolution/git_properties/GitPropertiesLocatorUtils.java

View check run for this annotation

cqse.teamscale.io / teamscale-findings

agent/src/main/java/com/teamscale/jacoco/agent/commit_resolution/git_properties/GitPropertiesLocatorUtils.java#L397-L430

[New] Violation of method length threshold (source lines of code) of 30: 32 https://cqse.teamscale.io/findings.html#details/teamscale-jacoco-agent?t=ts%2F38561_read_teamscale.timestamp_from_git.properties%3AHEAD&id=A10DAD34F1A4EDB75F54C0F47BB54EC1
}

CommitInfo commitInfo = new CommitInfo(revision, commitDescriptor);
commitInfo.preferCommitDescriptorOverRevision = preferCommitDescriptorOverRevision;
return commitInfo;
private static CommitDescriptor getCommitDescriptorFromDefaultGitPropertyValues(Properties gitProperties,
String entryName, File jarFile,
DateTimeFormatter dateTimeFormatter) throws InvalidGitPropertiesException {
String gitBranch = gitProperties.getProperty(GIT_PROPERTIES_GIT_BRANCH);
String gitTime = gitProperties.getProperty(GIT_PROPERTIES_GIT_COMMIT_TIME);
if (!StringUtils.isEmpty(gitBranch) && !StringUtils.isEmpty(gitTime)) {
long gitTimestamp = 0;
try {
gitTimestamp = ZonedDateTime.parse(gitTime, dateTimeFormatter).toInstant().toEpochMilli();
} catch (DateTimeParseException e) {
throw new InvalidGitPropertiesException(
"Could not parse the timestamp in property '" + GIT_PROPERTIES_GIT_COMMIT_TIME + "'." +
"\nIn " + entryName + " in " + jarFile + "." +
"\nContents of " + GIT_PROPERTIES_FILE_NAME + ":\n" + gitProperties, e);
}
return new CommitDescriptor(gitBranch, gitTimestamp);
}
return null;
}
}

0 comments on commit 8f94782

Please sign in to comment.