|
| 1 | +package rebuildproject |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/ActiveState/cli/internal/errs" |
| 7 | + "github.com/ActiveState/cli/internal/output" |
| 8 | + "github.com/ActiveState/cli/internal/primer" |
| 9 | + "github.com/ActiveState/cli/pkg/platform/api/buildplanner/types" |
| 10 | + "github.com/ActiveState/cli/pkg/platform/authentication" |
| 11 | + "github.com/ActiveState/cli/pkg/platform/model" |
| 12 | + "github.com/ActiveState/cli/pkg/platform/model/buildplanner" |
| 13 | + bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner" |
| 14 | + "github.com/ActiveState/cli/pkg/project" |
| 15 | +) |
| 16 | + |
| 17 | +type RebuildProjectRunner struct { |
| 18 | + auth *authentication.Auth |
| 19 | + output output.Outputer |
| 20 | + svcModel *model.SvcModel |
| 21 | +} |
| 22 | + |
| 23 | +func New(p *primer.Values) *RebuildProjectRunner { |
| 24 | + return &RebuildProjectRunner{ |
| 25 | + auth: p.Auth(), |
| 26 | + output: p.Output(), |
| 27 | + svcModel: p.SvcModel(), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +type Params struct { |
| 32 | + Namespace *project.Namespaced |
| 33 | +} |
| 34 | + |
| 35 | +func NewParams() *Params { |
| 36 | + return &Params{} |
| 37 | +} |
| 38 | + |
| 39 | +func (runner *RebuildProjectRunner) Run(params *Params) error { |
| 40 | + branch, err := model.DefaultBranchForProjectName(params.Namespace.Owner, params.Namespace.Project) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("error fetching default branch: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Collect "before" buildscript |
| 46 | + bpm := bpModel.NewBuildPlannerModel(runner.auth, runner.svcModel) |
| 47 | + localCommit, err := bpm.FetchCommitNoPoll(*branch.CommitID, params.Namespace.Owner, params.Namespace.Project, nil) |
| 48 | + if err != nil { |
| 49 | + return errs.Wrap(err, "Failed to fetch build result") |
| 50 | + } |
| 51 | + |
| 52 | + // Collect "after" buildscript |
| 53 | + bumpedBS, err := localCommit.BuildScript().Clone() |
| 54 | + if err != nil { |
| 55 | + return errs.Wrap(err, "Failed to clone build script") |
| 56 | + } |
| 57 | + |
| 58 | + latest, err := model.FetchLatestRevisionTimeStamp(runner.auth) |
| 59 | + if err != nil { |
| 60 | + return errs.Wrap(err, "Failed to fetch latest timestamp") |
| 61 | + } |
| 62 | + bumpedBS.SetAtTime(latest, true) |
| 63 | + |
| 64 | + // Since our platform is commit based we need to create a commit for the "after" buildscript |
| 65 | + bumpedCommit, err := bpm.StageCommitAndPoll(bpModel.StageCommitParams{ |
| 66 | + Owner: params.Namespace.Owner, |
| 67 | + Project: params.Namespace.Project, |
| 68 | + ParentCommit: branch.CommitID.String(), |
| 69 | + Script: bumpedBS, |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + return errs.Wrap(err, "Failed to stage bumped commit") |
| 73 | + } |
| 74 | + |
| 75 | + // Now, merge the new commit using the branch name to fast-forward |
| 76 | + _, err = bpm.MergeCommit(&buildplanner.MergeCommitParams{ |
| 77 | + Owner: params.Namespace.Owner, |
| 78 | + Project: params.Namespace.Project, |
| 79 | + TargetRef: branch.Label, |
| 80 | + OtherRef: bumpedCommit.CommitID.String(), |
| 81 | + Strategy: types.MergeCommitStrategyFastForward, |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("error merging commit: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + runner.output.Print("Project is now rebuilding with commit ID " + bumpedCommit.CommitID.String()) |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
0 commit comments