~edwargix/git.sr.ht

cbb50860b56784fbf570d7f3473e1b21032631da — Adnan Maolood 2 years ago 6c4a010
api/graph: Replace gqlErrorf with valid.Errorf
4 files changed, 11 insertions(+), 26 deletions(-)

M api/go.mod
M api/go.sum
M api/graph/resolver.go
M api/graph/schema.resolvers.go
M api/go.mod => api/go.mod +1 -1
@@ 3,7 3,7 @@ module git.sr.ht/~sircmpwn/git.sr.ht/api
go 1.14

require (
	git.sr.ht/~sircmpwn/core-go v0.0.0-20220112154231-e28d47cf5957
	git.sr.ht/~sircmpwn/core-go v0.0.0-20220126083025-75dfa2992b49
	github.com/99designs/gqlgen v0.14.0
	github.com/Masterminds/squirrel v1.4.0
	github.com/go-git/go-git/v5 v5.0.0

M api/go.sum => api/go.sum +2 -2
@@ 31,8 31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.sr.ht/~sircmpwn/core-go v0.0.0-20220112154231-e28d47cf5957 h1:TjvgAEU7+tsevGSTJU1DMhV7Ue6GnvMWKVIRxLfPcu0=
git.sr.ht/~sircmpwn/core-go v0.0.0-20220112154231-e28d47cf5957/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc=
git.sr.ht/~sircmpwn/core-go v0.0.0-20220126083025-75dfa2992b49 h1:+18ujTlMyXa6Zp1qXz7M4ha2eT8MuItSbeQd9w2LQ7I=
git.sr.ht/~sircmpwn/core-go v0.0.0-20220126083025-75dfa2992b49/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc=
git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3 h1:9WCv5cK67s2SiY/R4DWT/OchEsFnfYDz3lbevKxZ4QI=
git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3/go.mod h1:8neHEO3503w/rNtttnR0JFpQgM/GFhaafVwvkPsFIDw=
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk=

M api/graph/resolver.go => api/graph/resolver.go +0 -16
@@ 1,12 1,7 @@
package graph

import (
	"context"
	"fmt"
	"regexp"

	"github.com/99designs/gqlgen/graphql"
	"github.com/vektah/gqlparser/v2/gqlerror"
)

type Resolver struct{}


@@ 15,17 10,6 @@ var (
	repoNameRE = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
)

func gqlErrorf(ctx context.Context, field string, message string, items ...interface{}) *gqlerror.Error {
	err := &gqlerror.Error{
		Message: fmt.Sprintf(message, items...),
		Path:    graphql.GetPath(ctx),
		Extensions: map[string]interface{}{
			"field": field,
		},
	}
	return err
}

var allowedCloneSchemes = map[string]struct{}{
	"https": struct{}{},
	"http":  struct{}{},

M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +8 -7
@@ 26,6 26,7 @@ import (
	"git.sr.ht/~sircmpwn/core-go/database"
	coremodel "git.sr.ht/~sircmpwn/core-go/model"
	"git.sr.ht/~sircmpwn/core-go/server"
	"git.sr.ht/~sircmpwn/core-go/valid"
	corewebhooks "git.sr.ht/~sircmpwn/core-go/webhooks"
	"git.sr.ht/~sircmpwn/git.sr.ht/api/graph/api"
	"git.sr.ht/~sircmpwn/git.sr.ht/api/graph/model"


@@ 141,7 142,7 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi
			&repo.Description, &repo.RawVisibility, &repo.UpstreamURL,
			&repo.Path, &repo.OwnerID); err != nil {
			if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
				return gqlErrorf(ctx, "name", "A repository with this name already exists.")
				return valid.Errorf(ctx, "name", "A repository with this name already exists.")
			}
			return err
		}


@@ 152,9 153,9 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi
			if err != nil {
				return err
			} else if u.Host == "" {
				return gqlErrorf(ctx, "cloneUrl", "Cannot use URL without host")
				return valid.Errorf(ctx, "cloneUrl", "Cannot use URL without host")
			} else if _, ok := allowedCloneSchemes[u.Scheme]; !ok {
				return gqlErrorf(ctx, "cloneUrl", "Unsupported protocol %q", u.Scheme)
				return valid.Errorf(ctx, "cloneUrl", "Unsupported protocol %q", u.Scheme)
			}

			origin := config.GetOrigin(conf, "git.sr.ht", true)


@@ 168,21 169,21 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi
				u.Path = strings.TrimPrefix(u.Path, "/")
				split := strings.SplitN(u.Path, "/", 2)
				if len(split) != 2 {
					return gqlErrorf(ctx, "cloneUrl", "Invalid clone URL")
					return valid.Errorf(ctx, "cloneUrl", "Invalid clone URL")
				}
				canonicalName, repoName := split[0], split[1]
				entity := canonicalName
				if strings.HasPrefix(entity, "~") {
					entity = entity[1:]
				} else {
					return gqlErrorf(ctx, "cloneUrl", "Invalid username")
					return valid.Errorf(ctx, "cloneUrl", "Invalid username")
				}
				repo, err := loaders.ForContext(ctx).
					RepositoriesByOwnerRepoName.Load([2]string{entity, repoName})
				if err != nil {
					return err
				} else if repo == nil {
					return gqlErrorf(ctx, "cloneUrl", "Repository not found")
					return valid.Errorf(ctx, "cloneUrl", "Repository not found")
				}
				cloneURL = &repo.Path
			}


@@ 192,7 193,7 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi
				RecurseSubmodules: git.NoRecurseSubmodules,
			})
			if err != nil {
				return gqlErrorf(ctx, "cloneUrl", "%s", err)
				return valid.Errorf(ctx, "cloneUrl", "%s", err)
			}
		} else {
			var err error