Add support for different named READMEs

This commit is contained in:
Michał Sidor (Michcioperz) 2020-05-23 21:33:32 +02:00 committed by Drew DeVault
parent 030624ca8d
commit d24e508cd4
3 changed files with 40 additions and 27 deletions

View File

@ -7,10 +7,8 @@ from hubsrht.services import git, hg
from hubsrht.types import Feature, Event, EventType from hubsrht.types import Feature, Event, EventType
from hubsrht.types import Project, RepoType, Visibility from hubsrht.types import Project, RepoType, Visibility
from hubsrht.types import SourceRepo, MailingList, Tracker from hubsrht.types import SourceRepo, MailingList, Tracker
from jinja2 import Markup
from srht.database import db from srht.database import db
from srht.flask import paginate_query from srht.flask import paginate_query
from srht.markdown import markdown
from srht.oauth import current_user, loginrequired from srht.oauth import current_user, loginrequired
from srht.validation import Validation, valid_url from srht.validation import Validation, valid_url
@ -26,17 +24,11 @@ def summary_GET(owner, project_name):
repo = project.summary_repo repo = project.summary_repo
try: try:
if repo.repo_type == RepoType.git: if repo.repo_type == RepoType.git:
summary = git.get_readme(owner, repo.name) summary = git.get_readme(owner, repo.name, repo.url())
link_prefix = repo.url() + "/blob/refs/heads/master/"
elif repo.repo_type == RepoType.hg: elif repo.repo_type == RepoType.hg:
summary = hg.get_readme(owner, repo.name) summary = hg.get_readme(owner, repo.name, repo.url())
link_prefix = repo.url() + "/raw/"
else: else:
assert False assert False
# TODO: Pick from default branch
summary = markdown(summary, ["h1", "h2", "h3", "h4", "h5"],
link_prefix=link_prefix)
summary = Markup(summary)
except: except:
summary = None summary = None
summary_error = True summary_error = True

View File

@ -1,7 +1,10 @@
import os.path
import requests import requests
from abc import ABC from abc import ABC
from flask import url_for from flask import url_for
from jinja2 import Markup, escape
from srht.api import ensure_webhooks, get_authorization, get_results from srht.api import ensure_webhooks, get_authorization, get_results
from srht.markdown import markdown
from srht.config import get_origin from srht.config import get_origin
_gitsrht = get_origin("git.sr.ht", external=True, default=None) _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) _todosrht = get_origin("todo.sr.ht", external=True, default=None)
origin = get_origin("hub.sr.ht") 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): class SrhtService(ABC):
def __init__(self): def __init__(self):
self.session = requests.Session() self.session = requests.Session()
@ -40,16 +55,19 @@ class GitService(SrhtService):
raise Exception(r.text) raise Exception(r.text)
return r.json() return r.json()
def get_readme(self, user, repo_name): def get_readme(self, user, repo_name, repo_url):
# TODO: Cache? # TODO: Cache?
# TODO: Use default branch # TODO: Use default branch
r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/README.md", link_prefix = repo_url + "/blob/refs/heads/master/"
headers=get_authorization(user)) for readme_name in readme_names:
if r.status_code == 404: r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/{readme_name}",
return "" headers=get_authorization(user))
elif r.status_code != 200: if r.status_code == 404:
raise Exception(r.text) continue
return r.text 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): def create_repo(self, user, valid):
name = valid.require("name") name = valid.require("name")
@ -111,15 +129,18 @@ class HgService(SrhtService):
raise Exception(r.text) raise Exception(r.text)
return r.json() return r.json()
def get_readme(self, user, repo_name): def get_readme(self, user, repo_name, repo_url):
# TODO: Cache? # TODO: Cache?
r = self.session.get(f"{_hgsrht}/api/repos/{repo_name}/raw/README.md", link_prefix = repo_url + "/raw/"
headers=get_authorization(user)) for readme_name in readme_names:
if r.status_code == 404: r = self.session.get(f"{_hgsrht}/api/repos/{repo_name}/raw/{readme_name}",
return "" headers=get_authorization(user))
elif r.status_code != 200: if r.status_code == 404:
raise Exception(r.text) continue
return r.text 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): def create_repo(self, user, valid):
name = valid.require("name") name = valid.require("name")

View File

@ -204,7 +204,7 @@
class="pull-right btn btn-primary btn-lg" class="pull-right btn btn-primary btn-lg"
>Use README.md&nbsp;{{ icon("caret-right") }}</button> >Use README.md&nbsp;{{ icon("caret-right") }}</button>
<a <a
href="{{repo.url()}" href="{{repo.url()}}"
target="_blank" target="_blank"
rel="noopener" rel="noopener"
>{{ repo.name }}</a> >{{ repo.name }}</a>