diff --git a/src/main/java/pl/project13/core/GitCommitPropertyConstant.java b/src/main/java/pl/project13/core/GitCommitPropertyConstant.java index 2947fcd..ecc49ff 100644 --- a/src/main/java/pl/project13/core/GitCommitPropertyConstant.java +++ b/src/main/java/pl/project13/core/GitCommitPropertyConstant.java @@ -108,14 +108,15 @@ public class GitCommitPropertyConstant { * * Currently supported CIs: * */ public static final String BUILD_NUMBER = "build.number"; @@ -127,10 +128,11 @@ public class GitCommitPropertyConstant { * * Currently supported CIs: * */ public static final String BUILD_NUMBER_UNIQUE = "build.number.unique"; diff --git a/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java b/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java index f6108a6..1b88fdb 100644 --- a/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java +++ b/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java @@ -17,6 +17,7 @@ package pl.project13.core.cibuild; +import pl.project13.core.GitCommitPropertyConstant; import pl.project13.core.log.LogInterface; import javax.annotation.Nonnull; @@ -39,7 +40,12 @@ public static boolean isActiveServer(Map env) { @Override void loadBuildNumber(@Nonnull Properties properties) { - // This information is not reliably available on GitHub Actions + String runId = env.getOrDefault("GITHUB_RUN_ID", "0"); + String runNumber = env.getOrDefault("GITHUB_RUN_NUMBER", "0"); + String runAttempt = env.getOrDefault("GITHUB_RUN_ATTEMPT", "0"); + + maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER, () -> String.join(".", runNumber, runAttempt)); + maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, () -> String.join(".", runId, runNumber, runAttempt)); } @Override diff --git a/src/test/java/pl/project13/core/GitCommitIdTestCallback.java b/src/test/java/pl/project13/core/GitCommitIdTestCallback.java index 07b11c8..a637d12 100644 --- a/src/test/java/pl/project13/core/GitCommitIdTestCallback.java +++ b/src/test/java/pl/project13/core/GitCommitIdTestCallback.java @@ -18,6 +18,7 @@ package pl.project13.core; import pl.project13.core.git.GitDescribeConfig; +import pl.project13.core.log.DummyLogInterface; import pl.project13.core.log.LogInterface; import pl.project13.core.util.BuildFileChangeListener; @@ -34,7 +35,7 @@ public class GitCommitIdTestCallback { private Map systemEnv = System.getenv(); private String projectVersion = "dummy-version"; - private LogInterface logInterface = createDummyLogInterface(); + private LogInterface logInterface = new DummyLogInterface(); private String dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"; private String dateFormatTimeZone = TimeZone.getDefault().getID(); private String prefixDot = "git."; @@ -362,33 +363,4 @@ private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int a gitDescribeConfig.setAbbrev(abbrev); return gitDescribeConfig; } - - private LogInterface createDummyLogInterface() { - return new LogInterface() { - @Override - public void debug(String msg) { - // ignore - } - - @Override - public void info(String msg) { - // ignore - } - - @Override - public void warn(String msg) { - // ignore - } - - @Override - public void error(String msg) { - // ignore - } - - @Override - public void error(String msg, Throwable t) { - // ignore - } - }; - } } diff --git a/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java new file mode 100644 index 0000000..08d3344 --- /dev/null +++ b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java @@ -0,0 +1,101 @@ +/* + * This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski + * + * git-commit-id-plugin-core is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * git-commit-id-plugin-core is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with git-commit-id-plugin-core. If not, see . + */ + +package pl.project13.core.cibuild; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import pl.project13.core.GitCommitPropertyConstant; +import pl.project13.core.log.DummyLogInterface; + +import java.util.Map; +import java.util.Properties; + +import static org.assertj.core.api.Assertions.assertThat; + +class BuildServerDataProviderTest { + @Test + void shouldSelectGithubAsDataProvider() { + Map environment = Map.of("GITHUB_ACTIONS", "true"); + + BuildServerDataProvider provider = BuildServerDataProvider.getBuildServerProvider(environment, new DummyLogInterface()); + + assertThat(provider).isInstanceOf(GitHubBuildServerData.class); + } + + @Nested + class GithubProviderTests { + @Test + void shouldVerifyOnGithubEnvironment() { + Map environment = Map.of("GITHUB_ACTIONS", "true"); + + assertThat(GitHubBuildServerData.isActiveServer(environment)).isTrue(); + } + + @Test + void shouldLoadBuildNumber() { + Properties properties = new Properties(); + Map environment = Map.of( + "GITHUB_RUN_ID", "1658821493", + "GITHUB_RUN_NUMBER", "123", + "GITHUB_RUN_ATTEMPT", "1"); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + provider.loadBuildNumber(properties); + + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "123.1"); + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "1658821493.123.1"); + } + + @Test + void shouldLoadBuildNumberAsZerosIfNotAvailable() { + Properties properties = new Properties(); + Map environment = Map.of(); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + provider.loadBuildNumber(properties); + + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "0.0"); + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "0.0.0"); + } + + @Test + void shouldLoadBranchNameForPullRequestBuild() { + Map environment = Map.of("GITHUB_REF", "refs/pull/feature_branch", + "GITHUB_HEAD_REF", "feature_branch"); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + assertThat(provider.getBuildBranch()).isEqualTo("feature_branch"); + } + + @Test + void shouldLoadBranchNameForBranchBuild() { + Map environment = Map.of("GITHUB_REF", "refs/heads/feature_branch"); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + assertThat(provider.getBuildBranch()).isEqualTo("feature_branch"); + } + + @Test + void shouldLoadBranchNameAsEmptyIfNotAvailable() { + Map environment = Map.of(); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + assertThat(provider.getBuildBranch()).isEmpty(); + } + } +} \ No newline at end of file diff --git a/src/test/java/pl/project13/core/log/DummyLogInterface.java b/src/test/java/pl/project13/core/log/DummyLogInterface.java new file mode 100644 index 0000000..ffb8939 --- /dev/null +++ b/src/test/java/pl/project13/core/log/DummyLogInterface.java @@ -0,0 +1,45 @@ +/* + * This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski + * + * git-commit-id-plugin-core is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * git-commit-id-plugin-core is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with git-commit-id-plugin-core. If not, see . + */ + +package pl.project13.core.log; + +public class DummyLogInterface implements LogInterface { + @Override + public void debug(String msg) { + // ignore + } + + @Override + public void info(String msg) { + // ignore + } + + @Override + public void warn(String msg) { + // ignore + } + + @Override + public void error(String msg) { + // ignore + } + + @Override + public void error(String msg, Throwable t) { + // ignore + } +}