Skip to content

Commit

Permalink
Revert "chore(ci): move ExecRunner interface to exec package" (Google…
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge authored Aug 16, 2024
1 parent e7efcc3 commit 455b727
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 26 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 exec.ExecRunner, ctlr *source.Controller) error {
func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, projectId, commitSha string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller) error {
errors := map[string][]string{"Other": []string{}}

// TODO(ScottSuarez) - temporary fix to ensure the label is removed.
Expand Down Expand Up @@ -420,7 +420,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 exec.ExecRunner) error {
func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[string]string, rnr ExecRunner) error {
for _, path := range []string{"old", "new", "bin"} {
if err := rnr.RemoveAll(filepath.Join(diffProcessorPath, path)); err != nil {
return err
Expand All @@ -440,7 +440,7 @@ func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[str
return rnr.PopDir()
}

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

func changedSchemaResources(diffProcessorPath string, rnr exec.ExecRunner) ([]string, error) {
func changedSchemaResources(diffProcessorPath string, rnr ExecRunner) ([]string, error) {
if err := rnr.PushDir(diffProcessorPath); err != nil {
return nil, err
}
Expand All @@ -486,7 +486,7 @@ func changedSchemaResources(diffProcessorPath string, rnr exec.ExecRunner) ([]st
// 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 exec.ExecRunner) (map[string]*MissingTestInfo, error) {
func detectMissingTests(diffProcessorPath, tpgbLocalPath string, rnr 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 exec.ExecRunner, ctlr *source.Controller) error {
func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh GithubClient, rnr 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 exec.ExecRunner, ctlr *source.Controller) (*source.Repo, *source.Repo, string, error) {
func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref string, rnr 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 exec.ExecRunner) error {
func setGitConfig(rnr 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 exec.ExecRunner) error {
return nil
}

func runMake(downstreamRepo *source.Repo, command string, rnr exec.ExecRunner) error {
func runMake(downstreamRepo *source.Repo, command string, rnr 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 exec.ExecRunner) (string, error) {
func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner) (string, error) {
if err := rnr.PushDir(scratchRepo.Path); err != nil {
return "", err
}
Expand Down Expand Up @@ -346,7 +346,7 @@ func createCommit(scratchRepo *source.Repo, commitMessage string, rnr exec.ExecR
return commitSha, err
}

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

func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr exec.ExecRunner, gh GithubClient) error {
func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr ExecRunner, gh GithubClient) error {
fmt.Printf(`Base: %s:%s
Head: %s:%s
`, downstreamRepo.Owner, downstreamRepo.Branch, scratchRepo.Owner, scratchRepo.Branch)
Expand Down
12 changes: 12 additions & 0 deletions .ci/magician/cmd/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ 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: 1 addition & 2 deletions .ci/magician/cmd/mock_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"log"
"magician/exec"
"os"
"path/filepath"
"sort"
Expand All @@ -32,7 +31,7 @@ import (
type ParameterList []any

type MockRunner interface {
exec.ExecRunner
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 exec.ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error {
func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep, baseBranch string, gh GithubClient, rnr 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 exec.ExecRunner, ctlr *source.Controller, gh GithubClient) error {
func execTestTGCIntegration(prNumber, mmCommit, buildID, projectID, buildStep, ghRepo, githubUsername string, rnr ExecRunner, ctlr *source.Controller, gh GithubClient) error {
newBranch := "auto-pr-" + prNumber
repo := &source.Repo{
Name: ghRepo,
Expand Down
8 changes: 4 additions & 4 deletions .ci/magician/cmd/vcr_cassette_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var vcrCassetteUpdateCmd = &cobra.Command{
},
}

func execVCRCassetteUpdate(buildID, today string, rnr exec.ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error {
func execVCRCassetteUpdate(buildID, today string, rnr ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error {
if err := vt.FetchCassettes(provider.Beta, "main", ""); err != nil {
return fmt.Errorf("error fetching cassettes: %w", err)
}
Expand Down Expand Up @@ -199,15 +199,15 @@ func execVCRCassetteUpdate(buildID, today string, rnr exec.ExecRunner, ctlr *sou
return nil
}

func uploadLogsToGCS(src, dest string, rnr exec.ExecRunner) (string, error) {
func uploadLogsToGCS(src, dest string, rnr ExecRunner) (string, error) {
return uploadToGCS(src, dest, []string{"-h", "Content-Type:text/plain", "-q", "cp", "-r"}, rnr)
}

func uploadCassettesToGCS(src, dest string, rnr exec.ExecRunner) (string, error) {
func uploadCassettesToGCS(src, dest string, rnr ExecRunner) (string, error) {
return uploadToGCS(src, dest, []string{"-m", "-q", "cp"}, rnr)
}

func uploadToGCS(src, dest string, opts []string, rnr exec.ExecRunner) (string, error) {
func uploadToGCS(src, dest string, opts []string, rnr ExecRunner) (string, error) {
fmt.Printf("uploading from %s to %s\n", src, dest)
args := append(opts, src, dest)
fmt.Println("gsutil", args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package exec
package vcr

import "path/filepath"

Expand All @@ -9,9 +9,9 @@ type ExecRunner interface {
RemoveAll(path string) error
PushDir(path string) error
PopDir() error
ReadFile(name string) (string, error)
WriteFile(name, data string) error
Walk(root string, fn filepath.WalkFunc) 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)
}
5 changes: 2 additions & 3 deletions .ci/magician/vcr/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vcr
import (
"fmt"
"io/fs"
"magician/exec"
"magician/provider"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -50,7 +49,7 @@ type logKey struct {

type Tester struct {
env map[string]string // shared environment variables for running tests
rnr exec.ExecRunner // for running commands and manipulating files
rnr 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 +67,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.ExecRunner) (*Tester, error) {
func NewTester(env map[string]string, rnr ExecRunner) (*Tester, error) {
saKeyPath := "sa_key.json"
if err := rnr.WriteFile(saKeyPath, env["SA_KEY"]); err != nil {
return nil, err
Expand Down

0 comments on commit 455b727

Please sign in to comment.