private VersionInfo(final VersionInfoProperties versionInfoProperties, final GitProperties gitProperties) { if (gitProperties != null) { this.commitId = gitProperties.getCommitId(); this.commitIdAbbrev = gitProperties.getShortCommitId(); this.branch = gitProperties.getBranch(); this.commitTime = gitProperties.get(COMMIT_TIME); this.userName = gitProperties.get(USER_NAME); this.userEmail = gitProperties.get(USER_EMAIL); this.messageShort = gitProperties.get(MESSAGE_SHORT); this.messageFull = gitProperties.get(MESSAGE_FULL); } else { this.commitId = versionInfoProperties.getCommitId(); this.commitIdAbbrev= versionInfoProperties.getCommitIdAbbrev(); this.commitTime = versionInfoProperties.getCommitTime(); this.userName = versionInfoProperties.getUserName(); this.userEmail = versionInfoProperties.getUserEmail(); this.messageShort = versionInfoProperties.getMessageShort(); this.messageFull = versionInfoProperties.getMessageFull(); this.branch = versionInfoProperties.getBranch(); } this.version = Objects.toString(versionInfoProperties.getVersion(), this.commitId); this.url = versionInfoProperties.getUrlTemplate().replace("{commit}", commitId).replace("{version}", version); }
@Bean @ConditionalOnEnabledInfoContributor("git") @ConditionalOnSingleCandidate(GitProperties.class) @ConditionalOnMissingBean @Order(DEFAULT_ORDER) public GitInfoContributor gitInfoContributor(GitProperties gitProperties) { return new GitInfoContributor(gitProperties, this.properties.getGit().getMode()); }
@SuppressWarnings("unchecked") @Test public void coerceDate() { Properties properties = new Properties(); properties.put("branch", "master"); properties.put("commit.time", "2016-03-04T14:36:33+0100"); GitInfoContributor contributor = new GitInfoContributor( new GitProperties(properties)); Map<String, Object> content = contributor.generateContent(); assertThat(content.get("commit")).isInstanceOf(Map.class); Map<String, Object> commit = (Map<String, Object>) content.get("commit"); Object commitTime = commit.get("time"); assertThat(commitTime).isInstanceOf(Date.class); assertThat(((Date) commitTime).getTime()).isEqualTo(1457098593000L); }
@SuppressWarnings("unchecked") @Test public void shortenCommitId() { Properties properties = new Properties(); properties.put("branch", "master"); properties.put("commit.id", "8e29a0b0d423d2665c6ee5171947c101a5c15681"); GitInfoContributor contributor = new GitInfoContributor( new GitProperties(properties)); Map<String, Object> content = contributor.generateContent(); assertThat(content.get("commit")).isInstanceOf(Map.class); Map<String, Object> commit = (Map<String, Object>) content.get("commit"); assertThat(commit.get("id")).isEqualTo("8e29a0b"); }
@Bean public GitProperties gitProperties() { Properties properties = new Properties(); properties.put("branch", "master"); properties.put("commit.id", "abcdefg"); properties.put("foo", "bar"); return new GitProperties(properties); }
@Test public void gitPropertiesUnavailableIfResourceNotAvailable() { load(); Map<String, GitProperties> beans = this.context .getBeansOfType(GitProperties.class); assertThat(beans).hasSize(0); }
@Test public void gitLocationTakesPrecedenceOverLegacyKey() { load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties", "spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties"); GitProperties gitProperties = this.context.getBean(GitProperties.class); assertThat(gitProperties.getBranch()).isNull(); assertThat(gitProperties.getCommitId()) .isEqualTo("f95038ec09e29d8f91982fd1cbcc0f3b131b1d0a"); assertThat(gitProperties.getCommitTime().getTime()).isEqualTo(1456995720000L); }
@Test public void gitLegacyKeyIsUsedAsFallback() { load("spring.git.properties=classpath:/org/springframework/boot/autoconfigure/info/git-epoch.properties"); GitProperties gitProperties = this.context.getBean(GitProperties.class); assertThat(gitProperties.getBranch()).isEqualTo("master"); assertThat(gitProperties.getCommitId()) .isEqualTo("5009933788f5f8c687719de6a697074ff80b1b69"); assertThat(gitProperties.getCommitTime().getTime()).isEqualTo(1457103850000L); }
@Test public void gitPropertiesFallbackWithGitPropertiesBean() { load(CustomInfoPropertiesConfiguration.class, "spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git.properties"); GitProperties gitProperties = this.context.getBean(GitProperties.class); assertThat(gitProperties).isSameAs(this.context.getBean("customGitProperties")); }
public GitInfoContributor(GitProperties properties, Mode mode) { super(properties, mode); }
public GitInfoContributor(GitProperties properties) { this(properties, Mode.SIMPLE); }
@Bean public GitInfoContributor customGitInfoContributor() { return new GitInfoContributor(new GitProperties(new Properties())); }
@Conditional(GitResourceAvailableCondition.class) @ConditionalOnMissingBean @Bean public GitProperties gitProperties() throws Exception { return new GitProperties(loadFrom(this.properties.getGit().getLocation(), "git")); }
@Test public void gitPropertiesWithNoData() { load("spring.info.git.location=classpath:/org/springframework/boot/autoconfigure/info/git-no-data.properties"); GitProperties gitProperties = this.context.getBean(GitProperties.class); assertThat(gitProperties.getBranch()).isNull(); }
@Bean public GitProperties customGitProperties() { return new GitProperties(new Properties()); }
private GitProperties gitProperties(final String commitId) { return new GitProperties(new Properties() {{ put("commit.id", commitId); }}); }
/** * Creates VersionInfo from Spring Boot {@link GitProperties}. Missing Information ({@link #version} and * {@link #url})is filled from VersionInfoProperties. * * @param versionInfoProperties Edison VersionInfoProperties used for version and url * @param gitProperties Spring Boot GitProperties for all the other properties. * @return VersionInfo */ public static VersionInfo versionInfo(final VersionInfoProperties versionInfoProperties, final GitProperties gitProperties) { return new VersionInfo(versionInfoProperties, gitProperties); }