Skip to content

Commit 7e4b619

Browse files
Push the new commit and retrieve latest timestamp properly
1 parent 764a56f commit 7e4b619

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

cmd/state-mcp/internal/registry/tools.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,10 @@ func RebuildProjectTool() Tool {
317317
return mcp.NewToolResultError(fmt.Sprintf("error parsing project namespace: %s", errs.JoinMessage(err))), nil
318318
}
319319

320-
params := rebuildproject.NewParams(ns)
321-
runner := rebuildproject.New(p)
320+
params := rebuildproject.NewParams()
321+
params.Namespace = ns
322322

323+
runner := rebuildproject.New(p)
323324
err = runner.Run(params)
324325

325326
if err != nil {
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,90 @@
11
package rebuildproject
22

33
import (
4-
"encoding/json"
54
"fmt"
65

7-
"github.com/ActiveState/cli/internal/captain"
86
"github.com/ActiveState/cli/internal/errs"
97
"github.com/ActiveState/cli/internal/output"
108
"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"
1210
"github.com/ActiveState/cli/pkg/platform/authentication"
1311
"github.com/ActiveState/cli/pkg/platform/model"
12+
"github.com/ActiveState/cli/pkg/platform/model/buildplanner"
1413
bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner"
1514
"github.com/ActiveState/cli/pkg/project"
1615
)
1716

18-
type ProjectErrorsRunner struct {
17+
type RebuildProjectRunner struct {
1918
auth *authentication.Auth
2019
output output.Outputer
2120
svcModel *model.SvcModel
2221
}
2322

24-
func New(p *primer.Values) *ProjectErrorsRunner {
25-
return &ProjectErrorsRunner{
23+
func New(p *primer.Values) *RebuildProjectRunner {
24+
return &RebuildProjectRunner{
2625
auth: p.Auth(),
2726
output: p.Output(),
2827
svcModel: p.SvcModel(),
2928
}
3029
}
3130

3231
type Params struct {
33-
project *project.Namespaced
32+
Namespace *project.Namespaced
3433
}
3534

36-
func NewParams(project *project.Namespaced) *Params {
37-
return &Params{
38-
project: project,
39-
}
35+
func NewParams() *Params {
36+
return &Params{}
4037
}
4138

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)
4441
if err != nil {
4542
return fmt.Errorf("error fetching default branch: %w", err)
4643
}
4744

48-
// Collect "before" buildplan
45+
// Collect "before" buildscript
4946
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)
5148
if err != nil {
5249
return errs.Wrap(err, "Failed to fetch build result")
5350
}
5451

55-
// Collect "after" buildplan
52+
// Collect "after" buildscript
5653
bumpedBS, err := localCommit.BuildScript().Clone()
5754
if err != nil {
5855
return errs.Wrap(err, "Failed to clone build script")
5956
}
6057

61-
now := captain.TimeValue{}
62-
now.Set("now")
63-
ts, err := commits_runbit.ExpandTime(&now, runner.auth)
58+
latest, err := model.FetchLatestRevisionTimeStamp(runner.auth)
6459
if err != nil {
6560
return errs.Wrap(err, "Failed to fetch latest timestamp")
6661
}
67-
bumpedBS.SetAtTime(ts, true)
62+
bumpedBS.SetAtTime(latest, true)
6863

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
7165
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,
7468
ParentCommit: branch.CommitID.String(),
7569
Script: bumpedBS,
7670
})
7771
if err != nil {
7872
return errs.Wrap(err, "Failed to stage bumped commit")
7973
}
8074

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+
})
8283
if err != nil {
83-
return fmt.Errorf("error marshaling results: %w", err)
84+
return fmt.Errorf("error merging commit: %w", err)
8485
}
85-
runner.output.Print(string(jsonBytes))
86+
87+
runner.output.Print("Project is now rebuilding with commit ID " + bumpedCommit.CommitID.String())
8688

8789
return nil
8890
}

0 commit comments

Comments
 (0)