Repository: osbuild/osbuild-composer
Pull request #139 (2019-12-04 00:55:58)

weldr: bump blueprint version
According to the same rules as lorax-compposer:

  https://weldr.io/lorax/lorax-composer.html#blueprints


Patch:
diff --git a/internal/store/store.go b/internal/store/store.go
index 8fc57dc71..285638699 100644
--- a/internal/store/store.go
+++ b/internal/store/store.go
@@ -6,11 +6,14 @@ import (
 	"crypto/sha1"
 	"encoding/hex"
 	"encoding/json"
+	"fmt"
 	"io/ioutil"
 	"log"
 	"os"
 	"path/filepath"
 	"sort"
+	"strconv"
+	"strings"
 	"sync"
 	"time"
 
@@ -330,6 +333,21 @@ func (s *Store) GetBlueprintChanges(name string) []blueprint.Change {
 	return changes
 }
 
+func bumpVersion(str string) string {
+	v := [3]uint64{}
+	fields := strings.SplitN(str, ".", 3)
+	for i := 0; i < len(fields); i++ {
+		if n, err := strconv.ParseUint(fields[i], 10, 64); err == nil {
+			v[i] = n
+		} else {
+			// don't touch strings with invalid versions
+			return str
+		}
+	}
+
+	return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2] + 1)
+}
+
 func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) {
 	s.change(func() error {
 		hash := sha1.New()
@@ -352,6 +370,12 @@ func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) {
 			s.BlueprintsChanges[bp.Name] = make(map[string]blueprint.Change)
 		}
 		s.BlueprintsChanges[bp.Name][commit] = change
+
+		if old, ok := s.Blueprints[bp.Name]; ok {
+			if bp.Version == "" || bp.Version == old.Version {
+				bp.Version = bumpVersion(old.Version)
+			}
+		}
 		s.Blueprints[bp.Name] = bp
 		return nil
 	})


Test patch:
diff --git a/internal/store/store_test.go b/internal/store/store_test.go
new file mode 100644
index 000000000..be7b3a4b3
--- /dev/null
+++ b/internal/store/store_test.go
@@ -0,0 +1,30 @@
+package store
+
+import (
+	"testing"
+)
+
+func TestBumpVersion(t *testing.T) {
+	cases := []struct {
+		Version  string
+		Expected string
+	}{
+		{"", ""},
+		{"0", "0.0.1"},
+		{"0.0", "0.0.1"},
+		{"0.0.0", "0.0.1"},
+		{"2.1.3", "2.1.4"},
+
+		// don't touch invalid version strings
+		{"0.0.0.0", "0.0.0.0"},
+		{"0.a.0", "0.a.0"},
+		{"foo", "foo"},
+	}
+
+	for _, c := range cases {
+		result := bumpVersion(c.Version)
+		if result != c.Expected {
+			t.Errorf("bumpVersion(%#v) is expected to return %#v, but instead returned %#v", c.Version, c.Expected, result)
+		}
+	}
+}
diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go
index d3f244b75..04e9c9314 100644
--- a/internal/weldr/api_test.go
+++ b/internal/weldr/api_test.go
@@ -184,7 +184,7 @@ func TestBlueprintsFreeze(t *testing.T) {
 		ExpectedStatus int
 		ExpectedJSON   string
 	}{
-		{rpmmd_mock.BaseFixture, "/api/v0/blueprints/freeze/test", http.StatusOK, `{"blueprints":[{"blueprint":{"name":"test","description":"Test","version":"0.0.0","packages":[{"name":"dep-package1","version":"1.33-2.fc30.x86_64"}],"modules":[],"groups":[]}}],"errors":[]}`},
+		{rpmmd_mock.BaseFixture, "/api/v0/blueprints/freeze/test", http.StatusOK, `{"blueprints":[{"blueprint":{"name":"test","description":"Test","version":"0.0.1","packages":[{"name":"dep-package1","version":"1.33-2.fc30.x86_64"}],"modules":[],"groups":[]}}],"errors":[]}`},
 	}
 
 	for _, c := range cases {