From f1605262d3305e6c9d809f5778b3cbaa6bfe953d Mon Sep 17 00:00:00 2001 From: Vitalii Mahas Date: Fri, 16 May 2025 10:09:58 +0200 Subject: [PATCH 1/3] Added support for build number in Github Actions --- .../core/cibuild/GitHubBuildServerData.java | 5 +- .../core/GitCommitIdTestCallback.java | 32 +------- .../cibuild/BuildServerDataProviderTest.java | 79 +++++++++++++++++++ .../project13/core/log/DummyLogInterface.java | 28 +++++++ 4 files changed, 113 insertions(+), 31 deletions(-) create mode 100644 src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java create mode 100644 src/test/java/pl/project13/core/log/DummyLogInterface.java diff --git a/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java b/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java index f6108a6..f1b0725 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,9 @@ public static boolean isActiveServer(Map env) { @Override void loadBuildNumber(@Nonnull Properties properties) { - // This information is not reliably available on GitHub Actions + String buildNumber = env.getOrDefault("GITHUB_RUN_NUMBER", ""); + + maybePut(properties, GitCommitPropertyConstant.BUILD_NUMBER, () -> buildNumber); } @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..b4fb679 --- /dev/null +++ b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java @@ -0,0 +1,79 @@ +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_NUMBER", "123"); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + provider.loadBuildNumber(properties); + + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "123"); + } + + @Test + void shouldLoadBuildNumberAsEmptyIfNotAvailable() { + Properties properties = new Properties(); + Map environment = Map.of(); + GitHubBuildServerData provider = new GitHubBuildServerData(new DummyLogInterface(), environment); + + provider.loadBuildNumber(properties); + + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, ""); + } + + @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..f03bf49 --- /dev/null +++ b/src/test/java/pl/project13/core/log/DummyLogInterface.java @@ -0,0 +1,28 @@ +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 + } +} From ae13c191c914cbd64cecece2764a364db7e1928b Mon Sep 17 00:00:00 2001 From: Vitalii Mahas Date: Fri, 16 May 2025 10:18:17 +0200 Subject: [PATCH 2/3] Fix checkstyle --- .../cibuild/BuildServerDataProviderTest.java | 17 +++++++++++++++++ .../project13/core/log/DummyLogInterface.java | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java index b4fb679..c1429eb 100644 --- a/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java +++ b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java @@ -1,3 +1,20 @@ +/* + * 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; diff --git a/src/test/java/pl/project13/core/log/DummyLogInterface.java b/src/test/java/pl/project13/core/log/DummyLogInterface.java index f03bf49..ffb8939 100644 --- a/src/test/java/pl/project13/core/log/DummyLogInterface.java +++ b/src/test/java/pl/project13/core/log/DummyLogInterface.java @@ -1,3 +1,20 @@ +/* + * 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 { From 8856e4e310064f37d9d313f4a39cd484f377948b Mon Sep 17 00:00:00 2001 From: Vitalii Mahas Date: Mon, 19 May 2025 21:23:19 +0200 Subject: [PATCH 3/3] Addressed PR comments --- .../core/GitCommitPropertyConstant.java | 18 ++++++++++-------- .../core/cibuild/GitHubBuildServerData.java | 7 +++++-- .../cibuild/BuildServerDataProviderTest.java | 13 +++++++++---- 3 files changed, 24 insertions(+), 14 deletions(-) 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: *
    + *
  • AWS CodeBuild
  • + *
  • Azure DevOps
  • *
  • Bamboo
  • + *
  • Bitbucket Pipelines
  • + *
  • GitHub Actions
  • + *
  • Gitlab CI (Gitlab >8.10 & Gitlab CI >0.5)
  • *
  • Hudson/Jenkins
  • *
  • TeamCity
  • *
  • Travis
  • - *
  • Gitlab CI (Gitlab >8.10 & Gitlab CI >0.5)
  • - *
  • Azure DevOps
  • - *
  • AWS CodeBuild
  • - *
  • Bitbucket Pipelines
  • *
*/ public static final String BUILD_NUMBER = "build.number"; @@ -127,10 +128,11 @@ public class GitCommitPropertyConstant { * * Currently supported CIs: *
    - *
  • TeamCity
  • - *
  • Travis
  • - *
  • Gitlab CI (Gitlab >11.0)
  • - *
  • AWS CodeBuild
  • + *
  • AWS CodeBuild
  • + *
  • Gitlab CI (Gitlab >11.0)
  • + *
  • GitHub Actions
  • + *
  • TeamCity
  • + *
  • Travis
  • *
*/ 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 f1b0725..1b88fdb 100644 --- a/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java +++ b/src/main/java/pl/project13/core/cibuild/GitHubBuildServerData.java @@ -40,9 +40,12 @@ public static boolean isActiveServer(Map env) { @Override void loadBuildNumber(@Nonnull Properties properties) { - String buildNumber = env.getOrDefault("GITHUB_RUN_NUMBER", ""); + 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, () -> buildNumber); + 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/cibuild/BuildServerDataProviderTest.java b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java index c1429eb..08d3344 100644 --- a/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java +++ b/src/test/java/pl/project13/core/cibuild/BuildServerDataProviderTest.java @@ -49,23 +49,28 @@ void shouldVerifyOnGithubEnvironment() { @Test void shouldLoadBuildNumber() { Properties properties = new Properties(); - Map environment = Map.of("GITHUB_RUN_NUMBER", "123"); + 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"); + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "123.1"); + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "1658821493.123.1"); } @Test - void shouldLoadBuildNumberAsEmptyIfNotAvailable() { + 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, ""); + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER, "0.0"); + assertThat(properties).containsEntry(GitCommitPropertyConstant.BUILD_NUMBER_UNIQUE, "0.0.0"); } @Test