~sircmpwn/sr.ht-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH] services.py: use GQL to fetch git repos

Details
Message ID
<20240724145209.10450-1-sir@cmpwn.com>
DKIM signature
pass
Download raw message
Patch: +58 -7
---
As part of a broader effort to eliminate internal users of the legacy
API. If this looks good I'll start rolling a similar change out for
other services in services.py.

 hubsrht/services.py                   | 63 ++++++++++++++++++++++++---
 hubsrht/templates/sources-select.html |  2 +-
 2 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/hubsrht/services.py b/hubsrht/services.py
index d15245f..b58649a 100644
--- a/hubsrht/services.py
+++ b/hubsrht/services.py
@@ -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 = """
diff --git a/hubsrht/templates/sources-select.html b/hubsrht/templates/sources-select.html
index 4836d67..48f19e7 100644
--- a/hubsrht/templates/sources-select.html
+++ b/hubsrht/templates/sources-select.html
@@ -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>
-- 
2.45.2
Reply to thread Export thread (mbox)