Browse Source

prepare contributors info data for repo home

tags/v1.21.7^2
avadesian 4 years ago
parent
commit
a713600ff8
2 changed files with 54 additions and 0 deletions
  1. +31
    -0
      modules/git/repo.go
  2. +23
    -0
      routers/repo/view.go

+ 31
- 0
modules/git/repo.go View File

@@ -434,3 +434,34 @@ func GetDivergingCommits(repoPath string, baseBranch string, targetBranch string

return DivergeObject{ahead, behind}, nil
}

type Contributor struct {
CommitCnt int
Committer string
Email string
}

func GetContributors(repoPath string) ([]Contributor, error){
cmd := NewCommand("shortlog", "-sne", "--all")
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return nil, err
}
contributorRows := strings.Split(stdout, "\n")
if len(contributorRows) > 0 {
contributorsInfo := make([]Contributor, len(contributorRows))
for i := 0; i < len(contributorRows); i++ {
var oneCount string = strings.Trim(contributorRows[i], " ")
number := oneCount[0:strings.Index(oneCount," ")]
commitCnt, _ := strconv.Atoi(number)
committer := oneCount[strings.Index(oneCount," "):strings.LastIndex(oneCount," ")]
committer = strings.Trim(committer, " ")
email := oneCount[strings.LastIndex(oneCount," "):]
contributorsInfo[i] = Contributor{
commitCnt, committer, email,
}
}
return contributorsInfo, nil
}
return nil, nil
}

+ 23
- 0
routers/repo/view.go View File

@@ -567,9 +567,32 @@ func safeURL(address string) string {
return u.String()
}

type ContributorInfo struct {
UserInfo *models.User
Email string
}
// Home render repository home page
func Home(ctx *context.Context) {
if len(ctx.Repo.Units) > 0 {
//get repo contributors info
contributors, err := git.GetContributors(ctx.Repo.Repository.RepoPath())
if err != nil && contributors != nil {
fmt.Printf("contributors:%v",contributors)
contributorInfos := make([]*ContributorInfo, len(contributors))
for _, c := range contributors {
user,err := models.GetUserByEmail(c.Email)
if err != nil {
contributorInfos = append(contributorInfos, &ContributorInfo{
user, c.Email,
})
}else{
contributorInfos = append(contributorInfos, &ContributorInfo{
nil, c.Email,
})
}
}
ctx.Data["ContributorInfo"] = contributorInfos
}
if ctx.Repo.Repository.IsBeingCreated() {
task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
if err != nil {


Loading…
Cancel
Save