~edwargix/git.sr.ht

1838cce8c72a5330b67f0002b6e0cc646c4ade66 — Adnan Maolood 2 years ago 163edba
gitsrht/blueprints/api: Fix canonical_name

Fix canonical_name, which was wrongly named canonicalName, in user
resources returned by the legacy API.

Also take the opportunity to refactor the GraphQL queries such that each
query retrieves only the data it needs.
3 files changed, 100 insertions(+), 91 deletions(-)

M gitsrht/blueprints/api/info.py
M gitsrht/blueprints/manage.py
M gitsrht/repos.py
M gitsrht/blueprints/api/info.py => gitsrht/blueprints/api/info.py +45 -4
@@ 35,11 35,48 @@ def repos_by_user_GET(username):
@info.route("/api/repos", methods=["POST"])
@oauth("info:write")
def repos_POST():
    valid = Validation(request)
    user = current_token.user
    resp = repos.create_repo(valid, user)
    valid = Validation(request)
    name = valid.require("name", friendly_name="Name")
    description = valid.optional("description")
    visibility = valid.optional("visibility")
    if not valid.ok:
        return valid.response

    # Visibility must be uppercase
    if visibility is not None:
        visibility = visibility.upper()

    resp = exec_gql(current_app.site, """
        mutation CreateRepository(
                $name: String!,
                $visibility: Visibility = PUBLIC,
                $description: String) {
            createRepository(
                    name: $name,
                    visibility: $visibility,
                    description: $description) {
                id
                created
                updated
                name
                owner {
                    canonical_name: canonicalName
                    ... on User {
                        name: username
                    }
                }
                description
                visibility
            }
        }
    """, valid=valid, user=user, name=name,
        description=description, visibility=visibility)

    if not valid.ok:
        return valid.response

    resp = resp["createRepository"]
    # Convert visibility back to lowercase
    resp["visibility"] = resp["visibility"].lower()
    return resp, 201


