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