-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move migration support to provider-ci (#1150)
Move away from the typescript scripts which are only run during push but not from the `make ci-mgmt` target. This ensures migrations are run in both the pull and push styles consistently. - Migrate existing migrations. - Remove old typescript code. - Remove old step in CI. - Skip running migrations for local test-providers as they have no code in them and will fail most of the time.
- Loading branch information
1 parent
915e113
commit 4af8e48
Showing
14 changed files
with
213 additions
and
916 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package migrations | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
//go:embed fixupBridgeImports.patch | ||
var fixupBridgeImportsPatch string | ||
|
||
type fixupBridgeImports struct{} | ||
|
||
func (fixupBridgeImports) Name() string { | ||
return "Fixup Bridge Imports" | ||
} | ||
func (fixupBridgeImports) ShouldRun(templateName string) bool { | ||
return templateName == "bridged-provider" | ||
} | ||
func (fixupBridgeImports) Migrate(templateName, outDir string) error { | ||
path, cleanup, err := writeTempFile("fixupBridgeImports.patch", fixupBridgeImportsPatch) | ||
if err != nil { | ||
return fmt.Errorf("error writing patch file: %w", err) | ||
} | ||
defer cleanup() | ||
|
||
patchCmd := exec.Command("go", "run", "github.com/uber-go/[email protected]", "-p", path, "./provider/resources.go") | ||
patchCmd.Stdout = os.Stdout | ||
patchCmd.Stderr = os.Stderr | ||
patchCmd.Dir = outDir | ||
if err = patchCmd.Run(); err != nil { | ||
return fmt.Errorf("error running gopatch: %w", err) | ||
} | ||
|
||
// Find go.mod files and tidy them | ||
goModCmd := exec.Command("find", ".", "-name", "go.mod", "-not", "-path", "./upstream/*") | ||
goModCmd.Dir = outDir | ||
goModCmd.Stderr = os.Stderr | ||
goModOutput, err := goModCmd.Output() | ||
if err != nil { | ||
return fmt.Errorf("error finding go.mod files: %w\n%s", err, goModOutput) | ||
} | ||
|
||
goModPaths := strings.Split(string(goModOutput), "\n") | ||
for _, goModPath := range goModPaths { | ||
if goModPath == "" { | ||
continue | ||
} | ||
tidyCmd := exec.Command("go", "mod", "tidy") | ||
tidyCmd.Dir = filepath.Join(outDir, filepath.Dir(goModPath)) | ||
tidyCmd.Stdout = os.Stdout | ||
tidyCmd.Stderr = os.Stderr | ||
err = tidyCmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("error running go mod tidy: %w", err) | ||
} | ||
} | ||
|
||
// Find modified .go files and run gofumpt on them | ||
gitDiff := exec.Command("git", "diff", "--name-only") | ||
gitDiff.Dir = outDir | ||
gitDiffOutput, err := gitDiff.Output() | ||
if err != nil { | ||
return fmt.Errorf("error getting changed files: %w", err) | ||
} | ||
if len(gitDiffOutput) == 0 { | ||
return nil | ||
} | ||
|
||
diffLines := strings.Split(string(gitDiffOutput), "\n") | ||
modifiedGoFiles := []string{} | ||
for _, line := range diffLines { | ||
if strings.HasSuffix(line, ".go") { | ||
modifiedGoFiles = append(modifiedGoFiles, line) | ||
} | ||
} | ||
if len(modifiedGoFiles) == 0 { | ||
return nil | ||
} | ||
|
||
for _, file := range modifiedGoFiles { | ||
// Tidy each file twice to ensure that the file is formatted correctly | ||
for i := 0; i < 2; i++ { | ||
gofumptCmd := exec.Command("go", "run", "mvdan.cc/gofumpt@latest", "-w", file) | ||
gofumptCmd.Stdout = os.Stdout | ||
gofumptCmd.Stderr = os.Stderr | ||
gofumptCmd.Dir = outDir | ||
err = gofumptCmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("error running gofumpt: %w", err) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Migration interface { | ||
Name() string | ||
Migrate(templateName, outDir string) error | ||
ShouldRun(templateName string) bool | ||
} | ||
|
||
func Migrate(templateName, outDir string) error { | ||
migrations := []Migration{ | ||
fixupBridgeImports{}, | ||
removeExplicitSDKDependency{}, | ||
} | ||
for i, migration := range migrations { | ||
if !migration.ShouldRun(templateName) { | ||
fmt.Printf("Migration %d: %s: skipped\n", i+1, migration.Name()) | ||
continue | ||
} | ||
fmt.Printf("Migration %d: %s: running\n", i+1, migration.Name()) | ||
err := migration.Migrate(templateName, outDir) | ||
if err != nil { | ||
return fmt.Errorf("error running migration %q: %w", migration.Name(), err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Returns the path to the temporary file and a function to clean it up, or an error. | ||
func writeTempFile(name, content string) (string, func(), error) { | ||
dir, err := os.MkdirTemp(os.TempDir(), "pulumi-provider-ci-migration-files") | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
path := filepath.Join(dir, name) | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
defer f.Close() | ||
_, err = f.WriteString(content) | ||
return path, func() { os.Remove(f.Name()) }, err | ||
} |
48 changes: 48 additions & 0 deletions
48
provider-ci/internal/pkg/migrations/removeExplicitSDKDependency.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package migrations | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
//go:embed removeExplicitSDKDependency.patch | ||
var removeExplicitSDKDependencyPatch string | ||
|
||
type removeExplicitSDKDependency struct{} | ||
|
||
func (removeExplicitSDKDependency) Name() string { | ||
return "remove explicit SDK dependency" | ||
} | ||
func (removeExplicitSDKDependency) ShouldRun(templateName string) bool { | ||
return templateName == "bridged-provider" | ||
} | ||
func (removeExplicitSDKDependency) Migrate(templateName, outDir string) error { | ||
path, cleanup, err := writeTempFile("removeExplicitSDKDependency.patch", removeExplicitSDKDependencyPatch) | ||
if err != nil { | ||
return fmt.Errorf("error writing patch file: %w", err) | ||
} | ||
defer cleanup() | ||
|
||
patchCmd := exec.Command("go", "run", "github.com/uber-go/[email protected]", "-p", path, "./provider/resources.go") | ||
patchCmd.Stdout = os.Stdout | ||
patchCmd.Stderr = os.Stderr | ||
patchCmd.Dir = outDir | ||
if err = patchCmd.Run(); err != nil { | ||
return fmt.Errorf("error running gopatch: %w", err) | ||
} | ||
|
||
// Tidy twice to ensure that the file is formatted correctly | ||
for i := 0; i < 2; i++ { | ||
gofumptCmd := exec.Command("go", "run", "mvdan.cc/gofumpt@latest", "-w", "./provider/resources.go") | ||
gofumptCmd.Stdout = os.Stdout | ||
gofumptCmd.Stderr = os.Stderr | ||
gofumptCmd.Dir = outDir | ||
err = gofumptCmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("error running gofumpt: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
File renamed without changes.
Oops, something went wrong.