Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
pre/post update scripts
Browse files Browse the repository at this point in the history
I want this for code generation, like regenerating protobufs when
`protoc` is updated.
  • Loading branch information
thepwagner committed Oct 8, 2020
1 parent e41197d commit 743e47f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
10 changes: 6 additions & 4 deletions updater/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ type Group struct {

// Parameters that apply to members:
// Range is a comma separated list of allowed semver ranges
Range string `yaml:"range"`
CoolDown string `yaml:"cooldown"`
Range string `yaml:"range"`
CoolDown string `yaml:"cooldown"`
PreScript string `yaml:"pre-script"`
PostScript string `yaml:"post-script"`

compiledPattern *regexp.Regexp
}
Expand Down Expand Up @@ -48,7 +50,7 @@ func (g *Group) Validate() error {
return nil
}

func (g Group) InRange(v string) bool {
func (g *Group) InRange(v string) bool {
for _, rangeCond := range strings.Split(g.Range, ",") {
rangeCond = strings.TrimSpace(rangeCond)
switch {
Expand Down Expand Up @@ -90,7 +92,7 @@ const (
oneDay = 24 * time.Hour
)

func (g Group) CoolDownDuration() time.Duration {
func (g *Group) CoolDownDuration() time.Duration {
m := durPattern.FindStringSubmatch(g.CoolDown)

var ret time.Duration
Expand Down
5 changes: 3 additions & 2 deletions updater/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ func TestGroup_InRange(t *testing.T) {

for r, tc := range cases {
t.Run(r, func(t *testing.T) {
u := &updater.Group{Range: r}
for _, v := range tc.included {
t.Run(fmt.Sprintf("includes %s", v), func(t *testing.T) {
assert.True(t, updater.Group{Range: r}.InRange(v))
assert.True(t, u.InRange(v))
})
}
for _, v := range tc.excluded {
t.Run(fmt.Sprintf("excludes %q", v), func(t *testing.T) {
assert.False(t, updater.Group{Range: r}.InRange(v))
assert.False(t, u.InRange(v))
})
}
})
Expand Down
27 changes: 27 additions & 0 deletions updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package updater
import (
"context"
"fmt"
"os"
"os/exec"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -214,14 +216,39 @@ func (u *RepoUpdater) groupedUpdate(ctx context.Context, log logrus.FieldLogger,
return 0, fmt.Errorf("switching to target branch: %w", err)
}

if err := u.updateScript(ctx, "pre", group.PreScript); err != nil {
return 0, fmt.Errorf("executing pre-update script: %w", err)
}

for _, update := range updates {
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
return 0, fmt.Errorf("applying batched update: %w", err)
}
}

if err := u.updateScript(ctx, "post", group.PostScript); err != nil {
return 0, fmt.Errorf("executing pre-update script: %w", err)
}

if err := u.repo.Push(ctx, updates...); err != nil {
return 0, fmt.Errorf("pushing update: %w", err)
}
return len(updates), nil
}

func (u *RepoUpdater) updateScript(ctx context.Context, label, script string) error {
if script == "" {
return nil
}
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", script)
cmd.Dir = u.repo.Root()
out := os.Stdout
_, _ = fmt.Fprintf(out, "--- start %s update script ---\n", label)
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return err
}
_, _ = fmt.Fprintf(out, "--- end %s update script ---\n", label)
return nil
}
46 changes: 46 additions & 0 deletions updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package updater_test
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -120,3 +122,47 @@ func TestRepoUpdater_UpdateAll_MultipleGrouped(t *testing.T) {
r.AssertExpectations(t)
u.AssertExpectations(t)
}

func TestRepoUpdater_UpdateAll_Scripts(t *testing.T) {
cases := []*updater.Group{
{
Name: groupName,
Pattern: "github.com/foo",
PreScript: `echo "sup" && touch token`,
},
{
Name: groupName,
Pattern: "github.com/foo",
PostScript: `echo "sup" && touch token`,
},
}

for _, group := range cases {
err := group.Validate()
require.NoError(t, err)

tmpDir := t.TempDir()
tokenPath := filepath.Join(tmpDir, "token")
r := &mockRepo{}
u := &mockUpdater{}
ru := updater.NewRepoUpdater(r, u, updater.WithGroups(group))
ctx := context.Background()

r.On("SetBranch", baseBranch).Return(nil)
dep := updater.Dependency{Path: mockUpdate.Path, Version: mockUpdate.Previous}
u.On("Dependencies", ctx).Return([]updater.Dependency{dep}, nil)
availableUpdate := mockUpdate // avoid pointer to shared reference
u.On("Check", ctx, dep, mock.Anything).Return(&availableUpdate, nil)
r.On("NewBranch", baseBranch, "action-update-go/main/foo").Times(1).Return(nil)
u.On("ApplyUpdate", ctx, mock.Anything).Times(1).Return(nil)
r.On("Push", ctx, mock.Anything, mock.Anything).Times(1).Return(nil)
r.On("Root").Return(tmpDir)

err = ru.UpdateAll(ctx, baseBranch)
require.NoError(t, err)
r.AssertExpectations(t)
u.AssertExpectations(t)
_, err = os.Stat(tokenPath)
require.NoError(t, err)
}
}

0 comments on commit 743e47f

Please sign in to comment.