Skip to content

Commit

Permalink
allow to use precommit closure in commitChanges (kiegroup#115)
Browse files Browse the repository at this point in the history
* allow to use precommit closure in `commitChanges`

* review update

* update after review comment
  • Loading branch information
radtriste authored Nov 27, 2020
1 parent 83a587b commit 90373cd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
18 changes: 18 additions & 0 deletions test/vars/GithubScmSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ class GithubScmSpec extends JenkinsPipelineSpecification {
1 * getPipelineMock("sh")("git commit -m 'commit message'")
}

def "[githubscm.groovy] commitChanges with precommit closure"() {
when:
groovyScript.commitChanges('commit message', {
sh 'whatever'
})
then:
1 * getPipelineMock("sh")('whatever')
1 * getPipelineMock("sh")("git commit -m 'commit message'")
}

def "[githubscm.groovy] commitChanges without precommit closure"() {
when:
groovyScript.commitChanges('commit message')
then:
1 * getPipelineMock("sh")("git commit -m 'commit message'")
}


def "[githubscm.groovy] forkRepo without credentials"() {
setup:
groovyScript.getBinding().setVariable("GITHUB_USER", 'user')
Expand Down
27 changes: 25 additions & 2 deletions vars/githubscm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ def createBranch(String branchName) {
println "[INFO] Created branch '${branchName}' on repo."
}

def commitChanges(String commitMessage, String filesToAdd = '--all') {
sh "git add ${filesToAdd}"
def commitChanges(String commitMessage, Closure preCommit) {
preCommit()
sh "git commit -m '${commitMessage}'"
}

def commitChanges(String commitMessage, String filesToAdd = '--all') {
commitChanges(commitMessage, { sh "git add ${filesToAdd}" })
}

def forkRepo(String credentialID = 'kie-ci') {
cleanHubAuth()
withCredentials([usernamePassword(credentialsId: credentialID, usernameVariable: 'GITHUB_USER', passwordVariable: 'GITHUB_TOKEN')]) {
Expand Down Expand Up @@ -243,3 +247,22 @@ def getForkedProjectName(String group, String repository, String owner, String c
def cleanHubAuth() {
sh "rm -rf ~/.config/hub"
}

/**
* Uses `find` command to stage all files matching the pattern and which are not in .gitignore
*/
def findAndStageNotIgnoredFiles(String findNamePattern){
// based on https://stackoverflow.com/a/59888964/8811872
sh """
find . -type f -name '${findNamePattern}' > found_files.txt
files_to_add=""
while IFS= read -r file; do
if ! git check-ignore -q "\$file"; then
files_to_add="\$files_to_add \$file"
fi
done < found_files.txt
rm found_files.txt
git add \$files_to_add
git status
"""
}

0 comments on commit 90373cd

Please sign in to comment.