Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Limit git commit message to first 10 images #3140

Merged
merged 3 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions pkg/update/automated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
75 changes: 75 additions & 0 deletions pkg/update/automated_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}