From 082f944dfd2f3d8a7087a9b33c742a8330b65b29 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Mon, 23 Oct 2023 15:31:42 -0700 Subject: [PATCH] add mechanism to set config committer when unavailable --- .github/workflows/nebula.yml | 8 --- .../release/git/base/BaseReleasePlugin.groovy | 3 - .../git/command/GitCommandParameters.groovy | 3 + .../release/git/command/GitReadCommand.groovy | 37 +++++++++++- .../git/command/GitReadOnlyCommandUtil.groovy | 58 +++++++++++++++++++ .../git/command/GitWriteCommandsUtil.groovy | 1 + 6 files changed, 98 insertions(+), 12 deletions(-) diff --git a/.github/workflows/nebula.yml b/.github/workflows/nebula.yml index 0824472..9d68be1 100644 --- a/.github/workflows/nebula.yml +++ b/.github/workflows/nebula.yml @@ -26,10 +26,6 @@ jobs: name: Gradle Build without Publish steps: - uses: actions/checkout@v1 - - name: Setup git user - run: | - git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)" - git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)" - name: Set up JDK 8 uses: actions/setup-java@v2 with: @@ -70,10 +66,6 @@ jobs: NETFLIX_OSS_SONATYPE_PASSWORD: ${{ secrets.ORG_SONATYPE_PASSWORD }} steps: - uses: actions/checkout@v1 - - name: Setup git user - run: | - git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)" - git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)" - name: Setup jdk 8 uses: actions/setup-java@v1 with: diff --git a/src/main/groovy/nebula/plugin/release/git/base/BaseReleasePlugin.groovy b/src/main/groovy/nebula/plugin/release/git/base/BaseReleasePlugin.groovy index e0db819..a711fc9 100644 --- a/src/main/groovy/nebula/plugin/release/git/base/BaseReleasePlugin.groovy +++ b/src/main/groovy/nebula/plugin/release/git/base/BaseReleasePlugin.groovy @@ -16,11 +16,8 @@ package nebula.plugin.release.git.base import groovy.transform.CompileDynamic -import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project -import org.slf4j.Logger -import org.slf4j.LoggerFactory /** * Plugin providing the base structure of gradle-git's flavor of release diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy index 687762e..4c2ff8e 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitCommandParameters.groovy @@ -6,4 +6,7 @@ import org.gradle.api.provider.ValueSourceParameters interface GitCommandParameters extends ValueSourceParameters { Property getRootDir() Property getTagForSearch() + Property getGitConfigScope() + Property getGitConfigKey() + Property getGitConfigValue() } diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy index 08520ac..973355b 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy @@ -4,6 +4,8 @@ import groovy.transform.CompileDynamic import org.gradle.api.GradleException import org.gradle.api.provider.ValueSource import org.gradle.process.ExecOperations +import org.slf4j.Logger +import org.slf4j.LoggerFactory import javax.inject.Inject import java.nio.charset.Charset @@ -13,7 +15,6 @@ import java.nio.charset.Charset * @see {@link https://docs.gradle.org/8.4/userguide/configuration_cache.html#config_cache:requirements:external_processes} */ abstract class GitReadCommand implements ValueSource { - @Inject abstract ExecOperations getExecOperations() @@ -217,3 +218,37 @@ abstract class StatusPorcelain extends GitReadCommand { } } } + +/** + * Retrieves a given Git config key with its value for a given scope + */ +abstract class GetGitConfigValue extends GitReadCommand { + private static final Logger logger = LoggerFactory.getLogger(GetGitConfigValue) + @Override + String obtain() { + try { + return executeGitCommand( "config", parameters.getGitConfigScope().get(), parameters.getGitConfigKey().get()) + } catch (Exception e) { + logger.debug("Could not get git config {} {} {}", parameters.getGitConfigScope().get(), parameters.getGitConfigKey().get()) + return null + } + } +} + + +/** + * Set a given Git config key with its value for a given scope + */ +abstract class SetGitConfigValue extends GitReadCommand { + private static final Logger logger = LoggerFactory.getLogger(SetGitConfigValue) + @Override + String obtain() { + try { + return executeGitCommand( "config", parameters.getGitConfigKey().get(), parameters.getGitConfigValue().get()) + } catch (Exception e) { + logger.debug("Could not set git config {} {} {}", parameters.getGitConfigKey().get(), parameters.getGitConfigValue().get()) + return null + } + } +} + diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy index 9616bfc..35d2eb4 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy @@ -3,8 +3,11 @@ package nebula.plugin.release.git.command import nebula.plugin.release.git.model.TagRef import org.gradle.api.provider.Provider import org.gradle.api.provider.ProviderFactory +import org.slf4j.Logger +import org.slf4j.LoggerFactory class GitReadOnlyCommandUtil implements Serializable { + private static final Logger logger = LoggerFactory.getLogger(GitReadOnlyCommandUtil) private final ProviderFactory providers private Provider currentBranchProvider @@ -30,6 +33,7 @@ class GitReadOnlyCommandUtil implements Serializable { */ void configure(File gitRoot) { this.rootDir = gitRoot + configureCommitterIfNecessary() currentBranchProvider = providers.of(CurrentBranch.class) { it.parameters.rootDir.set(rootDir) } @@ -63,6 +67,19 @@ class GitReadOnlyCommandUtil implements Serializable { } + private void configureCommitterIfNecessary() { + String globalUsername = getGitConfig('--global', 'user.name') + String globalEmail = getGitConfig('--global', 'user.email') + String localUsername = getGitConfig('--local', 'user.name') + String localEmail = getGitConfig('--local', 'user.email') + if(!globalUsername && !localUsername) { + setGitConfig("user.name", "\"\$(git --no-pager log --format=format:'%an' -n 1)\"") + } + if(!globalEmail && !localEmail) { + setGitConfig("user.email", "\"\$(git --no-pager log --format=format:'%ae' -n 1)\"") + } + } + Boolean isGitRepo() { try { return Boolean.valueOf(isGitRepoProvider.get().toString()) @@ -181,4 +198,45 @@ class GitReadOnlyCommandUtil implements Serializable { return false } } + + /** + * Returns a git config value for a given scope + * @param scope + * @param configKey + * @return + */ + String getGitConfig(String scope, String configKey) { + try { + def getConfigValueProvider = providers.of(GetGitConfigValue.class) { + it.parameters.rootDir.set(rootDir) + it.parameters.gitConfigScope.set(scope) + it.parameters.gitConfigKey.set(configKey) + } + return getConfigValueProvider.get().toString()?. + replaceAll("\n", "")?.toString() + } catch(Exception e) { + logger.debug("Could not get git config {} {} {}", scope, configKey) + return null + } + } + + /** + * Returns a git config value for a given scope + * @param scope + * @param configKey + * @return + */ + void setGitConfig(String configKey, String configValue) { + try { + def getConfigValueProvider = providers.of(SetGitConfigValue.class) { + it.parameters.rootDir.set(rootDir) + it.parameters.gitConfigKey.set(configKey) + it.parameters.gitConfigValue.set(configValue) + } + getConfigValueProvider.get().toString()?. + replaceAll("\n", "")?.toString() + } catch(Exception e) { + logger.debug("Could not set git config {} {}", configKey, configValue) + } + } } diff --git a/src/main/groovy/nebula/plugin/release/git/command/GitWriteCommandsUtil.groovy b/src/main/groovy/nebula/plugin/release/git/command/GitWriteCommandsUtil.groovy index d4a60b3..00ce6eb 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitWriteCommandsUtil.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitWriteCommandsUtil.groovy @@ -25,6 +25,7 @@ class GitWriteCommandsUtil implements Serializable { this.rootDir = gitRoot } + /** * Pushes a Tag to a remote repository * @param remote