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

@ -30,6 +30,7 @@ type Middleware struct {
Server string `json:"server,omitempty"` Server string `json:"server,omitempty"`
Token string `json:"token,omitempty"` Token string `json:"token,omitempty"`
GiteaPages string `json:"gitea_pages,omitempty"` GiteaPages string `json:"gitea_pages,omitempty"`
GiteaPagesAllowAll string `json:"gitea_pages_allowall,omitempty"`
Domain string `json:"domain,omitempty"` Domain string `json:"domain,omitempty"`
} }
@ -44,7 +45,7 @@ func (Middleware) CaddyModule() caddy.ModuleInfo {
// Provision provisions gitea client. // Provision provisions gitea client.
func (m *Middleware) Provision(ctx caddy.Context) error { func (m *Middleware) Provision(ctx caddy.Context) error {
var err 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 return err
} }
@ -65,6 +66,8 @@ func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Args(&m.Token) d.Args(&m.Token)
case "gitea_pages": case "gitea_pages":
d.Args(&m.GiteaPages) d.Args(&m.GiteaPages)
case "gitea_pages_allowall":
d.Args(&m.GiteaPagesAllowAll)
case "domain": case "domain":
d.Args(&m.Domain) d.Args(&m.Domain)
} }

View File

@ -18,14 +18,19 @@ type Client struct {
serverURL string serverURL string
token string token string
giteapages string giteapages string
giteapagesAllowAll string
gc *gclient.Client gc *gclient.Client
} }
func NewClient(serverURL, token, giteapages string) (*Client, error) { func NewClient(serverURL, token, giteapages, giteapagesAllowAll string) (*Client, error) {
if giteapages == "" { if giteapages == "" {
giteapages = "gitea-pages" giteapages = "gitea-pages"
} }
if giteapagesAllowAll == "" {
giteapagesAllowAll = "gitea-pages-allowall"
}
gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion("")) gc, err := gclient.NewClient(serverURL, gclient.SetToken(token), gclient.SetGiteaVersion(""))
if err != nil { if err != nil {
return nil, err return nil, err
@ -36,14 +41,11 @@ func NewClient(serverURL, token, giteapages string) (*Client, error) {
token: token, token: token,
gc: gc, gc: gc,
giteapages: giteapages, giteapages: giteapages,
giteapagesAllowAll: giteapagesAllowAll,
}, nil }, nil
} }
func (c *Client) Open(name, ref string) (fs.File, error) { func (c *Client) Open(name, ref string) (fs.File, error) {
if ref == "" {
ref = c.giteapages
}
owner, repo, filepath := splitName(name) owner, repo, filepath := splitName(name)
// if repo is empty they want to have the gitea-pages repo // 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) // 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 we're checking the gitea-pages and it doesn't exist, return 404
if repo == c.giteapages && !c.hasRepoBranch(owner, repo, c.giteapages) { if repo == c.giteapages && !c.hasRepoBranch(owner, repo, c.giteapages) {
return nil, fs.ErrNotExist return nil, fs.ErrNotExist
@ -69,7 +72,12 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
filepath = repo filepath = repo
repo = c.giteapages 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 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 { if err := c.readConfig(owner, repo); err != nil {
// we don't need a config for gitea-pages // we don't need a config for gitea-pages
// no config is only exposing the gitea-pages branch // no config is only exposing the gitea-pages branch
if repo != c.giteapages { if repo != c.giteapages && !allowall {
return nil, err 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 // always overwrite the ref to the gitea-pages branch
if !hasConfig && (repo == c.giteapages || ref == c.giteapages) { if !hasConfig && (repo == c.giteapages || ref == c.giteapages) {
ref = c.giteapages ref = c.giteapages
} else if !validRefs(ref) { } else if !validRefs(ref, allowall) {
return nil, fs.ErrNotExist return nil, fs.ErrNotExist
} }
@ -174,7 +182,9 @@ func handleMD(res []byte) ([]byte, error) {
return nil, err 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, []byte("</h1>")...)
res = append(res, resmd...) res = append(res, resmd...)
res = append(res, []byte("</body></html>")...) res = append(res, []byte("</body></html>")...)
@ -196,19 +206,25 @@ func (c *Client) hasRepoBranch(owner, repo, branch string) bool {
return b.Name == branch 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) topics, err := c.repoTopics(owner, repo)
if err != nil { if err != nil {
return false return false, false
}
for _, topic := range topics {
if topic == c.giteapagesAllowAll {
return true, true
}
} }
for _, topic := range topics { for _, topic := range topics {
if topic == c.giteapages { if topic == c.giteapages {
return true return true, false
} }
} }
return false return false, false
} }
func (c *Client) readConfig(owner, repo string) error { 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") validrefs := viper.GetStringSlice("allowedrefs")
for _, r := range validrefs { for _, r := range validrefs {
if r == ref { if r == ref {