~sircmpwn/sr.ht-dev

hub.sr.ht: Add support for different named READMEs v3 PROPOSED

Michał Sidor (Michcioperz)
Michał Sidor (Michcioperz): 1
 Add support for different named READMEs

 2 files changed, 39 insertions(+), 26 deletions(-)
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-dev/patches/10773/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH hub.sr.ht v3] Add support for different named READMEs Export this patch

Michał Sidor (Michcioperz)
---
 hubsrht/blueprints/projects.py | 12 ++------
 hubsrht/services.py            | 53 ++++++++++++++++++++++++----------
 2 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/hubsrht/blueprints/projects.py b/hubsrht/blueprints/projects.py
index d1af78d..015bac2 100644
--- a/hubsrht/blueprints/projects.py
+++ b/hubsrht/blueprints/projects.py
@@ -7,10 +7,8 @@ from hubsrht.services import git, hg
from hubsrht.types import Feature, Event, EventType
from hubsrht.types import Project, RepoType, Visibility
from hubsrht.types import SourceRepo, MailingList, Tracker
from jinja2 import Markup
from srht.database import db
from srht.flask import paginate_query
from srht.markdown import markdown
from srht.oauth import current_user, loginrequired
from srht.validation import Validation, valid_url

@@ -26,17 +24,11 @@ def summary_GET(owner, project_name):
        repo = project.summary_repo
        try:
            if repo.repo_type == RepoType.git:
                summary = git.get_readme(owner, repo.name)
                link_prefix = repo.url() + "/blob/refs/heads/master/"
                summary = git.get_readme(owner, repo.name, repo.url())
            elif repo.repo_type == RepoType.hg:
                summary = hg.get_readme(owner, repo.name)
                link_prefix = repo.url() + "/raw/"
                summary = hg.get_readme(owner, repo.name, repo.url())
            else:
                assert False
            # TODO: Pick from default branch
            summary = markdown(summary, ["h1", "h2", "h3", "h4", "h5"],
                    link_prefix=link_prefix)
            summary = Markup(summary)
        except:
            summary = None
            summary_error = True
diff --git a/hubsrht/services.py b/hubsrht/services.py
index 7a342a6..5d8675a 100644
--- a/hubsrht/services.py
+++ b/hubsrht/services.py
@@ -1,7 +1,10 @@
import os.path
import requests
from abc import ABC
from flask import url_for
from jinja2 import Markup, escape
from srht.api import ensure_webhooks, get_authorization, get_results
from srht.markdown import markdown
from srht.config import get_origin

_gitsrht = get_origin("git.sr.ht", external=True, default=None)
@@ -10,6 +13,18 @@ _listsrht = get_origin("lists.sr.ht", external=True, default=None)
_todosrht = get_origin("todo.sr.ht", external=True, default=None)
origin = get_origin("hub.sr.ht")

readme_names = ["README.md", "README.markdown", "README"]

def format_readme(content, filename="", link_prefix=None):
    markdown_exts = ['.md', '.markdown']
    basename, ext = os.path.splitext(filename)
    if ext in markdown_exts:
        html = markdown(content, ["h1", "h2", "h3", "h4", "h5"],
                link_prefix=link_prefix)
    else:
        html = f"<pre>{escape(content)}</pre>"
    return Markup(html)

class SrhtService(ABC):
    def __init__(self):
        self.session = requests.Session()
@@ -40,16 +55,19 @@ class GitService(SrhtService):
            raise Exception(r.text)
        return r.json()

    def get_readme(self, user, repo_name):
    def get_readme(self, user, repo_name, repo_url):
        # TODO: Cache?
        # TODO: Use default branch
        r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/README.md",
                headers=get_authorization(user))
        if r.status_code == 404:
            return ""
        elif r.status_code != 200:
            raise Exception(r.text)
        return r.text
        link_prefix = repo_url + "/blob/refs/heads/master/"
        for readme_name in readme_names:
            r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/{readme_name}",
                    headers=get_authorization(user))
            if r.status_code == 404:
                continue
            elif r.status_code != 200:
                raise Exception(r.text)
            return format_readme(r.text, readme_name, link_prefix)
        return format_readme("")

    def create_repo(self, user, valid):
        name = valid.require("name")
@@ -111,15 +129,18 @@ class HgService(SrhtService):
            raise Exception(r.text)
        return r.json()

    def get_readme(self, user, repo_name):
    def get_readme(self, user, repo_name, repo_url):
        # TODO: Cache?
        r = self.session.get(f"{_hgsrht}/api/repos/{repo_name}/raw/README.md",
                headers=get_authorization(user))
        if r.status_code == 404:
            return ""
        elif r.status_code != 200:
            raise Exception(r.text)
        return r.text
        link_prefix = repo_url + "/raw/"
        for readme_name in readme_names:
            r = self.session.get(f"{_hgsrht}/api/repos/{repo_name}/raw/{readme_name}",
                    headers=get_authorization(user))
            if r.status_code == 404:
                continue
            elif r.status_code != 200:
                raise Exception(r.text)
            return format_readme(r.text, readme_name, link_prefix)
        return format_readme("")

    def create_repo(self, user, valid):
        name = valid.require("name")
-- 
2.26.2