Skip to content

Commit

Permalink
Fix verify workflow (#167)
Browse files Browse the repository at this point in the history
This fixes the DB migration check in the verify workflow to allow reverting migration files.
  • Loading branch information
jimbishopp authored Oct 6, 2023
1 parent ba1ff23 commit 2f50017
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bot/internal/bot/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func (b *Bot) verifyDBMigration(ctx context.Context, pathPrefix string) error {
return nil
}

// don't evaluate removed files
prFiles = filterSlice(prFiles, func(f github.PullRequestFile) bool {
return f.Status != "removed"
})

// parse PR migration file ids
// 202301031500_subscription-alter.up.sql => 202301031500
prIDs, err := parseMigrationFileIDs(pathPrefix, pullRequestFileNames(prFiles))
Expand Down Expand Up @@ -176,3 +181,15 @@ func pullRequestFileNames(files []github.PullRequestFile) []string {
}
return names
}

// filterSlice filters a slice returning a slice of the original items
// excluding those itens where filterfunc returns false.
func filterSlice[T any](ts []T, filterfunc func(T) bool) []T {
p := make([]T, 0, len(ts))
for i := range ts {
if filterfunc(ts[i]) {
p = append(p, ts[i])
}
}
return p
}
20 changes: 19 additions & 1 deletion bot/internal/bot/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func TestParseMigrationFileIDs(t *testing.T) {
}

func TestVerifyCloudDBMigration(t *testing.T) {
const defaultStatus = "added"

// load fake github with noop data
fgh := &fakeGithub{ref: github.Reference{Name: "foo", SHA: "abc"}}
fgh.files = []github.PullRequestFile{{Name: "foo.go"}, {Name: "pkg/lib/foo.go"}}
Expand All @@ -113,6 +115,7 @@ func TestVerifyCloudDBMigration(t *testing.T) {
cases := []struct {
prFiles []string
branchFiles []string
status string
expectErr bool
}{
{}, // 0 no migration files in branch or pr
Expand Down Expand Up @@ -145,11 +148,26 @@ func TestVerifyCloudDBMigration(t *testing.T) {
},
expectErr: true,
},
{ // 5 OK filter removed files
status: "removed",
prFiles: []string{
"db/202301031501_fake.up.sql",
},
branchFiles: []string{
"db/202301031501_exists.up.sql",
},
},
}
fghBaseline := *fgh
for i, test := range cases {
if test.status == "" {
test.status = defaultStatus
}
for _, f := range test.prFiles {
fgh.files = append(fgh.files, github.PullRequestFile{Name: f})
fgh.files = append(fgh.files, github.PullRequestFile{
Name: f,
Status: test.status,
})
}
fgh.commitFiles = append(fgh.commitFiles, test.branchFiles...)

Expand Down
3 changes: 3 additions & 0 deletions bot/internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ type PullRequestFile struct {
Additions int
// Deletions is the number of lines removed from the file
Deletions int
// Status is either added, removed, modified, renamed, copied, changed, unchanged
Status string
}

// PullRequestFiles is a list of pull request files.
Expand Down Expand Up @@ -411,6 +413,7 @@ func (c *Client) ListFiles(ctx context.Context, organization string, repository
Name: file.GetFilename(),
Additions: file.GetAdditions(),
Deletions: file.GetDeletions(),
Status: file.GetStatus(),
})
}

Expand Down

0 comments on commit 2f50017

Please sign in to comment.