~sircmpwn/hub.sr.ht

840a8b829edcb90ad068200c2ae2fc15bbace0ee — Drew DeVault a month ago 89b68f1
services.py: use GQL to fetch git repos
2 files changed, 58 insertions(+), 7 deletions(-)

M hubsrht/services.py
M hubsrht/templates/sources-select.html
M hubsrht/services.py => hubsrht/services.py +57 -6
@@ 6,6 6,7 @@ from abc import ABC
from flask import url_for
from markupsafe import Markup, escape
from srht.api import ensure_webhooks, encrypt_request_authorization, get_results
from srht.graphql import gql_time
from srht.markdown import markdown, sanitize
from srht.config import get_origin, cfg



@@ 80,14 81,64 @@ class GitService(SrhtService):
        super().__init__()

    def get_repos(self, user):
        return get_results(f"{_gitsrht}/api/repos", user)
        get_repos_query = """
        query GetRepos($cursor: Cursor) {
            me {
                repositories(cursor: $cursor) {
                    results {
                        id
                        name
                        updated
                        owner {
                            canonicalName
                        }
                    }
                    cursor
                }
            }
        }
        """

        repos = []
        cursor = None
        while True:
            r = self.post(user, None, f"{_gitsrht_api}/query", {
                "query": get_repos_query,
                "variables": {
                    "cursor": cursor,
                },
            })
            result = r["data"]["me"]["repositories"]
            repos.extend(result["results"])
            cursor = result["cursor"]
            if cursor is None:
                break

        for repo in repos:
            repo["updated"] = gql_time(repo["updated"])

        return repos

    def get_repo(self, user, repo_name):
        r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}",
                headers=encrypt_request_authorization(user))
        if r.status_code != 200:
            raise Exception(r.text)
        return r.json()
        get_repo_query = """
        query GetRepo($repoName: String!) {
            me {
                repository(name: $repoName) {
                    id
                    name
                    description
                    visibility
                }
            }
        }
        """
        r = self.post(user, None, f"{_gitsrht_api}/query", {
            "query": get_repo_query,
            "variables": {
                "repoName": repo_name,
            },
        })
        return r["data"]["me"]["repository"]

    def get_readme(self, user, repo_name, repo_url):
        readme_query = """

M hubsrht/templates/sources-select.html => hubsrht/templates/sources-select.html +1 -1
@@ 75,7 75,7 @@
          >Select repo&nbsp;{{ icon("caret-right") }}</button>
          {% endif %}
          <a
            href="{{origin}}/{{ repo["owner"]["canonical_name"] }}/{{repo["name"]}}"
            href="{{origin}}/{{ repo["owner"]["canonicalName"] }}/{{repo["name"]}}"
            target="_blank"
            rel="noopener"
          >{{ repo["name"] }}</a>