Add support for gitea-pages-allowall repo topic tag

If a repo has been tagged with gitea-pages-allowall we don't need an
extra gitea-pages branch with a toml configuration.

This makes it easier for repos that can just expose every branch
This commit is contained in:
Wim 2022-11-02 18:53:32 +01:00
parent e46079d2b1
commit f9a7e98702
No known key found for this signature in database
2 changed files with 52 additions and 29 deletions

View File

@ -26,11 +26,12 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
// Middleware implements gitea plugin.
type Middleware struct {
Client *gitea.Client `json:"-"`
Server string `json:"server,omitempty"`
Token string `json:"token,omitempty"`
GiteaPages string `json:"gitea_pages,omitempty"`
Domain string `json:"domain,omitempty"`
Client *gitea.Client `json:"-"`
Server string `json:"server,omitempty"`
Token string `json:"token,omitempty"`
GiteaPages string `json:"gitea_pages,omitempty"`
GiteaPagesAllowAll string `json:"gitea_pages_allowall,omitempty"`
Domain string `json:"domain,omitempty"`
}
// CaddyModule returns the Caddy module information.
@ -44,7 +45,7 @@ func (Middleware) CaddyModule() caddy.ModuleInfo {
// Provision provisions gitea client.
func (m *Middleware) Provision(ctx caddy.Context) error {
var err error
m.Client, err = gitea.NewClient(m.Server, m.Token, m.GiteaPages)
m.Client, err = gitea.NewClient(m.Server, m.Token, m.GiteaPages, m.GiteaPagesAllowAll)
return err
}
@ -65,6 +66,8 @@ func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Args(&m.Token)
case "gitea_pages":
d.Args(&m.GiteaPages)
case "gitea_pages_allowall":
d.Args(&m.GiteaPagesAllowAll)
case "domain":
d.Args(&m.Domain)
}

View File

@ -15,35 +15,37 @@ import (
)
type Client struct {
serverURL string
token string
giteapages string
gc *gclient.Client
serverURL string
token string
giteapages string
giteapagesAllowAll string
gc *gclient.Client
}
func NewClient(serverURL, token, giteapages string) (*Client, error) {
func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) {
if giteapages == "" {
giteapages = "gitea-pages"
}
if giteapagesAllowAll == "" {
giteapagesAllowAll = "gitea-pages-allowall"
}
gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion(""))
if err != nil {
return nil, err
}
return &Client{
serverURL: serverURL,
token: token,
gc: gc,
giteapages: giteapages,
serverURL: serverURL,
token: token,
gc: gc,
giteapages: giteapages,
giteapagesAllowAll: giteapagesAllowAll,
}, nil
}
func (c *Client) Open(name, ref string) (fs.File, error) {
if ref == "" {
ref = c.giteapages
}
owner, repo, filepath := splitName(name)
// if repo is empty they want to have the gitea-pages repo
@ -58,7 +60,8 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
}
// we need to check if the repo exists (and allows access)
if !c.allowsPages(owner, repo) {
limited, allowall := c.allowsPages(owner, repo)
if !limited && !allowall {
// if we're checking the gitea-pages and it doesn't exist, return 404
if repo == c.giteapages && !c.hasRepoBranch(owner, repo, c.giteapages) {
return nil, fs.ErrNotExist
@ -69,7 +72,12 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
filepath = repo
repo = c.giteapages
if !c.allowsPages(owner, repo) || !c.hasRepoBranch(owner, repo, c.giteapages) {
if ref == "" {
ref = c.giteapages
}
limited, allowall = c.allowsPages(owner, repo)
if !limited && !allowall || !c.hasRepoBranch(owner, repo, c.giteapages) {
return nil, fs.ErrNotExist
}
}
@ -79,7 +87,7 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
if err := c.readConfig(owner, repo); err != nil {
// we don't need a config for gitea-pages
// no config is only exposing the gitea-pages branch
if repo != c.giteapages {
if repo != c.giteapages && !allowall {
return nil, err
}
@ -90,7 +98,7 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
// always overwrite the ref to the gitea-pages branch
if !hasConfig && (repo == c.giteapages || ref == c.giteapages) {
ref = c.giteapages
} else if !validRefs(ref) {
} else if !validRefs(ref, allowall) {
return nil, fs.ErrNotExist
}
@ -174,7 +182,9 @@ func handleMD(res []byte) ([]byte, error) {
return nil, err
}
res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(meta["title"].(string))...)
title, _ := meta["title"].(string)
res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(title)...)
res = append(res, []byte("</h1>")...)
res = append(res, resmd...)
res = append(res, []byte("</body></html>")...)
@ -196,19 +206,25 @@ func (c *Client) hasRepoBranch(owner, repo, branch string) bool {
return b.Name == branch
}
func (c *Client) allowsPages(owner, repo string) bool {
func (c *Client) allowsPages(owner, repo string) (bool, bool) {
topics, err := c.repoTopics(owner, repo)
if err != nil {
return false
return false, false
}
for _, topic := range topics {
if topic == c.giteapagesAllowAll {
return true, true
}
}
for _, topic := range topics {
if topic == c.giteapages {
return true
return true, false
}
}
return false
return false, false
}
func (c *Client) readConfig(owner, repo string) error {
@ -236,7 +252,11 @@ func splitName(name string) (string, string, string) {
}
}
func validRefs(ref string) bool {
func validRefs(ref string, allowall bool) bool {
if allowall {
return true
}
validrefs := viper.GetStringSlice("allowedrefs")
for _, r := range validrefs {
if r == ref {