Skip to content

Commit

Permalink
melange bump: optional flag to modify git-checkout pipeline expected-…
Browse files Browse the repository at this point in the history
…commit value

Signed-off-by: James Rawlings <jrawlings@chainguard.dev>
  • Loading branch information
rawlingsj committed Apr 3, 2023
1 parent 2e81bb9 commit 21481b3
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/cli/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
)

func Bump() *cobra.Command {
var expectedCommit string
cmd := &cobra.Command{
Use: "bump",
Short: "Update a Melange YAML file to reflect a new package version",
Expand All @@ -39,6 +40,7 @@ func Bump() *cobra.Command {

bumpRenovator := bump.New(
bump.WithTargetVersion(args[1]),
bump.WithExpectedCommit(expectedCommit),
)

if err := ctx.Renovate(bumpRenovator); err != nil {
Expand All @@ -48,6 +50,6 @@ func Bump() *cobra.Command {
return nil
},
}

cmd.Flags().StringVar(&expectedCommit, "expected-commit", "", "optional flag to update the expected-commit value of a git-checkout pipeline")
return cmd
}
43 changes: 42 additions & 1 deletion pkg/renovate/bump/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import (
// BumpConfig contains the configuration data for a bump
// renovator.
type BumpConfig struct {
TargetVersion string
TargetVersion string
ExpectedCommit string
}

// Option sets a config option on a BumpConfig.
Expand All @@ -47,6 +48,15 @@ func WithTargetVersion(targetVersion string) Option {
}
}

// WithExpectedCommit sets the desired target expected commit for the
// bump renovator.
func WithExpectedCommit(expectedCommit string) Option {
return func(cfg *BumpConfig) error {
cfg.ExpectedCommit = expectedCommit
return nil
}
}

// New returns a renovator which performs a version bump.
func New(opts ...Option) renovate.Renovator {
bcfg := BumpConfig{}
Expand Down Expand Up @@ -99,6 +109,16 @@ func New(opts ...Option) renovate.Renovator {
}
}

// Look for git-checkout nodes.
it = yit.FromNode(pipelineNode).
RecurseNodes().
Filter(yit.WithMapValue("git-checkout"))

for gitCheckoutNode, ok := it(); ok; gitCheckoutNode, ok = it() {
if err := updateGitCheckout(gitCheckoutNode, bcfg.ExpectedCommit); err != nil {
return err
}
}
return nil
}
}
Expand Down Expand Up @@ -155,3 +175,24 @@ func updateFetch(node *yaml.Node, targetVersion string) error {

return nil
}

// updateGitCheckout takes a "git-checkout" pipeline node and updates the parameters of it.
func updateGitCheckout(node *yaml.Node, expectedGitSha string) error {
withNode, err := renovate.NodeFromMapping(node, "with")
if err != nil {
return err
}

log.Printf("processing git-checkout node")

if expectedGitSha != "" {
// Update expected hash nodes.
nodeCommit, err := renovate.NodeFromMapping(withNode, "expected-commit")
if err == nil {
nodeCommit.Value = expectedGitSha
log.Printf(" expected-commit: %s", expectedGitSha)
}
}

return nil
}
42 changes: 42 additions & 0 deletions pkg/renovate/bump/bump_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bump

import (
"chainguard.dev/melange/pkg/build"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -61,6 +62,47 @@ func TestBump_versions(t *testing.T) {

}

func TestBump_withExpectedCommit(t *testing.T) {

dir := t.TempDir()

tests := []struct {
name string
newVersion string
expectedCommit string
}{
{name: "expected_commit.yaml", newVersion: "7.0.1", expectedCommit: "dbd7bc96fd6cd383b8e895dc4a928d808541bb17"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

data, err := os.ReadFile(filepath.Join("testdata", tt.name))
assert.NoError(t, err)

// write the modified melange config to our working temp folder
err = os.WriteFile(filepath.Join(dir, tt.name), data, 0755)
assert.NoError(t, err)

ctx, err := renovate.New(renovate.WithConfig(filepath.Join(dir, tt.name)))
assert.NoError(t, err)

bumpRenovator := New(
WithTargetVersion(tt.newVersion),
WithExpectedCommit(tt.expectedCommit),
)

err = ctx.Renovate(bumpRenovator)
assert.NoError(t, err)

rs, err := build.ParseConfiguration(filepath.Join(dir, tt.name))
assert.NoError(t, err)
assert.Contains(t, rs.Package.Version, tt.newVersion)
assert.Contains(t, rs.Pipeline[0].With["expected-commit"], tt.expectedCommit)

})
}

}
func setupTestServer(t *testing.T) (error, *httptest.Server) {
packageData, err := os.ReadFile(filepath.Join("testdata", "cheese-7.0.1.tar.gz"))
assert.NoError(t, err)
Expand Down
11 changes: 11 additions & 0 deletions pkg/renovate/bump/testdata/expected_commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package:
name: cheese
version: 6.8
epoch: 2
description: "a cheesy library"

pipeline:
- uses: git-checkout
with:
repository: cheese/crisps
expected-commit: dbd7bc96fd6cd383b8e895dc4a928d808541bb17

0 comments on commit 21481b3

Please sign in to comment.