Skip to content

Commit

Permalink
Merge pull request #3346 from ActiveState/mitchell/dx-2857
Browse files Browse the repository at this point in the history
Verify a given commitIDs existence in the given project for `state artifacts`.
  • Loading branch information
mitchell-as authored Jun 7, 2024
2 parents 6c3bcdf + fd785e0 commit 5d483b3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
36 changes: 34 additions & 2 deletions internal/runners/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,22 @@ func New(p primeable) *Artifacts {
}

type errInvalidCommitId struct {
error
id string
}

func (e *errInvalidCommitId) Error() string {
return "Invalid commit ID"
}

type errCommitDoesNotExistInProject struct {
Project string
CommitID string
}

func (e *errCommitDoesNotExistInProject) Error() string {
return "Commit does not exist in project"
}

func rationalizeArtifactsError(rerr *error, auth *authentication.Auth) {
if rerr == nil {
return
Expand Down Expand Up @@ -290,7 +302,7 @@ func getBuildPlan(

commitUUID := strfmt.UUID(commitID)
if commitUUID != "" && !strfmt.IsUUID(commitUUID.String()) {
return nil, &errInvalidCommitId{errs.New("Invalid commit ID"), commitUUID.String()}
return nil, &errInvalidCommitId{commitUUID.String()}
}

namespaceProvided := namespace.IsValid()
Expand Down Expand Up @@ -372,5 +384,25 @@ func getBuildPlan(
return nil, errs.New("Unhandled case")
}

// Note: the Platform does not raise an error when requesting a commit ID that does not exist in
// a given project, so we have verify existence client-side. See DS-1705 (yes, DS, not DX).
var owner, name, nsString string
if namespaceProvided {
owner = namespace.Owner
name = namespace.Project
nsString = namespace.String()
} else {
owner = pj.Owner()
name = pj.Name()
nsString = pj.NamespaceString()
}
_, err = model.GetCommitWithinProjectHistory(commit.CommitID, owner, name, auth)
if err != nil {
if err == model.ErrCommitNotInHistory {
return nil, errs.Pack(err, &errCommitDoesNotExistInProject{nsString, commit.CommitID.String()})
}
return nil, errs.Wrap(err, "Unable to determine if commit exists in project")
}

return commit.BuildPlan(), nil
}
10 changes: 10 additions & 0 deletions internal/runners/artifacts/rationalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func rationalizeCommonError(err *error, auth *authentication.Auth) {
var invalidCommitIdErr *errInvalidCommitId
var projectNotFoundErr *model.ErrProjectNotFound
var commitIdDoesNotExistInProject *errCommitDoesNotExistInProject

switch {
case errors.Is(*err, rationalize.ErrNoProject):
Expand All @@ -30,6 +31,15 @@ func rationalizeCommonError(err *error, auth *authentication.Auth) {
locale.Tr("err_api_project_not_found", projectNotFoundErr.Organization, projectNotFoundErr.Project),
errs.SetIf(!auth.Authenticated(), errs.SetTips(locale.T("tip_private_project_auth"))),
errs.SetInput())

case errors.As(*err, &commitIdDoesNotExistInProject):
*err = errs.WrapUserFacing(*err,
locale.Tl("err_commit_id_not_in_history",
"The project '[ACTIONABLE]{{.V0}}[/RESET]' does not contain the provided commit: '[ACTIONABLE]{{.V1}}[/RESET]'.",
commitIdDoesNotExistInProject.Project,
commitIdDoesNotExistInProject.CommitID,
),
errs.SetInput())
}

}
31 changes: 31 additions & 0 deletions pkg/platform/model/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,37 @@ func GetCommitWithinCommitHistory(currentCommitID, targetCommitID strfmt.UUID, a
return commit, nil
}

// GetCommitWithinProjectHistory searches for the a commit with the given commit ID in the given
// project (including all of its branch history) and returns it if found. Otherwise, it returns
// ErrCommitNotInHistory.
// It doesn't matter if the commit exists in multiple branches, as commits do not belong to
// branches.
// This function exists primarily as an existence check because the buildplanner API currently
// accepts a query for a org/project#commitID even if commitID does not belong to org/project.
// See DS-1705 (yes, DS, not DX).
func GetCommitWithinProjectHistory(commitID strfmt.UUID, owner, name string, auth *authentication.Auth) (*mono_models.Commit, error) {
commit, err := GetCommit(commitID, auth)
if err != nil {
return nil, errs.Wrap(err, "Unable to get commit")
}

branches, err := BranchesForProject(owner, name)
if err != nil {
return nil, errs.Wrap(err, "Unable to get branches for project")
}
for _, branch := range branches {
ok, err := CommitWithinCommitHistory(*branch.CommitID, commitID, auth)
if err != nil {
return nil, errs.Wrap(err, "Unable to determine if commit exists in branch history")
}
if ok {
return commit, nil
}
}

return nil, ErrCommitNotInHistory
}

func AddRevertCommit(commit *mono_models.Commit, auth *authentication.Auth) (*mono_models.Commit, error) {
params := vcsClient.NewAddCommitParams()

Expand Down

0 comments on commit 5d483b3

Please sign in to comment.