From ce5185f551b707fe8dd8db8b5cbffd46e96cacc0 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 15 Jun 2024 19:07:14 +0200 Subject: feat: Add per repository about page --- pkg/config/config.go | 6 ++++++ pkg/config/config_test.go | 6 ++++++ pkg/handler/git/handler.go | 25 ++++++++++++++++++++++--- pkg/service/git.go | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) (limited to 'pkg') diff --git a/pkg/config/config.go b/pkg/config/config.go index 6ac6d05..3759b7c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -42,6 +42,7 @@ type ( Path string Description string Public bool + About string } // ConfigurationRepository represents the configuration repository (as in @@ -214,6 +215,10 @@ func setRepositories(block scfg.Block, repositories *[]*GitRepositoryConfigurati if err := setBool(d, &repository.Public); err != nil { return err } + case "about": + if err := setString(d, &repository.About); err != nil { + return err + } } } @@ -249,6 +254,7 @@ func defaultRepisotryConfiguration(path string) *GitRepositoryConfiguration { Name: filepath.Base(path), Description: "", Public: false, + About: "README.md", } } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 2d779c5..8c1d27e 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -53,6 +53,7 @@ scan "/srv/git" { Path: "/srv/git/cerrado.git", Description: "", Public: false, + About: "README.md", }, }, }, @@ -64,6 +65,7 @@ repository /srv/git/cerrado.git { name cerrado description "Single person forge" public true + about readme.txt }`, expectedConfig: &configuration{ Scan: defaultScan(), @@ -74,6 +76,7 @@ repository /srv/git/cerrado.git { Path: "/srv/git/cerrado.git", Description: "Single person forge", Public: true, + About: "readme.txt", }, }, }, @@ -111,6 +114,7 @@ repository /srv/git/cerrado.git { name cerrado description "Single person forge" public true + about readme.txt }`, expectedConfig: &configuration{ Scan: &scan{ @@ -124,12 +128,14 @@ repository /srv/git/cerrado.git { Path: "/srv/git/linux.git", Description: "", Public: false, + About: "README.md", }, { Name: "cerrado", Path: "/srv/git/cerrado.git", Description: "Single person forge", Public: true, + About: "readme.txt", }, }, }, diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 899f61e..25505ba 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -33,6 +33,7 @@ type ( GetHead(name string) (*plumbing.Reference, error) GetTree(name, ref, path string) (*object.Tree, error) GetFileContent(name, ref, path string) (string, error) + GetAbout(name string) (string, error) ListTags(name string) ([]*plumbing.Reference, error) ListBranches(name string) ([]*plumbing.Reference, error) } @@ -126,10 +127,28 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) error { if err != nil { return err } + + file, err := g.gitService.GetAbout(name) + if err != nil { + return err + } + + extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock + p := parser.NewWithExtensions(extensions) + doc := p.Parse([]byte(file)) + + htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank + opts := markdownhtml.RendererOptions{Flags: htmlFlag} + renderer := markdownhtml.NewRenderer(opts) + + bs := markdown.Render(doc, renderer) + gitList := &templates.GitItemPage{ - Name: name, - Ref: ref.Name().Short(), - GitItemBase: &templates.GitItemAboutPage{}, + Name: name, + Ref: ref.Name().Short(), + GitItemBase: &templates.GitItemAboutPage{ + About: bs, + }, } templates.WritePageTemplate(w, gitList) return nil diff --git a/pkg/service/git.go b/pkg/service/git.go index 2165abe..6bb6e9e 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -128,6 +128,24 @@ func (g *GitService) GetFileContent(name, ref, path string) (string, error) { return repo.FileContent(path) } +func (g *GitService) GetAbout(name string) (string, error) { + r := g.configRepo.GetByName(name) + if r == nil { + return "", RepositoryNotFoundErr + } + + repo, err := git.OpenRepository(r.Path) + if err != nil { + return "", err + } + err = repo.SetRef("") + if err != nil { + return "", err + } + + return repo.FileContent(r.About) +} + func (g *GitService) ListTags(name string) ([]*plumbing.Reference, error) { r := g.configRepo.GetByName(name) if r == nil { -- cgit v1.2.3