Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

melange bump: optional flag to modify git-checkout pipeline expected-… #367

Merged
merged 1 commit into from
Apr 3, 2023
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
3 changes: 2 additions & 1 deletion docs/md/melange_bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ melange bump [flags]
### Options

```
-h, --help help for bump
--expected-commit string optional flag to update the expected-commit value of a git-checkout pipeline
-h, --help help for bump
```

### SEE ALSO
Expand Down
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"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be a single test that starts with what's currently at like 79? If there's only one case here, it's probably not worth the table-driven scaffolding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test was generated from the IDE and does make it easier for folks adding new cases but TBH I don't mind either way. Happy to switch it to a single test if you prefer. FWIW as this is merged I have opened a second PR to discuss as I did noticed that the test data meant the test didn't actually verify the new sha was correct. #368

}
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
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
expected-commit: dbd7bc96fd6cd383b8e895dc4a928d808541bb17