Skip to content

Commit

Permalink
Git config verification: avoid mutating local git config and throw ex…
Browse files Browse the repository at this point in the history
…ception instead
  • Loading branch information
rpalcolea committed Jan 8, 2024
1 parent 95d2f18 commit 2dbd262
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/nebula.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ abstract class RevListCountHead extends GitReadCommand {
* ex. git rev-parse HEAD -> 8e6c4c925a54dbe827f043d21cd7a2a01b97fbac
*/
abstract class RevParseHead extends GitReadCommand {

@Override
String obtain() {
try {
Expand Down Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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
}
}

/**
Expand Down

0 comments on commit 2dbd262

Please sign in to comment.