Skip to content

Commit

Permalink
fix: escape special chars in commit title / message and set default v…
Browse files Browse the repository at this point in the history
…alues for empty repos
  • Loading branch information
PhilippHeuer committed Jan 6, 2020
1 parent 94cc4d8 commit f3d7bd7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
44 changes: 22 additions & 22 deletions pkg/common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,32 @@ func GetSCMArguments(projectDir string) []string {
log.Debug("Using git repository at " + projectDir)
repository, repositoryErr := git.PlainOpen(projectDir)
if repositoryErr != nil {
log.Warn("No repository!")
return getDefaultInfo()
info = append(info, "NCI_REPOSITORY_KIND=none")
info = append(info, "NCI_REPOSITORY_REMOTE=local")
info = append(info, "NCI_COMMIT_REF_TYPE=unknown")
info = append(info, "NCI_COMMIT_REF_NAME=unknown")
info = append(info, "NCI_COMMIT_REF_SLUG=unknown")
info = append(info, "NCI_COMMIT_REF_RELEASE=unknown")
info = append(info, "NCI_COMMIT_SHA=")
info = append(info, "NCI_COMMIT_SHA_SHORT=")
info = append(info, "NCI_COMMIT_TITLE=")
info = append(info, "NCI_COMMIT_DESCRIPTION=")
return info
}

// get current reference
ref, refErr := repository.Head()
if refErr != nil {
log.Warn("Empty repository!")
return getDefaultInfo()
info = append(info, "NCI_REPOSITORY_KIND=none")
info = append(info, "NCI_REPOSITORY_REMOTE=local")
info = append(info, "NCI_COMMIT_REF_TYPE=unknown")
info = append(info, "NCI_COMMIT_REF_NAME=unknown")
info = append(info, "NCI_COMMIT_REF_SLUG=unknown")
info = append(info, "NCI_COMMIT_REF_RELEASE=unknown")
info = append(info, "NCI_COMMIT_SHA=")
info = append(info, "NCI_COMMIT_SHA_SHORT=")
info = append(info, "NCI_COMMIT_TITLE=")
info = append(info, "NCI_COMMIT_DESCRIPTION=")
return info
}
log.Debug("Git Ref " + ref.String())

Expand Down Expand Up @@ -147,20 +164,3 @@ func readLastLine(filename string) string {

return lastLine
}

func getDefaultInfo() []string {
var info []string

info = append(info, "NCI_REPOSITORY_KIND=none")
info = append(info, "NCI_REPOSITORY_REMOTE=local")
info = append(info, "NCI_COMMIT_REF_TYPE=unknown")
info = append(info, "NCI_COMMIT_REF_NAME=unknown")
info = append(info, "NCI_COMMIT_REF_SLUG=unknown")
info = append(info, "NCI_COMMIT_REF_RELEASE=unknown")
info = append(info, "NCI_COMMIT_SHA=")
info = append(info, "NCI_COMMIT_SHA_SHORT=")
info = append(info, "NCI_COMMIT_TITLE=")
info = append(info, "NCI_COMMIT_DESCRIPTION=")

return info
}
31 changes: 10 additions & 21 deletions pkg/common/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,31 @@ package common

import (
"testing"
"io/ioutil"
"os"

"gopkg.in/src-d/go-git.v4"
)

func TestOnInvalidGitDirectory(t *testing.T) {
var scmArgs = GetSCMArguments(GetGitDirectory()+"/../invalidpath")

// log all normalized values
var scmArgs = GetSCMArguments(GetGitDirectory()+"/tmp/invaliddir")
for _, envvar := range scmArgs {
t.Log(envvar)
}
}

func TestOnEmptyGitRepository(t *testing.T) {
// create empty repo
dir, dirErr := ioutil.TempDir("", "normalizeci")
if dirErr != nil {
t.Errorf(dirErr.Error())
}

// init empty repo
_, gitErr := git.PlainInit(dir, false)
if gitErr != nil {
t.Errorf(gitErr.Error())
// create git repo
var tmpDir = GetGitDirectory()+"/tmp/empty"
os.RemoveAll(GetGitDirectory()+"/tmp/empty")
_, err := git.PlainInit(tmpDir, true)
if err != nil {
t.Errorf(err.Error())
}

// call func
var scmArgs = GetSCMArguments(dir)

// log all normalized values
var scmArgs = GetSCMArguments(tmpDir)
for _, envvar := range scmArgs {
t.Log("Args:"+envvar)
t.Log(envvar)
}

// clean up
defer os.RemoveAll(dir)
os.RemoveAll(GetGitDirectory()+"/tmp/empty")
}
4 changes: 2 additions & 2 deletions src/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func main() {
err := os.Setenv(entrySplit[0], entrySplit[1])
common.CheckForError(err)

// print via stdout
s := fmt.Sprintf("export %s=\"%s\"\n", entrySplit[0], entrySplit[1])
// print via stdout and escape values
s := fmt.Sprintf("export %s=\"%s\"\n", entrySplit[0], strings.ReplaceAll(entrySplit[1], "\"", "\\\""))
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
}

0 comments on commit f3d7bd7

Please sign in to comment.