diff --git a/.github/workflows/nebula.yml b/.github/workflows/nebula.yml index 9d68be1..9310034 100644 --- a/.github/workflows/nebula.yml +++ b/.github/workflows/nebula.yml @@ -26,6 +26,10 @@ 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: 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 7289fb4..3aa27bb 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitReadCommand.groovy @@ -276,7 +276,6 @@ abstract class RevListCountHead extends GitReadCommand { * ex. git rev-parse HEAD -> 8e6c4c925a54dbe827f043d21cd7a2a01b97fbac */ abstract class RevParseHead extends GitReadCommand { - @Override String obtain() { try { @@ -312,9 +311,13 @@ abstract class GetGitConfigValue extends GitReadCommand { @Override String obtain() { try { - return executeGitCommand( "config", parameters.getGitConfigScope().get(), parameters.getGitConfigKey().get()) + if(parameters.getGitConfigScope().isPresent()) { + return executeGitCommand( "config", parameters.getGitConfigScope().get(), parameters.getGitConfigKey().get()) + } else { + return executeGitCommand( "config", parameters.getGitConfigKey().get()) + } } catch (Exception e) { - logger.debug("Could not get git config {} {} {}", parameters.getGitConfigScope().get(), parameters.getGitConfigKey().get()) + logger.debug("Could not get git config {} {}", parameters.getGitConfigScope().isPresent() ? parameters.gitConfigScope.get() : "", parameters.getGitConfigKey().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 d82d903..6b1c112 100644 --- a/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy +++ b/src/main/groovy/nebula/plugin/release/git/command/GitReadOnlyCommandUtil.groovy @@ -1,6 +1,7 @@ package nebula.plugin.release.git.command import nebula.plugin.release.git.model.TagRef +import org.gradle.api.GradleException import org.gradle.api.provider.Provider import org.gradle.api.provider.ProviderFactory import org.slf4j.Logger @@ -41,7 +42,7 @@ class GitReadOnlyCommandUtil implements Serializable { emailFromLogProvider = providers.of(EmailFromLog.class) { it.parameters.rootDir.set(rootDir) } - configureCommitterIfNecessary() + verifyGitConfig() currentBranchProvider = providers.of(CurrentBranch.class) { it.parameters.rootDir.set(rootDir) } @@ -75,18 +76,22 @@ class GitReadOnlyCommandUtil implements Serializable { } - private void configureCommitterIfNecessary() { + private void verifyGitConfig() { + String username = getGitConfig('user.name') + String email = getGitConfig('user.email') String globalUsername = getGitConfig('--global', 'user.name') String globalEmail = getGitConfig('--global', 'user.email') + String systemUsername = getGitConfig('--system', 'user.name') + String systemEmail = getGitConfig('--system', 'user.email') String localUsername = getGitConfig('--local', 'user.name') String localEmail = getGitConfig('--local', 'user.email') String usernameFromLog = usernameFromLogProvider.isPresent() ? usernameFromLogProvider.get() : null - if(!globalUsername && !localUsername && usernameFromLog) { - setGitConfig("user.name", usernameFromLog) + if(!username && !globalUsername && !localUsername && !systemUsername && usernameFromLog) { + throw new GradleException("Git user.name is not set. Please configure git user.name globally, locally or system wide. You can learn more in https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup") } String emailFromLog = emailFromLogProvider.isPresent() ? emailFromLogProvider.get() : null - if(!globalEmail && !localEmail && emailFromLog) { - setGitConfig("user.email", emailFromLog) + if(!email && !globalEmail && !localEmail && !systemEmail && emailFromLog) { + throw new GradleException("Git user.email is not set. Please configure git user.email globally, locally or system wide. You can learn more in https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup") } } @@ -255,6 +260,23 @@ class GitReadOnlyCommandUtil implements Serializable { logger.debug("Could not get git config {} {} {}", scope, configKey) return null } + } /** + * Returns a git config value for a given scope + * @param configKey + * @return + */ + String getGitConfig(String configKey) { + try { + def getConfigValueProvider = providers.of(GetGitConfigValue.class) { + it.parameters.rootDir.set(rootDir) + it.parameters.gitConfigKey.set(configKey) + } + return getConfigValueProvider.get().toString()?. + replaceAll("\n", "")?.toString() + } catch(Exception e) { + logger.debug("Could not get git config {} {}", configKey) + return null + } } /**