Skip to content

Commit

Permalink
feat!: print contributors in tables
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
shizhMSFT committed Jul 21, 2023
1 parent b65953c commit 6ee6e33
Showing 1 changed file with 102 additions and 5 deletions.
107 changes: 102 additions & 5 deletions cmd/gha/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/shizhMSFT/gha/pkg/analysis"
Expand Down Expand Up @@ -104,11 +105,7 @@ func printSummary(summary *analysis.Summary, includeContributors bool) {
if includeContributors {
fmt.Println()
fmt.Println("### Contributors")
for name, summary := range summary.Authors {
fmt.Println()
fmt.Println("####", name)
printRepositorySummary(summary)
}
printContributors(summary.Authors)
}
}

Expand Down Expand Up @@ -151,6 +148,106 @@ func printRepositorySummary(summary *analysis.RepositorySummary) {
}
}

func printContributors(authors map[string]*analysis.RepositorySummary) {
fmt.Println("#### Issues")
printIssueSummaryTable(authors)
fmt.Println()
fmt.Println("#### Pull Requests")
printPullRequestSummaryTable(authors)
}

func printIssueSummaryTable(authors map[string]*analysis.RepositorySummary) {
// sort by issue counts
issueCounts := make(map[string]int)
for author, summary := range authors {
if summary.Issue.Total == 0 {
continue
}
issueCounts[author] += summary.Issue.Total
}
counts := sort.SliceFromMap(issueCounts, false)

// print header
nameSize := 6 // len("Author")
for _, entry := range counts {
if len(entry.Key) > nameSize {
nameSize = len(entry.Key)
}
}
headerFormat := fmt.Sprintf("| %%-%ds | %%-6s | %%-6s | %%-6s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s |\n", nameSize)
bodyFormat := fmt.Sprintf("| %%-%ds | %%-6d | %%-6d | %%-6d | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s |\n", nameSize)
fmt.Println()
fmt.Printf(headerFormat, "Author", "Total", "Open", "Closed", "Min", "Max", "Mean", "Median", "P90", "P95", "P99")
fmt.Printf("|%s|--------|--------|--------|----------|----------|----------|----------|----------|----------|----------|\n", strings.Repeat("-", nameSize+2))

// print body
for _, entry := range counts {
author := entry.Key
summary := authors[author].Issue
if len(summary.Durations) == 0 {
fmt.Printf(bodyFormat, author, summary.Total, summary.Open, summary.Closed, "", "", "", "", "", "", "")
continue
}

sort.Sort(summary.Durations)
fmt.Printf(bodyFormat, author, summary.Total, summary.Open, summary.Closed,
formatDuration(math.Min(summary.Durations)),
formatDuration(math.Max(summary.Durations)),
formatDuration(math.Mean(summary.Durations)),
formatDuration(math.Median(summary.Durations)),
formatDuration(math.Percentile(summary.Durations, 0.9)),
formatDuration(math.Percentile(summary.Durations, 0.95)),
formatDuration(math.Percentile(summary.Durations, 0.99)),
)
}
}

func printPullRequestSummaryTable(authors map[string]*analysis.RepositorySummary) {
// sort by pull request counts
prCounts := make(map[string]int)
for author, summary := range authors {
if summary.PullRequest.Total == 0 {
continue
}
prCounts[author] += summary.PullRequest.Total
}
counts := sort.SliceFromMap(prCounts, false)

// print header
nameSize := 6 // len("Author")
for _, entry := range counts {
if len(entry.Key) > nameSize {
nameSize = len(entry.Key)
}
}
headerFormat := fmt.Sprintf("| %%-%ds | %%-6s | %%-6s | %%-6s | %%-6s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s |\n", nameSize)
bodyFormat := fmt.Sprintf("| %%-%ds | %%-6d | %%-6d | %%-6d | %%-6d | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s | %%-8s |\n", nameSize)
fmt.Println()
fmt.Printf(headerFormat, "Author", "Total", "Open", "Closed", "Merged", "Min", "Max", "Mean", "Median", "P90", "P95", "P99")
fmt.Printf("|%s|--------|--------|--------|--------|----------|----------|----------|----------|----------|----------|----------|\n", strings.Repeat("-", nameSize+2))

// print body
for _, entry := range counts {
author := entry.Key
summary := authors[author].PullRequest
if len(summary.Durations) == 0 {
fmt.Printf(bodyFormat, author, summary.Total, summary.Open, summary.Closed, summary.Merged, "", "", "", "", "", "", "")
continue
}

sort.Sort(summary.Durations)
fmt.Printf(bodyFormat, author, summary.Total, summary.Open, summary.Closed, summary.Merged,
formatDuration(math.Min(summary.Durations)),
formatDuration(math.Max(summary.Durations)),
formatDuration(math.Mean(summary.Durations)),
formatDuration(math.Median(summary.Durations)),
formatDuration(math.Percentile(summary.Durations, 0.9)),
formatDuration(math.Percentile(summary.Durations, 0.95)),
formatDuration(math.Percentile(summary.Durations, 0.99)),
)
}
}

func formatDuration(d time.Duration) string {
seconds := d / time.Second
minutes, seconds := seconds/60, seconds%60
Expand Down

0 comments on commit 6ee6e33

Please sign in to comment.