Skip to content

Commit

Permalink
add mechanism to set config committer when unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Oct 23, 2023
1 parent 0edfff5 commit 082f944
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 12 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/nebula.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ import org.gradle.api.provider.ValueSourceParameters
interface GitCommandParameters extends ValueSourceParameters {
Property<File> getRootDir()
Property<String> getTagForSearch()
Property<String> getGitConfigScope()
Property<String> getGitConfigKey()
Property<String> getGitConfigValue()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, GitCommandParameters> {

@Inject
abstract ExecOperations getExecOperations()

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

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GitWriteCommandsUtil implements Serializable {
this.rootDir = gitRoot
}


/**
* Pushes a Tag to a remote repository
* @param remote
Expand Down

0 comments on commit 082f944

Please sign in to comment.