Skip to content

Commit

Permalink
Merge pull request #201 from fluxcd/fix/parse-revision
Browse files Browse the repository at this point in the history
Fix revision parsing when branch contains slash
  • Loading branch information
stefanprodan authored May 25, 2021
2 parents 337b5ab + df752f0 commit a144e3a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
9 changes: 4 additions & 5 deletions internal/notifier/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,14 @@ func splitCamelcase(src string) (entries []string) {

func parseRevision(rev string) (string, error) {
comp := strings.Split(rev, "/")
if len(comp) != 2 {
if len(comp) < 2 {
return "", fmt.Errorf("Revision string format incorrect: %v", rev)
}

if comp[1] == "" {
sha := comp[len(comp)-1]
if sha == "" {
return "", fmt.Errorf("Commit Sha cannot be empty: %v", rev)
}

return comp[1], nil
return sha, nil
}

func isCommitStatus(meta map[string]string, status string) bool {
Expand Down
19 changes: 13 additions & 6 deletions internal/notifier/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,23 @@ func TestUtil_ParseRevision(t *testing.T) {
require.Equal(t, "a1afe267b54f38b46b487f6e938a6fd508278c07", rev)
}

func TestUtil_ParseRevisionTooFewComponents(t *testing.T) {
revString := "master/"
func TestUtil_ParseRevisionNestedBranch(t *testing.T) {
revString := "environment/dev/a1afe267b54f38b46b487f6e938a6fd508278c07"
rev, err := parseRevision(revString)
require.NoError(t, err)
require.Equal(t, "a1afe267b54f38b46b487f6e938a6fd508278c07", rev)
}

func TestUtil_ParseRevisionOneComponents(t *testing.T) {
revString := "master"
_, err := parseRevision(revString)
require.Error(t, err)
require.EqualError(t, err, "Revision string format incorrect: master")
}

func TestUtil_ParseRevisionTooManyComponents(t *testing.T) {
revString := "master/a1afe267b54f38b46b487f6e938a6fd508278c07/foo/bar"
func TestUtil_ParseRevisionTooFewComponents(t *testing.T) {
revString := "master/"
_, err := parseRevision(revString)
require.Error(t, err)
require.EqualError(t, err, "Commit Sha cannot be empty: master/")
}

func TestUtil_ParseGitHttps(t *testing.T) {
Expand Down

0 comments on commit a144e3a

Please sign in to comment.