From ef37bd34382ef5cc41c6933298e5edb74dd3274e Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Fri, 15 Oct 2021 10:02:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=BB=9F=E8=AE=A1wiki=E9=A1=B5=E9=9D=A2=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/repo_activity_custom.go | 6 ++- modules/git/repo_stats_custom.go | 87 +++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/models/repo_activity_custom.go b/models/repo_activity_custom.go index 44d20abb3..f6cbf0331 100644 --- a/models/repo_activity_custom.go +++ b/models/repo_activity_custom.go @@ -3,7 +3,11 @@ package models import "code.gitea.io/gitea/modules/git" func GetRepoKPIStats(repo *Repository) (*git.RepoKPIStats, error) { - return git.GetRepoKPIStats(repo.RepoPath()) + wikiPath := "" + if repo.HasWiki() { + wikiPath = repo.WikiPath() + } + return git.GetRepoKPIStats(repo.RepoPath(), wikiPath) } func GetAllUserKPIStats() (map[string]*git.UserKPIStats, error) { diff --git a/modules/git/repo_stats_custom.go b/modules/git/repo_stats_custom.go index 50a740fb0..f7556d5c2 100644 --- a/modules/git/repo_stats_custom.go +++ b/modules/git/repo_stats_custom.go @@ -4,10 +4,13 @@ import ( "bufio" "bytes" "fmt" + "net/url" "sort" "strconv" "strings" "time" + + Log "code.gitea.io/gitea/modules/log" ) type RepoKPIStats struct { @@ -17,6 +20,7 @@ type RepoKPIStats struct { ContributorsAdded int64 CommitsAdded int64 CommitLinesModified int64 + WikiPages int64 Authors []*UserKPITypeStats } @@ -31,7 +35,7 @@ type UserKPITypeStats struct { isNewContributor bool //是否是4个月内的新增贡献者 } -func GetRepoKPIStats(repoPath string) (*RepoKPIStats, error) { +func GetRepoKPIStats(repoPath string, wikiPath string) (*RepoKPIStats, error) { stats := &RepoKPIStats{} contributors, err := GetContributors(repoPath) @@ -76,6 +80,8 @@ func GetRepoKPIStats(repoPath string) (*RepoKPIStats, error) { if err != nil { return nil, fmt.Errorf("FillFromGit: %v", err) } + + setWikiPages(wikiPath, stats) return stats, nil } @@ -282,3 +288,82 @@ func getContributors(repoPath string, fromTime time.Time) ([]Contributor, error) } return nil, nil } + +func setWikiPages(wikiPath string, stats *RepoKPIStats) { + wikiPages := 0 + + if wikiPath == "" { + stats.WikiPages = int64(wikiPages) + return + } + + wikiRepo, commit, err := findWikiRepoCommit(wikiPath) + if err != nil { + if !IsErrNotExist(err) { + Log.Warn("GetBranchCommit", err) + } + stats.WikiPages = int64(wikiPages) + return + } + + // Get page list. + entries, err := commit.ListEntries() + if err != nil { + if wikiRepo != nil { + wikiRepo.Close() + } + Log.Warn("GetBranchCommit", err) + stats.WikiPages = int64(wikiPages) + return + + } + + for _, entry := range entries { + if !entry.IsRegular() { + continue + } + + wikiName, err := filenameToName(entry.Name()) + if err != nil || wikiName == "_Sidebar" || wikiName == "_Footer" { + continue + } + + wikiPages += 1 + + } + //确保wikiRepo用完被关闭 + defer func() { + if wikiRepo != nil { + wikiRepo.Close() + } + }() + stats.WikiPages = int64(wikiPages) + return + +} + +func filenameToName(filename string) (string, error) { + if !strings.HasSuffix(filename, ".md") { + return "", fmt.Errorf("invalid file") + } + basename := filename[:len(filename)-3] + unescaped, err := url.QueryUnescape(basename) + if err != nil { + return "", err + } + return strings.Replace(unescaped, "-", " ", -1), nil +} + +func findWikiRepoCommit(wikiPath string) (*Repository, *Commit, error) { + wikiRepo, err := OpenRepository(wikiPath) + if err != nil { + + return nil, nil, err + } + + commit, err := wikiRepo.GetBranchCommit("master") + if err != nil { + return wikiRepo, nil, err + } + return wikiRepo, commit, nil +}