@@ 78,7 115,7 @@ def repos_by_name_PUT(reponame):
                updated
                name
                owner {
                    canonicalName
                    canonical_name: canonicalName
                    ... on User {
                        name: username
                    }


@@ 103,7 140,11 @@ def repos_by_name_DELETE(reponame):
    user = current_token.user
    repo = get_repo(user, reponame, needs=UserAccess.manage)
    repo_id = repo.id
    repos.delete_repo(repo, user)
    exec_gql(current_app.site, """
        mutation DeleteRepository($id: Int!) {
            deleteRepository(id: $id) { id }
        }
    """, user=user, id=repo.id)
    return {}, 204

@info.route("/api/repos/<reponame>/readme", defaults={"username": None})

M gitsrht/blueprints/manage.py => gitsrht/blueprints/manage.py +55 -6
@@ 6,7 6,7 @@ from gitsrht.git import Repository as GitRepository
from srht.config import cfg
from srht.database import db
from srht.flask import session
from srht.graphql import exec_gql, GraphQLError
from srht.graphql import exec_gql
from srht.oauth import current_user, loginrequired, UserType
from srht.validation import Validation
from gitsrht.access import check_access, UserAccess, AccessMode


@@ 27,10 27,31 @@ def create_GET():
@loginrequired
def create_POST():
    valid = Validation(request)
    resp = repos.create_repo(valid)
    name = valid.require("name", friendly_name="Name")
    description = valid.optional("description")
    visibility = valid.require("visibility")
    if not valid.ok:
        return render_template("create.html", **valid.kwargs)

    resp = exec_gql(current_app.site, """
        mutation CreateRepository(
                $name: String!,
                $visibility: Visibility!,
                $description: String) {
            createRepository(
                    name: $name,
                    visibility: $visibility,
                    description: $description) {
                name
            }
        }
    """, valid=valid, name=name, description=description, visibility=visibility)

    if not valid.ok:
        return render_template("create.html", **valid.kwargs)

    resp = resp["createRepository"]

    another = valid.optional("another")
    if another == "on":
        return redirect("/create?another")


@@ 48,9 69,33 @@ def clone():
@loginrequired
def clone_POST():
    valid = Validation(request)
    resp = repos.clone_repo(valid)
    cloneUrl = valid.require("cloneUrl", friendly_name="Clone URL")
    name = valid.require("name", friendly_name="Name")
    description = valid.optional("description")
    visibility = valid.require("visibility")
    if not valid.ok:
        return render_template("clone.html", **valid.kwargs)

    resp = exec_gql(current_app.site, """
        mutation CloneRepository(
                $name: String!,
                $visibility: Visibility!,
                $description: String,
                $cloneUrl: String!) {
            createRepository(name: $name,
                    visibility: $visibility,
                    description: $description,
                    cloneUrl: $cloneUrl) {
                name
            }
        }
    """, valid=valid, name=name, visibility=visibility,
        description=description, cloneUrl=cloneUrl)

    if not valid.ok:
        return render_template("clone.html", **valid.kwargs)

    resp = resp["createRepository"]
    return redirect(url_for("repo.summary",
        owner=current_user.canonical_name, repo=resp["name"]))



@@ 79,7 124,7 @@ def settings_info_POST(owner_name, repo_name):
        ] if valid.source.get(key) is not None
    }

    resp = exec_gql("git.sr.ht", """
    resp = exec_gql(current_app.site, """
        mutation UpdateRepository($id: Int!, $input: RepoInput!) {
            updateRepository(id: $id, input: $input) { id }
        }


@@ 113,7 158,7 @@ def settings_rename_POST(owner_name, repo_name):
        return render_template("settings_rename.html", owner=owner, repo=repo,
                **valid.kwargs)

    resp = exec_gql("git.sr.ht", """
    resp = exec_gql(current_app.site, """
        mutation RenameRepository($id: Int!, $name: String!) {
            updateRepository(id: $id, input: {name: $name}) {
                name


@@ 213,7 258,11 @@ def settings_delete_POST(owner_name, repo_name):
        # Normally we'd redirect but we don't want to fuck up some other repo
        abort(404)
    repo_id = repo.id
    repos.delete_repo(repo)
    exec_gql(current_app.site, """
        mutation DeleteRepository($id: Int!) {
            deleteRepository(id: $id) { id }
        }
    """, id=repo.id)
    session["notice"] = "{}/{} was deleted.".format(
        owner.canonical_name, repo.name)
    return redirect("/" + owner.canonical_name)

M gitsrht/repos.py => gitsrht/repos.py +0 -81
@@ 8,7 8,6 @@ from gitsrht.types import Artifact, Repository, Redirect
from minio import Minio
from srht.config import cfg
from srht.database import db
from srht.graphql import exec_gql, GraphQLError
from werkzeug.utils import secure_filename

repos_path = cfg("git.sr.ht", "repos")


@@ 76,83 75,3 @@ def upload_artifact(valid, repo, commit, f, filename):
    artifact.size = size
    db.session.add(artifact)
    return artifact

def get_repo_path(owner, repo_name):
    return os.path.join(repos_path, "~" + owner.username, repo_name)

def create_repo(valid, user=None):
    repo_name = valid.require("name", friendly_name="Name")
    description = valid.optional("description")
    visibility = valid.optional("visibility")
    if not valid.ok:
        return None

    # Convert the visibility to uppercase. This is needed for the REST API
    # TODO: Remove this when the REST API is phased out
    if visibility is not None:
        visibility = visibility.upper()

    resp = exec_gql("git.sr.ht", """
        mutation CreateRepository(
                $name: String!,
                $visibility: Visibility = PUBLIC,
                $description: String) {
            createRepository(
                    name: $name,
                    visibility: $visibility,
                    description: $description) {
                id
                created
                updated
                name
                owner {
                    canonicalName
                    ... on User {
                        name: username
                    }
                }
                description
                visibility
            }
        }
    """, valid=valid, user=user, name=repo_name,
        description=description, visibility=visibility)

    if not valid.ok:
        return None
    return resp["createRepository"]

def clone_repo(valid):
    cloneUrl = valid.require("cloneUrl", friendly_name="Clone URL")
    name = valid.require("name", friendly_name="Name")
    description = valid.optional("description")
    visibility = valid.optional("visibility")
    if not valid.ok:
        return None

    resp = exec_gql("git.sr.ht", """
        mutation CreateRepository(
                $name: String!,
                $visibility: Visibility = UNLISTED,
                $description: String,
                $cloneUrl: String) {
            createRepository(name: $name,
                    visibility: $visibility,
                    description: $description,
                    cloneUrl: $cloneUrl) {
                name
            }
        }
    """, valid=valid, name=name, visibility=visibility,
        description=description, cloneUrl=cloneUrl)

    if not valid.ok:
        return None
    return resp["createRepository"]

def delete_repo(repo, user=None):
    exec_gql("git.sr.ht", """
        mutation DeleteRepository($id: Int!) {
            deleteRepository(id: $id) { id }
        }
    """, user=user, id=repo.id)