diff --git a/internal/actions/runWorkflow.go b/internal/actions/runWorkflow.go index 7c495d84..d7ad93ff 100644 --- a/internal/actions/runWorkflow.go +++ b/internal/actions/runWorkflow.go @@ -33,7 +33,7 @@ func RunWorkflow() error { return err } - return runWorkflow(g, resolvedVersion) + return runWorkflow(g, resolvedVersion, false) } pinnedVersion := wf.SpeakeasyVersion.String() @@ -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 { @@ -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 @@ -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 diff --git a/internal/git/git.go b/internal/git/git.go index ccfb6a3b..cf9a5cd7 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -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 { @@ -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 } @@ -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...) @@ -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 {