1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-21 22:29:41 +02:00

improve github commit list presentation

This commit is contained in:
Elliott Stoneham 2016-08-22 17:29:43 +01:00
parent 7c72b72bae
commit ba1512c7cd
3 changed files with 146 additions and 133 deletions

View file

@ -15,30 +15,29 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"sort" "sort"
"time"
"github.com/documize/community/core/log" "github.com/documize/community/core/log"
gogithub "github.com/google/go-github/github" gogithub "github.com/google/go-github/github"
) )
type githubBranchCommits struct { const commitTimeFormat = "January 2 2006, 15:04"
Name string `json:"name"`
URL string `json:"url"`
CommitCount int `json:"commitCount"`
Days []githubDayCommits `json:"days"`
}
type githubDayCommits struct {
Day string `json:"day"`
Commits []githubCommit `json:"commits"`
}
type githubCommit struct { type githubCommit struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
ShowRepo bool `json:"showRepo"`
Branch string `json:"branch"`
ShowBranch bool `json:"ShowBranch"`
Date string `json:"date"` Date string `json:"date"`
Message string `json:"message"` BinDate time.Time `json:"-"`
URL template.URL `json:"url"` ShowDate bool `json:"ShowDate"`
Name string `json:"name"` Name string `json:"name"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
ShowUser bool `json:"ShowUser"`
Message string `json:"message"`
URL template.URL `json:"url"`
} }
type githubAuthorStats struct { type githubAuthorStats struct {
@ -50,6 +49,24 @@ type githubAuthorStats struct {
ClosedIssues int `json:"closedIssues"` ClosedIssues int `json:"closedIssues"`
} }
// order commits in a way that makes sense of the table
type orderCommits []githubCommit
func (s orderCommits) Len() int { return len(s) }
func (s orderCommits) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s orderCommits) Less(i, j int) bool {
if s[i].Repo == s[j].Repo {
if s[i].Branch == s[j].Branch {
if s[i].BinDate == s[j].BinDate {
return s[i].Name < s[j].Name
}
return s[i].BinDate.Before(s[j].BinDate)
}
return s[i].Branch < s[j].Branch
}
return s[i].Repo < s[j].Repo
}
// sort stats in order that that should be presented. // sort stats in order that that should be presented.
type asToSort []githubAuthorStats type asToSort []githubAuthorStats
@ -70,7 +87,7 @@ func (s branchByID) Less(i, j int) bool {
const tagCommitsData = "commitsData" const tagCommitsData = "commitsData"
func getCommits(client *gogithub.Client, config *githubConfig) ([]githubBranchCommits, []githubAuthorStats, error) { func getCommits(client *gogithub.Client, config *githubConfig) ([]githubCommit, []githubAuthorStats, error) {
// first make sure we've got all the branches // first make sure we've got all the branches
for _, orb := range config.Lists { for _, orb := range config.Lists {
@ -100,19 +117,19 @@ func getCommits(client *gogithub.Client, config *githubConfig) ([]githubBranchCo
} }
} }
} }
sort.Stable(branchByID(config.Lists)) sort.Sort(branchByID(config.Lists))
authorStats := make(map[string]githubAuthorStats) authorStats := make(map[string]githubAuthorStats)
contribBranch := make(map[string]map[string]struct{}) contribBranch := make(map[string]map[string]struct{})
overall := []githubBranchCommits{} overall := []githubCommit{}
for _, orb := range config.Lists { for _, orb := range config.Lists {
if orb.Included { if orb.Included {
opts := &gogithub.CommitsListOptions{ opts := &gogithub.CommitsListOptions{
SHA: config.Branch, SHA: orb.Name,
ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}} ListOptions: gogithub.ListOptions{PerPage: config.BranchLines}}
if config.SincePtr != nil { if config.SincePtr != nil {
@ -125,38 +142,25 @@ func getCommits(client *gogithub.Client, config *githubConfig) ([]githubBranchCo
return nil, nil, err return nil, nil, err
} }
day := "" thisBranch := fmt.Sprintf("%s:%s", orb.Repo, orb.Name)
newDay := ""
ret := []githubDayCommits{}
thisBranch := fmt.Sprintf("%s/%s:%s", orb.Owner, orb.Repo, orb.Name) for _, v := range guff {
for k, v := range guff {
if guff[k].Commit != nil {
if guff[k].Commit.Committer.Date != nil {
y, m, d := (*guff[k].Commit.Committer.Date).Date()
newDay = fmt.Sprintf("%s %d, %d", m.String(), d, y)
}
}
if day != newDay {
day = newDay
ret = append(ret, githubDayCommits{
Day: day,
})
}
var d, m, u string var d, m, u string
var bd time.Time
if v.Commit != nil { if v.Commit != nil {
if v.Commit.Committer.Date != nil { if v.Commit.Committer.Date != nil {
// d = fmt.Sprintf("%v", *v.Commit.Committer.Date) // d = fmt.Sprintf("%v", *v.Commit.Committer.Date)
d = v.Commit.Committer.Date.Format("January 2 2006, 15:04") d = v.Commit.Committer.Date.Format(commitTimeFormat)
bd = *v.Commit.Committer.Date
} }
if v.Commit.Message != nil { if v.Commit.Message != nil {
m = *v.Commit.Message m = *v.Commit.Message
} }
} }
/* Use author rather than committer
// TODO(elliott5) remove this comment
/*
var a, l string var a, l string
if v.Committer != nil { if v.Committer != nil {
if v.Committer.Login != nil { if v.Committer.Login != nil {
@ -184,42 +188,64 @@ func getCommits(client *gogithub.Client, config *githubConfig) ([]githubBranchCo
if v.Author.AvatarURL != nil { if v.Author.AvatarURL != nil {
aa = *v.Author.AvatarURL aa = *v.Author.AvatarURL
} }
cum := authorStats[al]
cum.Author = al
cum.Avatar = aa
cum.CommitCount++
/* TODO review, this code removed as too slow
cmt, _, err := client.Repositories.GetCommit(orb.Owner, orb.Repo, *v.SHA)
if err == nil {
if cmt.Stats != nil {
if cmt.Stats.Total != nil {
cum.TotalChanges += (*cmt.Stats.Total)
}
}
}
*/
authorStats[al] = cum
} }
l := al // use author not committer
a := aa // ditto
ret[len(ret)-1].Commits = append(ret[len(ret)-1].Commits, githubCommit{ overall = append(overall, githubCommit{
Name: al, Owner: orb.Owner,
Repo: orb.Repo,
Branch: orb.Name,
Name: l,
Message: m, Message: m,
Date: d, Date: d,
Avatar: aa, BinDate: bd,
Avatar: a,
URL: template.URL(u), URL: template.URL(u),
}) })
if _, ok := contribBranch[al]; !ok { if _, ok := contribBranch[l]; !ok {
contribBranch[al] = make(map[string]struct{}) contribBranch[l] = make(map[string]struct{})
}
contribBranch[l][thisBranch] = struct{}{}
cum := authorStats[l]
cum.Author = l
cum.Avatar = a
cum.CommitCount++
// TODO review, this code removed as too slow
//cmt, _, err := client.Repositories.GetCommit(orb.Owner, orb.Repo, *v.SHA)
//if err == nil {
// if cmt.Stats != nil {
// if cmt.Stats.Total != nil {
// cum.TotalChanges += (*cmt.Stats.Total)
// }
// }
//}
//
authorStats[l] = cum
}
} }
contribBranch[al][thisBranch] = struct{}{}
} }
overall = append(overall, githubBranchCommits{ sort.Sort(orderCommits(overall))
Name: thisBranch,
URL: fmt.Sprintf("https://github.com/%s/%s/tree/%s", orb.Owner, orb.Repo, orb.Name), for k := range overall {
Days: ret, overall[k].ShowRepo = true
}) overall[k].ShowBranch = true
overall[k].ShowDate = true
overall[k].ShowUser = true
if k > 0 {
if overall[k].Repo == overall[k-1].Repo {
overall[k].ShowRepo = false
if overall[k].Branch == overall[k-1].Branch {
overall[k].ShowBranch = false
if overall[k].Date == overall[k-1].Date {
overall[k].ShowDate = false
overall[k].ShowUser = overall[k].Name != overall[k-1].Name
}
}
}
} }
} }
@ -240,7 +266,6 @@ func getCommits(client *gogithub.Client, config *githubConfig) ([]githubBranchCo
} }
func refreshCommits(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) { func refreshCommits(gr *githubRender, config *githubConfig, client *gogithub.Client) (err error) {
gr.BranchCommits, gr.AuthorStats, err = getCommits(client, config) gr.BranchCommits, gr.AuthorStats, err = getCommits(client, config)
if err != nil { if err != nil {
log.Error("github refreshCommits:", err) log.Error("github refreshCommits:", err)
@ -251,12 +276,8 @@ func refreshCommits(gr *githubRender, config *githubConfig, client *gogithub.Cli
func renderCommits(payload *githubRender, c *githubConfig) error { func renderCommits(payload *githubRender, c *githubConfig) error {
payload.CommitCount = 0 payload.CommitCount = 0
for l, list := range payload.BranchCommits { for range payload.BranchCommits {
payload.BranchCommits[l].CommitCount = 0 payload.CommitCount++
for _, day := range list.Days {
payload.BranchCommits[l].CommitCount += len(day.Commits)
payload.CommitCount += len(day.Commits)
}
} }
for a := range payload.AuthorStats { for a := range payload.AuthorStats {
@ -274,9 +295,6 @@ func renderCommits(payload *githubRender, c *githubConfig) error {
return nil return nil
} }
// TODO(elliott5) - move to templates.go once working
// COMMITS
func init() { func init() {
reports[tagCommitsData] = report{refreshCommits, renderCommits, commitsTemplate} reports[tagCommitsData] = report{refreshCommits, renderCommits, commitsTemplate}
} }

View file

@ -42,33 +42,28 @@ const commitsTemplate = `
</tr> </tr>
{{end}} {{end}}
</table> </table>
{{range $branch := .BranchCommits}} <table>
<h4> {{range $commit := .BranchCommits}}
There are {{ $branch.CommitCount }} commits for branch <a href="{{$branch.URL}}">{{$branch.Name}}</a>. <tr>
</h4> <td>
<div class="github-board"> {{if $commit.ShowRepo}}{{$commit.Repo}}{{end}}
{{range $data := $branch.Days}} </td>
<div class="github-group-title"> <td>
Commits on {{ $data.Day }} {{if $commit.ShowBranch}}{{$commit.Branch}}{{end}}
</div> </td>
<ul class="github-list"> <td>
{{range $commit := $data.Commits}} {{if $commit.ShowDate}}{{$commit.Date}}{{end}}
<li class="github-commit-item"> </td>
<a class="link" href="{{$commit.URL}}"> <td>
<div class="github-avatar"> {{if $commit.ShowUser}}
<img alt="@{{$commit.Name}}" src="{{$commit.Avatar}}" height="36" width="36"> <img alt="@{{$commit.Name}}" src="{{$commit.Avatar}}" height="36" width="36"> {{$commit.Name}}
</div>
<div class="github-commit-body">
<div class="github-commit-title">{{$commit.Message}}</div>
<div class="github-commit-meta">{{$commit.Name}} committed on {{$commit.Date}}</div>
</div>
</a>
<div class="clearfix" />
</li>
{{end}} {{end}}
</ul> </td>
{{end}} <td>
</div> <a class="link" href="{{$commit.URL}}">{{$commit.Message}}</a>
</td>
</tr>
{{end}} {{end}}
</table>
</div> </div>
` `

View file

@ -28,7 +28,7 @@ type githubRender struct {
List []githubBranch `json:"list"` List []githubBranch `json:"list"`
ShowList bool `json:"showList"` ShowList bool `json:"showList"`
ShowIssueNumbers bool `json:"showIssueNumbers"` ShowIssueNumbers bool `json:"showIssueNumbers"`
BranchCommits []githubBranchCommits `json:"branchCommits"` BranchCommits []githubCommit `json:"branchCommits"`
CommitCount int `json:"commitCount"` CommitCount int `json:"commitCount"`
Issues []githubIssue `json:"issues"` Issues []githubIssue `json:"issues"`
SharedLabels []githubSharedLabel `json:"sharedLabels"` SharedLabels []githubSharedLabel `json:"sharedLabels"`