diff --git a/pkg/update/automated.go b/pkg/update/automated.go index e7e2ae965..e4bc129e3 100644 --- a/pkg/update/automated.go +++ b/pkg/update/automated.go @@ -60,19 +60,28 @@ func (a *Automated) ReleaseKind() ReleaseKind { func (a *Automated) CommitMessage(result Result) string { images := result.ChangedImages() buf := &bytes.Buffer{} - prefix := "" - switch len(images) { + + switch total := len(images); total { case 0: // FIXME(michael): can we get here? fmt.Fprintln(buf, "Auto-release (no images)") + case 1: - fmt.Fprint(buf, "Auto-release ") + fmt.Fprintf(buf, "Auto-release %s", images[0]) + default: - fmt.Fprintln(buf, "Auto-release multiple images") - fmt.Fprintln(buf) - prefix = " - " - } - for _, im := range images { - fmt.Fprintf(buf, "%s%s\n", prefix, im) + limit := 10 + + fmt.Fprintf(buf, "Auto-release multiple (%d) images\n\n", total) + if total > limit { + // Take first 10 images to keep commit message size in bounds + images = images[:limit] + } + for _, im := range images { + fmt.Fprintf(buf, " - %s\n", im) + } + if total > limit { + fmt.Fprintln(buf, " ...") + } } return buf.String() } diff --git a/pkg/update/automated_test.go b/pkg/update/automated_test.go new file mode 100644 index 000000000..740eab6ee --- /dev/null +++ b/pkg/update/automated_test.go @@ -0,0 +1,75 @@ +package update + +import ( + "testing" + + "github.com/fluxcd/flux/pkg/resource" +) + +func TestCommitMessage(t *testing.T) { + automated := Automated{} + result := Result{ + resource.MakeID("ns", "kind", "1"): { + Status: ReleaseStatusSuccess, + PerContainer: []ContainerUpdate{ + {Target: mustParseRef("docker.io/image:v1")}, + {Target: mustParseRef("docker.io/image:v2")}, + {Target: mustParseRef("docker.io/image:v3")}, + }, + }, + } + result.ChangedImages() + + actual := automated.CommitMessage(result) + expected := `Auto-release multiple (3) images + + - docker.io/image:v1 + - docker.io/image:v2 + - docker.io/image:v3 +` + if actual != expected { + t.Fatalf("Expected git commit message: '%s', was '%s'", expected, actual) + } +} + +func TestCommitMessage10Max(t *testing.T) { + automated := Automated{} + result := Result{ + resource.MakeID("ns", "kind", "1"): { + Status: ReleaseStatusSuccess, + PerContainer: []ContainerUpdate{ + {Target: mustParseRef("docker.io/image:v1")}, + {Target: mustParseRef("docker.io/image:v2")}, + {Target: mustParseRef("docker.io/image:v3")}, + {Target: mustParseRef("docker.io/image:v4")}, + {Target: mustParseRef("docker.io/image:v5")}, + {Target: mustParseRef("docker.io/image:v6")}, + {Target: mustParseRef("docker.io/image:v7")}, + {Target: mustParseRef("docker.io/image:v8")}, + {Target: mustParseRef("docker.io/image:v9")}, + {Target: mustParseRef("docker.io/image:v10")}, + {Target: mustParseRef("docker.io/image:v11")}, + }, + }, + } + result.ChangedImages() + + actual := automated.CommitMessage(result) + expected := `Auto-release multiple (11) images + + - docker.io/image:v1 + - docker.io/image:v10 + - docker.io/image:v11 + - docker.io/image:v2 + - docker.io/image:v3 + - docker.io/image:v4 + - docker.io/image:v5 + - docker.io/image:v6 + - docker.io/image:v7 + - docker.io/image:v8 + ... +` + if actual != expected { + t.Fatalf("Expected git commit message: '%s', was '%s'", expected, actual) + } +}