Skip to content

Commit

Permalink
chore(ci): move ExecRunner interface to exec package (GoogleCloudPlat…
Browse files Browse the repository at this point in the history
  • Loading branch information
iyabchen authored and BBBmau committed Aug 21, 2024
1 parent 5108aef commit a39bdea
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 32 deletions.
10 changes: 5 additions & 5 deletions .ci/magician/cmd/generate_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func listGCEnvironmentVariables() string {
return result
}

func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, projectId, commitSha string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller) error {
func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, projectId, commitSha string, gh GithubClient, rnr exec.ExecRunner, ctlr *source.Controller) error {
errors := map[string][]string{"Other": []string{}}

pullRequest, err := gh.GetPullRequest(strconv.Itoa(prNumber))
Expand Down Expand Up @@ -414,7 +414,7 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
}

// Build the diff processor for tpg or tpgb
func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[string]string, rnr ExecRunner) error {
func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[string]string, rnr exec.ExecRunner) error {
for _, path := range []string{"old", "new", "bin"} {
if err := rnr.RemoveAll(filepath.Join(diffProcessorPath, path)); err != nil {
return err
Expand All @@ -434,7 +434,7 @@ func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[str
return rnr.PopDir()
}

func computeBreakingChanges(diffProcessorPath string, rnr ExecRunner) ([]BreakingChange, error) {
func computeBreakingChanges(diffProcessorPath string, rnr exec.ExecRunner) ([]BreakingChange, error) {
if err := rnr.PushDir(diffProcessorPath); err != nil {
return nil, err
}
Expand All @@ -454,7 +454,7 @@ func computeBreakingChanges(diffProcessorPath string, rnr ExecRunner) ([]Breakin
return changes, rnr.PopDir()
}

func changedSchemaResources(diffProcessorPath string, rnr ExecRunner) ([]string, error) {
func changedSchemaResources(diffProcessorPath string, rnr exec.ExecRunner) ([]string, error) {
if err := rnr.PushDir(diffProcessorPath); err != nil {
return nil, err
}
Expand All @@ -480,7 +480,7 @@ func changedSchemaResources(diffProcessorPath string, rnr ExecRunner) ([]string,
// Run the missing test detector and return the results.
// Returns an empty string unless there are missing tests.
// Error will be nil unless an error occurs during setup.
func detectMissingTests(diffProcessorPath, tpgbLocalPath string, rnr ExecRunner) (map[string]*MissingTestInfo, error) {
func detectMissingTests(diffProcessorPath, tpgbLocalPath string, rnr exec.ExecRunner) (map[string]*MissingTestInfo, error) {
if err := rnr.PushDir(diffProcessorPath); err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions .ci/magician/cmd/generate_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func listGDEnvironmentVariables() string {
return result
}

func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller) error {
func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh GithubClient, rnr exec.ExecRunner, ctlr *source.Controller) error {
if baseBranch == "" {
baseBranch = "main"
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh G
return nil
}

func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref string, rnr ExecRunner, ctlr *source.Controller) (*source.Repo, *source.Repo, string, error) {
func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref string, rnr exec.ExecRunner, ctlr *source.Controller) (*source.Repo, *source.Repo, string, error) {
downstreamRepo := &source.Repo{
Title: repo,
Branch: baseBranch,
Expand Down Expand Up @@ -244,7 +244,7 @@ func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref stri
return downstreamRepo, scratchRepo, commitMessage, nil
}

func setGitConfig(rnr ExecRunner) error {
func setGitConfig(rnr exec.ExecRunner) error {
if _, err := rnr.Run("git", []string{"config", "--local", "user.name", "Modular Magician"}, nil); err != nil {
return err
}
Expand All @@ -254,7 +254,7 @@ func setGitConfig(rnr ExecRunner) error {
return nil
}

func runMake(downstreamRepo *source.Repo, command string, rnr ExecRunner) error {
func runMake(downstreamRepo *source.Repo, command string, rnr exec.ExecRunner) error {
switch downstreamRepo.Title {
case "terraform-google-conversion":
if _, err := rnr.Run("make", []string{"clean-tgc", "OUTPUT_PATH=" + downstreamRepo.Path}, nil); err != nil {
Expand Down Expand Up @@ -308,7 +308,7 @@ func getPullRequest(baseBranch, ref string, gh GithubClient) (*github.PullReques
return nil, fmt.Errorf("no pr found with merge commit sha %s and base branch %s", ref, baseBranch)
}

func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner) (string, error) {
func createCommit(scratchRepo *source.Repo, commitMessage string, rnr exec.ExecRunner) (string, error) {
if err := rnr.PushDir(scratchRepo.Path); err != nil {
return "", err
}
Expand Down Expand Up @@ -338,7 +338,7 @@ func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner
return commitSha, err
}

func addChangelogEntry(downstreamRepo *source.Repo, pullRequest *github.PullRequest, rnr ExecRunner) error {
func addChangelogEntry(downstreamRepo *source.Repo, pullRequest *github.PullRequest, rnr exec.ExecRunner) error {
if err := rnr.PushDir(downstreamRepo.Path); err != nil {
return err
}
Expand All @@ -349,7 +349,7 @@ func addChangelogEntry(downstreamRepo *source.Repo, pullRequest *github.PullRequ
return rnr.PopDir()
}

func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr ExecRunner, gh GithubClient) error {
func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr exec.ExecRunner, gh GithubClient) error {
fmt.Printf(`Base: %s:%s
Head: %s:%s
`, downstreamRepo.Owner, downstreamRepo.Branch, scratchRepo.Owner, scratchRepo.Branch)
Expand Down
12 changes: 0 additions & 12 deletions .ci/magician/cmd/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,3 @@ type CloudbuildClient interface {
ApproveCommunityChecker(prNumber, commitSha string) error
TriggerMMPresubmitRuns(commitSha string, substitutions map[string]string) error
}

type ExecRunner interface {
GetCWD() string
Copy(src, dest string) error
Mkdir(path string) error
RemoveAll(path string) error
PushDir(path string) error
PopDir() error
WriteFile(name, data string) error
Run(name string, args []string, env map[string]string) (string, error)
MustRun(name string, args []string, env map[string]string) string
}
3 changes: 2 additions & 1 deletion .ci/magician/cmd/mock_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"log"
"magician/exec"
"os"
"path/filepath"
"sort"
Expand All @@ -31,7 +32,7 @@ import (
type ParameterList []any

type MockRunner interface {
ExecRunner
exec.ExecRunner
Calls(method string) ([]ParameterList, bool)
}

Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/test_terraform_vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ var testTerraformVCRCmd = &cobra.Command{
},
}

func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep, baseBranch string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error {
func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep, baseBranch string, gh GithubClient, rnr exec.ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error {
newBranch := "auto-pr-" + prNumber
oldBranch := newBranch + "-old"

Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/test_tgc_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var testTGCIntegrationCmd = &cobra.Command{
},
}

func execTestTGCIntegration(prNumber, mmCommit, buildID, projectID, buildStep, ghRepo, githubUsername string, rnr ExecRunner, ctlr *source.Controller, gh GithubClient) error {
func execTestTGCIntegration(prNumber, mmCommit, buildID, projectID, buildStep, ghRepo, githubUsername string, rnr exec.ExecRunner, ctlr *source.Controller, gh GithubClient) error {
newBranch := "auto-pr-" + prNumber
repo := &source.Repo{
Name: ghRepo,
Expand Down
17 changes: 17 additions & 0 deletions .ci/magician/exec/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package exec

import "path/filepath"

type ExecRunner interface {
GetCWD() string
Copy(src, dest string) error
Mkdir(path string) error
RemoveAll(path string) error
PushDir(path string) error
PopDir() error
WriteFile(name, data string) error
Run(name string, args []string, env map[string]string) (string, error)
MustRun(name string, args []string, env map[string]string) string
Walk(root string, fn filepath.WalkFunc) error
ReadFile(name string) (string, error)
}
10 changes: 5 additions & 5 deletions .ci/magician/vcr/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type logKey struct {

type Tester struct {
env map[string]string // shared environment variables for running tests
rnr *exec.Runner // for running commands and manipulating files
rnr exec.ExecRunner // for running commands and manipulating files
baseDir string // the directory in which this tester was created
saKeyPath string // where sa_key.json is relative to baseDir
cassettePaths map[provider.Version]string // where cassettes are relative to baseDir by version
Expand All @@ -68,7 +68,7 @@ var testResultsExpression = regexp.MustCompile(`(?m:^--- (PASS|FAIL|SKIP): (Test
var testPanicExpression = regexp.MustCompile(`^panic: .*`)

// Create a new tester in the current working directory and write the service account key file.
func NewTester(env map[string]string, rnr *exec.Runner) (*Tester, error) {
func NewTester(env map[string]string, rnr exec.ExecRunner) (*Tester, error) {
saKeyPath := "sa_key.json"
if err := rnr.WriteFile(saKeyPath, env["SA_KEY"]); err != nil {
return nil, err
Expand All @@ -91,11 +91,11 @@ func (vt *Tester) SetRepoPath(version provider.Version, repoPath string) {
// Fetch the cassettes for the current version if not already fetched.
// Should be run from the base dir.
func (vt *Tester) FetchCassettes(version provider.Version, baseBranch, prNumber string) error {
cassettePath, ok := vt.cassettePaths[version]
_, ok := vt.cassettePaths[version]
if ok {
return nil
}
cassettePath = filepath.Join(vt.baseDir, "cassettes", version.String())
cassettePath := filepath.Join(vt.baseDir, "cassettes", version.String())
vt.rnr.Mkdir(cassettePath)
if baseBranch != "FEATURE-BRANCH-major-release-6.0.0" {
// pull main cassettes (major release uses branch specific casssettes as primary ones)
Expand Down Expand Up @@ -240,7 +240,7 @@ func (vt *Tester) RunParallel(mode Mode, version provider.Version, testDirs, tes
if err != nil {
return nil, err
}
if vt.rnr.Mkdir(filepath.Join(vt.baseDir, "testlogs", mode.Lower()+"_build")); err != nil {
if err := vt.rnr.Mkdir(filepath.Join(vt.baseDir, "testlogs", mode.Lower()+"_build")); err != nil {
return nil, err
}
repoPath, ok := vt.repoPaths[version]
Expand Down

0 comments on commit a39bdea

Please sign in to comment.