Skip to content

Commit

Permalink
Merge branch 'eb/mergecheck-hotfix' of _OKE5H2PQKOUfzFFDuD4FA/default…
Browse files Browse the repository at this point in the history
…/CODE/gitness (#12)
  • Loading branch information
enver-bisevac authored and Harness committed Mar 30, 2023
2 parents 4b58ef9 + 5c266ad commit 4c05cbd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
16 changes: 15 additions & 1 deletion gitrpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var (
ErrNoParamsProvided = ErrInvalidArgumentf("params not provided")
)

const (
conflictFilesKey = "conflict_files"
)

type Status string

const (
Expand Down Expand Up @@ -147,7 +151,7 @@ func processRPCErrorf(err error, format string, args ...interface{}) error {
for _, detail := range st.Details() {
switch t := detail.(type) {
case *rpc.MergeConflictError:
details["conflict_files"] = t.ConflictingFiles
details[conflictFilesKey] = t.ConflictingFiles
code = StatusNotMergeable
default:
}
Expand All @@ -160,3 +164,13 @@ func processRPCErrorf(err error, format string, args ...interface{}) error {
return fallbackErr
}
}

func AsConflictFilesError(err error) (files []string) {
details := ErrorDetails(err)
object, ok := details[conflictFilesKey]
if ok {
files, _ = object.([]string)
}

return
}
25 changes: 17 additions & 8 deletions internal/api/controller/repo/merge_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,33 @@ import (
)

type MergeCheck struct {
Mergeable bool `json:"mergeable"`
Mergeable bool `json:"mergeable"`
ConflictFiles []string `json:"conflict_files,omitempty"`
}

func (c *Controller) MergeCheck(
ctx context.Context,
session *auth.Session,
repoRef string,
diffPath string,
) error {
) (MergeCheck, error) {
repo, err := c.repoStore.FindByRef(ctx, repoRef)
if err != nil {
return err
return MergeCheck{}, err
}

if err = apiauth.CheckRepo(ctx, c.authorizer, session, repo, enum.PermissionRepoView, false); err != nil {
return fmt.Errorf("access check failed: %w", err)
return MergeCheck{}, fmt.Errorf("access check failed: %w", err)
}

info, err := parseDiffPath(diffPath)
if err != nil {
return err
return MergeCheck{}, err
}

writeParams, err := CreateRPCWriteParams(ctx, c.urlProvider, session, repo)
if err != nil {
return fmt.Errorf("failed to create rpc write params: %w", err)
return MergeCheck{}, fmt.Errorf("failed to create rpc write params: %w", err)
}

_, err = c.gitRPCClient.Merge(ctx, &gitrpc.MergeParams{
Expand All @@ -50,8 +51,16 @@ func (c *Controller) MergeCheck(
HeadBranch: info.HeadRef,
})
if err != nil {
return fmt.Errorf("merge execution failed: %w", err)
if gitrpc.ErrorStatus(err) == gitrpc.StatusNotMergeable {
return MergeCheck{
Mergeable: false,
ConflictFiles: gitrpc.AsConflictFilesError(err),
}, nil
}
return MergeCheck{}, fmt.Errorf("merge check execution failed: %w", err)
}

return nil
return MergeCheck{
Mergeable: true,
}, nil
}
4 changes: 2 additions & 2 deletions internal/api/handler/repo/merge_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func HandleMergeCheck(repoCtrl *repo.Controller) http.HandlerFunc {

path := request.GetOptionalRemainderFromPath(r)

err = repoCtrl.MergeCheck(ctx, session, repoRef, path)
output, err := repoCtrl.MergeCheck(ctx, session, repoRef, path)
if err != nil {
render.TranslatedUserError(w, err)
return
}

w.WriteHeader(http.StatusNoContent)
render.JSON(w, http.StatusOK, output)
}
}
3 changes: 1 addition & 2 deletions internal/api/openapi/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,7 @@ func repoOperations(reflector *openapi3.Reflector) {
opMergeCheck.WithTags("repository")
opMergeCheck.WithMapOfAnything(map[string]interface{}{"operationId": "mergeCheck"})
_ = reflector.SetRequest(&opMergeCheck, new(getRawDiffRequest), http.MethodPost)
_ = reflector.SetJSONResponse(&opMergeCheck, nil, http.StatusNoContent)
_ = reflector.SetJSONResponse(&opMergeCheck, new(usererror.Error), http.StatusPreconditionFailed)
_ = reflector.SetJSONResponse(&opMergeCheck, new(repo.MergeCheck), http.StatusOK)
_ = reflector.SetJSONResponse(&opMergeCheck, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opMergeCheck, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opMergeCheck, new(usererror.Error), http.StatusForbidden)
Expand Down

0 comments on commit 4c05cbd

Please sign in to comment.