Skip to content

Commit

Permalink
Only verify git user information when creating and pushing tags. fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea authored Feb 14, 2024
1 parent 67169e6 commit 46b9955
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import nebula.plugin.release.git.command.GitReadOnlyCommandUtil
import nebula.plugin.release.git.model.TagRef
import nebula.plugin.release.git.opinion.TimestampUtil
import nebula.plugin.release.git.semver.NearestVersionLocator
import nebula.plugin.release.util.ReleaseTasksUtil
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.slf4j.Logger
Expand All @@ -37,14 +38,13 @@ class OverrideStrategies {


static class ReleaseLastTagStrategy implements VersionStrategy {
static final String PROPERTY_NAME = 'release.useLastTag'
private static final String NOT_SUPPLIED = 'release-strategy-is-not-supplied'
private static final Logger logger = LoggerFactory.getLogger(ReleaseLastTagStrategy)

Project project
String propertyName

ReleaseLastTagStrategy(Project project, String propertyName = PROPERTY_NAME) {
ReleaseLastTagStrategy(Project project, String propertyName = ReleaseTasksUtil.USE_LAST_TAG_PROPERTY) {
this.project = project
this.propertyName = propertyName
}
Expand Down
26 changes: 6 additions & 20 deletions src/main/groovy/nebula/plugin/release/ReleasePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,13 @@ import org.gradle.process.ExecOperations

import javax.inject.Inject

import static nebula.plugin.release.util.ReleaseTasksUtil.*

class ReleasePlugin implements Plugin<Project> {
public static final String DISABLE_GIT_CHECKS = 'release.disableGitChecks'
public static final String DEFAULT_VERSIONING_STRATEGY = 'release.defaultVersioningStrategy'
Project project
static Logger logger = Logging.getLogger(ReleasePlugin)

static final String SNAPSHOT_TASK_NAME = 'snapshot'
static final String SNAPSHOT_TASK_NAME_OPTIONAL_COLON = ":$SNAPSHOT_TASK_NAME"
static final String SNAPSHOT_SETUP_TASK_NAME = 'snapshotSetup'
static final String DEV_SNAPSHOT_TASK_NAME = 'devSnapshot'
static final String DEV_SNAPSHOT_SETUP_TASK_NAME = 'devSnapshotSetup'
static final String DEV_SNAPSHOT_SETUP_TASK_NAME_OPTIONAL_COLON = ":$DEV_SNAPSHOT_SETUP_TASK_NAME"
static final String IMMUTABLE_SNAPSHOT_TASK_NAME = 'immutableSnapshot'
static final String IMMUTABLE_SNAPSHOT_SETUP_TASK_NAME = 'immutableSnapshotSetup'
static final String IMMUTABLE_SNAPSHOT_TASK_NAME_OPTIONAL_COLON = ":$IMMUTABLE_SNAPSHOT_TASK_NAME"
static final String CANDIDATE_TASK_NAME = 'candidate'
static final String CANDIDATE_TASK_NAME_OPTIONAL_COLON = ":$CANDIDATE_TASK_NAME"
static final String CANDIDATE_SETUP_TASK_NAME = 'candidateSetup'
static final String FINAL_TASK_NAME = 'final'
static final String FINAL_TASK_NAME_WITH_OPTIONAL_COLON = ":$FINAL_TASK_NAME"
static final String FINAL_SETUP_TASK_NAME = 'finalSetup'
static final String RELEASE_CHECK_TASK_NAME = 'releaseCheck'
static final String NEBULA_RELEASE_EXTENSION_NAME = 'nebulaRelease'
static final String POST_RELEASE_TASK_NAME = 'postRelease'
static final String GROUP = 'Nebula Release'

private final GitReadOnlyCommandUtil gitCommandUtil
Expand All @@ -85,7 +68,10 @@ class ReleasePlugin implements Plugin<Project> {
this.project = project

File gitRoot = project.hasProperty('git.root') ? new File(project.property('git.root')) : project.rootProject.projectDir
gitCommandUtil.configure(gitRoot)

// Verify user git config only when using release tags and 'release.useLastTag' property is not used
boolean shouldVerifyUserGitConfig = isReleaseTaskThatRequiresTagging(project.gradle.startParameter.taskNames) && !isUsingLatestTag(project)
gitCommandUtil.configure(gitRoot, shouldVerifyUserGitConfig)
gitWriteCommandsUtil.configure(gitRoot)

boolean isGitRepo = gitCommandUtil.isGitRepo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ class GitReadOnlyCommandUtil implements Serializable {
* This uses the provider factory to create the providers in order to support ocnfiguration cache
* @param gitRoot
*/
void configure(File gitRoot) {
void configure(File gitRoot, boolean shouldVerifyUserGitConfig) {
this.rootDir = gitRoot
usernameFromLogProvider = providers.of(UsernameFromLog.class) {
it.parameters.rootDir.set(rootDir)
}
emailFromLogProvider = providers.of(EmailFromLog.class) {
it.parameters.rootDir.set(rootDir)
}
verifyGitConfig()
if(shouldVerifyUserGitConfig) {
verifyUserGitConfig()
}
currentBranchProvider = providers.of(CurrentBranch.class) {
it.parameters.rootDir.set(rootDir)
}
Expand Down Expand Up @@ -76,7 +78,7 @@ class GitReadOnlyCommandUtil implements Serializable {

}

private void verifyGitConfig() {
private void verifyUserGitConfig() {
String username = getGitConfig('user.name')
String email = getGitConfig('user.email')
String globalUsername = getGitConfig('--global', 'user.name')
Expand Down Expand Up @@ -250,7 +252,9 @@ 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
Expand Down
37 changes: 37 additions & 0 deletions src/main/groovy/nebula/plugin/release/util/ReleaseTasksUtil.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nebula.plugin.release.util

import org.gradle.api.Project

class ReleaseTasksUtil {

static final String SNAPSHOT_TASK_NAME = 'snapshot'
static final String SNAPSHOT_TASK_NAME_OPTIONAL_COLON = ":$SNAPSHOT_TASK_NAME"
static final String SNAPSHOT_SETUP_TASK_NAME = 'snapshotSetup'
static final String DEV_SNAPSHOT_TASK_NAME = 'devSnapshot'
static final String DEV_SNAPSHOT_SETUP_TASK_NAME = 'devSnapshotSetup'
static final String DEV_SNAPSHOT_TASK_NAME_OPTIONAL_COLON = ":$DEV_SNAPSHOT_TASK_NAME"
static final String DEV_SNAPSHOT_SETUP_TASK_NAME_OPTIONAL_COLON = ":$DEV_SNAPSHOT_SETUP_TASK_NAME"
static final String IMMUTABLE_SNAPSHOT_TASK_NAME = 'immutableSnapshot'
static final String IMMUTABLE_SNAPSHOT_SETUP_TASK_NAME = 'immutableSnapshotSetup'
static final String IMMUTABLE_SNAPSHOT_TASK_NAME_OPTIONAL_COLON = ":$IMMUTABLE_SNAPSHOT_TASK_NAME"
static final String CANDIDATE_TASK_NAME = 'candidate'
static final String CANDIDATE_TASK_NAME_OPTIONAL_COLON = ":$CANDIDATE_TASK_NAME"
static final String CANDIDATE_SETUP_TASK_NAME = 'candidateSetup'
static final String FINAL_TASK_NAME = 'final'
static final String FINAL_TASK_NAME_WITH_OPTIONAL_COLON = ":$FINAL_TASK_NAME"
static final String FINAL_SETUP_TASK_NAME = 'finalSetup'
static final String RELEASE_CHECK_TASK_NAME = 'releaseCheck'
static final String NEBULA_RELEASE_EXTENSION_NAME = 'nebulaRelease'
static final String POST_RELEASE_TASK_NAME = 'postRelease'
static final String USE_LAST_TAG_PROPERTY = 'release.useLastTag'

static boolean isUsingLatestTag(Project project) {
return project.hasProperty(USE_LAST_TAG_PROPERTY) && project.property(USE_LAST_TAG_PROPERTY).toString().toBoolean()
}

static boolean isReleaseTaskThatRequiresTagging(List<String> cliTasks) {
def hasCandidate = cliTasks.contains(CANDIDATE_TASK_NAME) || cliTasks.contains(CANDIDATE_TASK_NAME_OPTIONAL_COLON)
def hasFinal = cliTasks.contains(FINAL_TASK_NAME) || cliTasks.contains(FINAL_TASK_NAME_WITH_OPTIONAL_COLON)
return hasCandidate || hasFinal
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package nebula.plugin.release.util

import nebula.test.ProjectSpec
import spock.lang.Unroll

class ReleaseTasksUtilSpec extends ProjectSpec {

def 'verify isUsingLatestTag reads the project property accordingly'() {
given:
project

expect:
!ReleaseTasksUtil.isUsingLatestTag(project)

when:
project.ext.'release.useLastTag' = false

then:
!ReleaseTasksUtil.isUsingLatestTag(project)

when:
project.ext.'release.useLastTag' = true

then:
ReleaseTasksUtil.isUsingLatestTag(project)
}

@Unroll
def 'checking release task for #tasks results in #expected'() {
expect:
ReleaseTasksUtil.isReleaseTaskThatRequiresTagging(tasks) == expected

where:
tasks || expected
['build'] || false
['build', 'devSnapshot'] || false
['build', 'candidate'] || true
['build', 'immutableSnapshot'] || false
['build', 'final'] || true
[':devSnapshot'] || false
['devSnapshot'] || false
[':snapshot'] || false
['snapshot'] || false
['candidate'] || true
[':candidate'] || true
['immutableSnapshot'] || false
[':immutableSnapshot'] || false
['final'] || true
[':final'] || true
}

}

0 comments on commit 46b9955

Please sign in to comment.