Skip to content

Commit

Permalink
Merge branch 'version/0-42-0-RC1' into mitchell/dx-2303
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Nov 2, 2023
2 parents 6a50dc9 + 94b566e commit 9403510
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 133 deletions.
42 changes: 11 additions & 31 deletions internal/runners/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ActiveState/cli/internal/rtutils/ptr"
"github.com/ActiveState/cli/internal/runbits/commitmediator"
"github.com/ActiveState/cli/internal/runbits/rationalize"
bpModel "github.com/ActiveState/cli/pkg/platform/api/buildplanner/model"
"github.com/ActiveState/cli/pkg/platform/api/mono/mono_models"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
Expand Down Expand Up @@ -63,10 +64,8 @@ const (
)

var (
errNoChanges = errors.New("no changes")
errNoCommit = errors.New("no commit")
errTargetInvalidHistory = errors.New("local and remove histories do not match")
errPullNeeded = errors.New("pull needed")
errNoChanges = errors.New("no changes")
errNoCommit = errors.New("no commit")
)

type errProjectNameInUse struct {
Expand Down Expand Up @@ -230,35 +229,16 @@ func (r *Push) Run(params PushParams) (rerr error) {
return errNoChanges
}

// Check whether there is a conflict
if branch.CommitID != nil {
mergeStrategy, err := model.MergeCommit(*branch.CommitID, commitID)
if err != nil {
if errors.Is(err, model.ErrMergeCommitInHistory) {
return errNoChanges
}
if !errors.Is(err, model.ErrMergeFastForward) {
if params.Namespace.IsValid() {
return errTargetInvalidHistory
}
return errs.Wrap(err, "Could not detect if merge is necessary")
}
}
if mergeStrategy != nil {
return errPullNeeded
}
}

// Perform the push.
err = bp.AttachStagedCommit(&model.AttachStagedCommitParams{
Owner: targetNamespace.Owner,
Project: targetNamespace.Project,
ParentCommitID: *branch.CommitID,
StagedCommitID: commitID,
Branch: branch.BranchID.String(),
// Perform the (fast-forward) push.
_, err = bp.MergeCommit(&model.MergeCommitParams{
Owner: targetNamespace.Owner,
Project: targetNamespace.Project,
TargetRef: branch.Label, // using branch name will fast-forward
OtherRef: commitID.String(),
Strategy: bpModel.MergeCommitStrategyFastForward,
})
if err != nil {
return errs.Wrap(err, "Could not attach staged commit")
return errs.Wrap(err, "Could not push")
}
}

Expand Down
36 changes: 25 additions & 11 deletions internal/runners/push/rationalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/runbits/rationalize"
bpModel "github.com/ActiveState/cli/pkg/platform/api/buildplanner/model"
)

func rationalizeError(err *error) {
Expand All @@ -17,6 +18,8 @@ func rationalizeError(err *error) {

var headlessErr *errHeadless

var mergeCommitErr *bpModel.MergedCommitError

switch {

// Not authenticated
Expand Down Expand Up @@ -68,17 +71,28 @@ func rationalizeError(err *error) {
locale.T("err_push_create_project_aborted"),
errs.SetInput())

// Custom target does not have a compatible history
case errors.Is(*err, errTargetInvalidHistory):
*err = errs.WrapUserFacing(*err,
locale.T("err_push_target_invalid_history"),
errs.SetInput())
case errors.As(*err, &mergeCommitErr):
switch mergeCommitErr.Type {
// Need to pull first
case bpModel.FastForwardErrorType:
*err = errs.WrapUserFacing(*err,
locale.T("err_push_outdated"),
errs.SetInput(),
errs.SetTips(locale.T("err_tip_push_outdated")))

// Need to pull first
case errors.Is(*err, errPullNeeded):
*err = errs.WrapUserFacing(*err,
locale.T("err_push_outdated"),
errs.SetInput(),
errs.SetTips(locale.T("err_tip_push_outdated")))
// Custom target does not have a compatible history
case bpModel.NoCommonBaseFoundType:
*err = errs.WrapUserFacing(*err,
locale.T("err_push_target_invalid_history"),
errs.SetInput())

// No changes made
case bpModel.NoChangeSinceLastCommitErrorType:
*err = errs.WrapUserFacing(*err,
locale.T("push_no_changes"),
errs.SetInput(),
)

}
}
}
41 changes: 38 additions & 3 deletions pkg/platform/api/buildplanner/model/buildplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

type Operation int

type MergeStrategy string

const (
OperationAdded Operation = iota
OperationRemoved
Expand Down Expand Up @@ -60,6 +62,12 @@ const (
XGozipInstallerMimeType = "application/x-gozip-installer"
XActiveStateBuilderMimeType = "application/x-activestate-builder"

// MergeCommit strategies
MergeCommitStrategyRecursive MergeStrategy = "Recursive"
MergeCommitStrategyRecursiveOverwriteOnConflict MergeStrategy = "RecursiveOverwriteOnConflict"
MergeCommitStrategyRecursiveKeepOnConflict MergeStrategy = "RecursiveKeepOnConflict"
MergeCommitStrategyFastForward MergeStrategy = "FastForward"

// Error types
ErrorType = "Error"
NotFoundErrorType = "NotFound"
Expand All @@ -71,6 +79,9 @@ const (
ForbiddenErrorType = "Forbidden"
RemediableSolveErrorType = "RemediableSolveError"
PlanningErrorType = "PlanningError"
MergeConflictType = "MergeConflict"
FastForwardErrorType = "FastForwardError"
NoCommonBaseFoundType = "NoCommonBaseFound"
)

func IsStateToolArtifact(mimeType string) bool {
Expand Down Expand Up @@ -268,7 +279,10 @@ func IsErrorResponse(errorType string) bool {
errorType == ForbiddenErrorType ||
errorType == RemediableSolveErrorType ||
errorType == PlanningErrorType ||
errorType == NotFoundErrorType
errorType == NotFoundErrorType ||
errorType == MergeConflictType ||
errorType == FastForwardErrorType ||
errorType == NoCommonBaseFoundType
}

func ProcessCommitError(commit *Commit, fallbackMessage string) error {
Expand Down Expand Up @@ -354,6 +368,20 @@ type BuildExpression struct {
*Error
}

type MergedCommitError struct {
Type string
Message string
}

func (m *MergedCommitError) Error() string { return m.Message }

func ProcessMergedCommitError(mcErr *mergedCommit, fallbackMessage string) error {
if mcErr.Type != "" {
return &MergedCommitError{mcErr.Type, mcErr.Message}
}
return errs.New(fallbackMessage)
}

// PushCommitResult is the result of a push commit mutation.
// It contains the resulting commit from the operation and any errors.
// The resulting commit is pushed to the platform automatically.
Expand All @@ -380,12 +408,19 @@ type CreateProjectResult struct {
ProjectCreated *projectCreated `json:"createProject"`
}

type AttachStagedCommitResult struct {
type mergedCommit struct {
Type string `json:"__typename"`
Commit *Commit `json:"attachStagedCommit"`
Commit *Commit `json:"commit"`
*Error
}

// MergeCommitResult is the result of a merge commit mutation.
// The resulting commit is only pushed to the platform automatically if the target ref was a named
// branch and the merge strategy was FastForward.
type MergeCommitResult struct {
MergedCommit *mergedCommit `json:"mergeCommit"`
}

// Error contains an error message.
type Error struct {
Message string `json:"message"`
Expand Down
60 changes: 0 additions & 60 deletions pkg/platform/api/buildplanner/request/attachstagedcommit.go

This file was deleted.

76 changes: 76 additions & 0 deletions pkg/platform/api/buildplanner/request/mergecommit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package request

import "github.com/ActiveState/cli/pkg/platform/api/buildplanner/model"

func MergeCommit(owner, project, targetRef, otherRef string, strategy model.MergeStrategy) *mergeCommit {
return &mergeCommit{map[string]interface{}{
"organization": owner,
"project": project,
"targetRef": targetRef,
"otherRef": otherRef,
"strategy": strategy,
}}
}

type mergeCommit struct {
vars map[string]interface{}
}

func (b *mergeCommit) Query() string {
return `
mutation ($organization: String!, $project: String!, $targetRef: String!, $otherRef: String!, $strategy: MergeStrategy) {
mergeCommit(input:{organization:$organization, project:$project, targetVcsRef:$targetRef, otherVcsRef:$otherRef, strategy:$strategy}) {
... on MergedCommit {
commit {
__typename
commitId
}
}
... on MergeConflict {
__typename
message
}
... on FastForwardError {
__typename
message
}
... on NoCommonBaseFound {
__typename
message
}
... on NotFound {
__typename
message
mayNeedAuthentication
}
... on ParseError {
__typename
message
}
... on ValidationError {
__typename
message
}
... on Forbidden {
__typename
message
}
... on HeadOnBranchMoved {
__typename
message
commitId
branchId
}
... on NoChangeSinceLastCommit {
__typename
message
commitId
}
}
}
`
}

func (b *mergeCommit) Vars() map[string]interface{} {
return b.vars
}
Loading

0 comments on commit 9403510

Please sign in to comment.