~sircmpwn/sr.ht-packages

git.sr.ht: api: expose current user's repository access level v1 REJECTED

Conrad Hoffmann: 2
 api: expose current user's repository access level
 api: rename accessControlList() to acls()

 5 files changed, 48 insertions(+), 4 deletions(-)
#870500 running alpine.yml
#870501 running archlinux.yml
#870502 running debian.yml
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/sr.ht-packages/patches/36425/mbox | git am -3
Learn more about email & git

[PATCH git.sr.ht 1/2] api: expose current user's repository access level Export this patch

For a given repository, the `access` attribute contains the AccessMode
that applies to the current user.
---
 api/graph/schema.graphqls     |  4 ++++
 api/graph/schema.resolvers.go | 40 +++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/api/graph/schema.graphqls b/api/graph/schema.graphqls
index e13016e..162890c 100644
--- a/api/graph/schema.graphqls
+++ b/api/graph/schema.graphqls
@@ -126,6 +126,10 @@ type Repository {
  """
  readme: String

  "The access that applies to this user for this repository"
  access: AccessMode! @access(scope: ACLS, kind: RO)

  # Only available to the repository owner
  accessControlList(cursor: Cursor): ACLCursor! @access(scope: ACLS, kind: RO)

  ## Plumbing API:
diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go
index 7ed274e..54aafcd 100644
--- a/api/graph/schema.resolvers.go
+++ b/api/graph/schema.resolvers.go
@@ -1056,6 +1056,46 @@ func (r *repositoryResolver) Owner(ctx context.Context, obj *model.Repository) (
	return loaders.ForContext(ctx).UsersByID.Load(obj.OwnerID)
}

// Access is the resolver for the access field.
func (r *repositoryResolver) Access(ctx context.Context, obj *model.Repository) (model.AccessMode, error) {
	currentUser := auth.ForContext(ctx)
	if obj.OwnerID == currentUser.UserID {
		return model.AccessModeRw, nil
	}

	mode := model.AccessModeRo
	if err := database.WithTx(ctx, &sql.TxOptions{
		Isolation: 0,
		ReadOnly:  true,
	}, func(tx *sql.Tx) error {
		var rawAccessMode string
		row := tx.QueryRowContext(ctx, `
			SELECT mode
			FROM access
			WHERE
				access.repo_id = $1 AND
				access.user_id = $2;
		`, obj.ID, currentUser.UserID)

		if err := row.Scan(&rawAccessMode); err != nil {
			if err == sql.ErrNoRows {
				return nil
			}
			return err
		}

		mode = model.AccessMode(strings.ToUpper(rawAccessMode))
		if !mode.IsValid() {
			panic(fmt.Errorf("Invalid access mode '%s'", rawAccessMode))
		}
		return nil
	}); err != nil {
		return mode, err
	}

	return mode, nil
}

// AccessControlList is the resolver for the accessControlList field.
func (r *repositoryResolver) AccessControlList(ctx context.Context, obj *model.Repository, cursor *coremodel.Cursor) (*model.ACLCursor, error) {
	if cursor == nil {
-- 
2.38.1
Please disregard, wrong list. Sorry 'bout that!

[PATCH git.sr.ht 2/2] api: rename accessControlList() to acls() Export this patch

To be consistent with other services (e.g. todo.sr.ht).
---
 api/graph/complexity.go       | 2 +-
 api/graph/schema.graphqls     | 2 +-
 api/graph/schema.resolvers.go | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/api/graph/complexity.go b/api/graph/complexity.go
index 70948e1..dae4213 100644
--- a/api/graph/complexity.go
+++ b/api/graph/complexity.go
@@ -21,7 +21,7 @@ func ApplyComplexity(conf *api.Config) {
		}
		return c
	}
	conf.Complexity.Repository.AccessControlList = func(c int, cursor *model.Cursor) int {
	conf.Complexity.Repository.Acls = func(c int, cursor *model.Cursor) int {
		return cursorComplexity(c, cursor)
	}
	conf.Complexity.Repository.Log = func(c int, cursor *model.Cursor, from *string) int {
diff --git a/api/graph/schema.graphqls b/api/graph/schema.graphqls
index 162890c..f2f8284 100644
--- a/api/graph/schema.graphqls
+++ b/api/graph/schema.graphqls
@@ -130,7 +130,7 @@ type Repository {
  access: AccessMode! @access(scope: ACLS, kind: RO)

  # Only available to the repository owner
  accessControlList(cursor: Cursor): ACLCursor! @access(scope: ACLS, kind: RO)
  acls(cursor: Cursor): ACLCursor! @access(scope: ACLS, kind: RO)

  ## Plumbing API:

diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go
index 54aafcd..f86001c 100644
--- a/api/graph/schema.resolvers.go
+++ b/api/graph/schema.resolvers.go
@@ -1096,8 +1096,8 @@ func (r *repositoryResolver) Access(ctx context.Context, obj *model.Repository)
	return mode, nil
}

// AccessControlList is the resolver for the accessControlList field.
func (r *repositoryResolver) AccessControlList(ctx context.Context, obj *model.Repository, cursor *coremodel.Cursor) (*model.ACLCursor, error) {
// Acls is the resolver for the acls field.
func (r *repositoryResolver) Acls(ctx context.Context, obj *model.Repository, cursor *coremodel.Cursor) (*model.ACLCursor, error) {
	if cursor == nil {
		cursor = coremodel.NewCursor(nil)
	}
-- 
2.38.1