新增:readme中文增加一条规则以及用户贡献占比

This commit is contained in:
yystopf 2023-03-28 14:14:49 +08:00
parent d101c834d8
commit 5ead9a2271
2 changed files with 26 additions and 12 deletions

View File

@ -3,6 +3,7 @@ package git
import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"
@ -11,13 +12,14 @@ import (
)
type RepoContributor struct {
Name string `json:"name"`
Email string `json:"email"`
Commits int `json:"commits"`
Name string `json:"name"`
Email string `json:"email"`
Commits int `json:"commits"`
ContributionPerc string `json:"contribution_perc"`
}
func GetRepoContributors(repo *gitea_git.Repository, page, pageSize int) (int, []*RepoContributor, error) {
var total, skip int
var total, totalContributions, skip int
var contributors []*RepoContributor
skip = (page - 1) * pageSize
@ -44,19 +46,20 @@ func GetRepoContributors(repo *gitea_git.Repository, page, pageSize int) (int, [
scanner.Split(bufio.ScanLines)
var ca int
for scanner.Scan() {
l := strings.TrimSpace(scanner.Text())
commits := l[0:strings.Index(l, "\t")]
commitsInt, err := strconv.Atoi(commits)
if err != nil {
return err
}
name := l[strings.Index(l, "\t")+1 : strings.Index(l, " <")]
email := l[strings.Index(l, "<")+1 : strings.Index(l, ">")]
totalContributions += commitsInt
total++
if skip > 0 {
skip--
} else {
if ca < pageSize {
l := strings.TrimSpace(scanner.Text())
commits := l[0:strings.Index(l, "\t")]
commitsInt, err := strconv.Atoi(commits)
if err != nil {
return err
}
name := l[strings.Index(l, "\t")+1 : strings.Index(l, " <")]
email := l[strings.Index(l, "<")+1 : strings.Index(l, ">")]
contributors = append(contributors, &RepoContributor{
Commits: commitsInt,
Name: name,
@ -72,5 +75,10 @@ func GetRepoContributors(repo *gitea_git.Repository, page, pageSize int) (int, [
},
})
for _, cont := range contributors {
fperc := fmt.Sprintf("%.2f", float64(cont.Commits)*100/float64(totalContributions))
cont.ContributionPerc = fperc + "%"
}
return total, contributors, nil
}

View File

@ -149,6 +149,9 @@ func GetReadmeContents(ctx *context.APIContext) {
if strings.ToLower(file.Name) == "readme_zh.md" {
readmezhPath = file.Name
}
if strings.ToLower(file.Name) == "readme_cn.md" {
readmezhPath = file.Name
}
if strings.ToLower(file.Name) == "readme" {
readmePath = file.Name
}
@ -205,6 +208,9 @@ func GetReadmeContentsByPath(ctx *context.APIContext) {
if strings.ToLower(file.Name) == "readme_zh.md" {
readmezhPath = file.Name
}
if strings.ToLower(file.Name) == "readme_ch.md" {
readmezhPath = file.Name
}
if strings.ToLower(file.Name) == "readme" {
readmePath = file.Name
}