Skip to content

Commit

Permalink
chore: force fail for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
chase-crumbaugh committed Apr 10, 2024
1 parent 2d1299d commit 0b17995
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
17 changes: 13 additions & 4 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func RunWorkflow() error {
return err
}

return runWorkflow(g, resolvedVersion)
return runWorkflow(g, resolvedVersion, false)
}

pinnedVersion := wf.SpeakeasyVersion.String()
Expand All @@ -53,7 +53,7 @@ func RunWorkflow() error {
logging.Info("Attempting auto-upgrade from Speakeasy version %s to %s", pinnedVersion, resolvedVersion)
}

err = runWorkflow(g, resolvedVersion)
err = runWorkflow(g, resolvedVersion, true)
if attemptingAutoUpgrade {
// If we tried to upgrade and the run succeeded, update the workflow file with the new version
if err == nil {
Expand All @@ -68,19 +68,24 @@ func RunWorkflow() error {
logging.Info("Error running workflow with version %s: %v", firstRunVersion, err)
logging.Info("Trying again with pinned version %s", pinnedVersion)

// Before re-running, reset anything we already did
if err := g.HardResetToDefault(); err != nil {
return err
}

resolvedVersion, err := cli.Download(firstRunVersion, g)
if err != nil {
return err
}

return runWorkflow(g, resolvedVersion)
return runWorkflow(g, resolvedVersion, false)
}
}

return err
}

func runWorkflow(g *git.Git, resolvedVersion string) error {
func runWorkflow(g *git.Git, resolvedVersion string, forceFail bool) error {
wf, _, err := configuration.GetWorkflowAndValidateLanguages(true)
if err != nil {
return err
Expand Down Expand Up @@ -195,6 +200,10 @@ func runWorkflow(g *git.Git, resolvedVersion string) error {
return err
}

if forceFail {
return fmt.Errorf("force fail")
}

success = true

return nil
Expand Down
33 changes: 17 additions & 16 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (
)

type Git struct {
accessToken string
repo *git.Repository
client *github.Client
accessToken string
repo *git.Repository
client *github.Client
defaultBreanch string
}

func New(accessToken string) *Git {
Expand Down Expand Up @@ -85,6 +86,13 @@ func (g *Git) CloneRepo() error {
}
g.repo = r

defaultBranch, err := g.GetCurrentBranch()
if err != nil {
// Swallow this error for now. Functionality will be unchanged from previous behavior if it fails
logging.Info("failed to get default branch: %s", err.Error())
}
g.defaultBreanch = defaultBranch

return nil
}

Expand Down Expand Up @@ -236,6 +244,11 @@ func (g *Git) FindAndCheckoutBranch(branchName string) (string, error) {
return branchName, nil
}

func (g *Git) HardResetToDefault() error {
origin := fmt.Sprintf("origin/%s", g.defaultBreanch)
return g.Reset("--hard", origin)
}

func (g *Git) Reset(args ...string) error {
// We execute this manually because go-git doesn't support all the options we need
args = append([]string{"reset"}, args...)
Expand Down Expand Up @@ -264,24 +277,12 @@ func (g *Git) FindOrCreateBranch(branchName string, action environment.Action) (
}

if branchName != "" {
defaultBranch, err := g.GetCurrentBranch()
if err != nil {
// Swallow this error for now. Functionality will be unchanged from previous behavior if it fails
logging.Info("failed to get default branch: %s", err.Error())
}

branchName, err := g.FindAndCheckoutBranch(branchName)
if err != nil {
return "", err
}

origin := fmt.Sprintf("origin/%s", defaultBranch)
if err = g.Reset("--hard", origin); err != nil {
// Swallow this error for now. Functionality will be unchanged from previous behavior if it fails
logging.Info("failed to reset branch: %s", err.Error())
}

return branchName, nil
return branchName, g.HardResetToDefault()
}

if action == environment.ActionRunWorkflow {
Expand Down

0 comments on commit 0b17995

Please sign in to comment.