Skip to content

Commit

Permalink
add sort capabilities to user contributions (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
k1nho authored Nov 21, 2023
1 parent f9c7694 commit fa9808a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/insights/user-contributions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/csv"
"errors"
"fmt"
"sort"
"strconv"
"sync"

Expand Down Expand Up @@ -38,6 +39,9 @@ type userContributionsOptions struct {

// Output is the formatting style for command output
Output string

// Sort is the column to be used to sort user contributions (total, commits, pr, none)
Sort string
}

// NewUserContributionsCommand returns a new user-contributions command
Expand Down Expand Up @@ -68,6 +72,7 @@ func NewUserContributionsCommand() *cobra.Command {
cmd.Flags().StringVarP(&opts.FilePath, constants.FlagNameFile, "f", "", "Path to yaml file containing an array of git repository urls")
cmd.Flags().Int32VarP(&opts.Period, constants.FlagNamePeriod, "p", 30, "Number of days, used for query filtering")
cmd.Flags().StringSliceVarP(&opts.Users, "users", "u", []string{}, "Inclusive comma separated list of GitHub usernames to filter for")
cmd.Flags().StringVarP(&opts.Sort, "sort", "s", "none", "Sort user contributions by (total, commits, prs)")

return cmd
}
Expand Down Expand Up @@ -130,6 +135,10 @@ func (opts *userContributionsOptions) run(ctx context.Context) error {
return allErrors
}

if opts.Sort != "none" {
sortUserContributions(insights, opts.Sort)
}

for _, insight := range insights {
output, err := insight.BuildOutput(opts.Output)
if err != nil {
Expand Down Expand Up @@ -283,3 +292,21 @@ func findAllUserContributionsInsights(ctx context.Context, opts *userContributio

return repoUserContributionsInsightGroup, nil
}

func sortUserContributions(ucig []*userContributionsInsightGroup, sortBy string) {
for _, group := range ucig {
if group != nil {
sort.SliceStable(group.Insights, func(i, j int) bool {
switch sortBy {
case "total":
return group.Insights[i].TotalContributions > group.Insights[j].TotalContributions
case "prs":
return group.Insights[i].PrsCreated > group.Insights[j].PrsCreated
case "commits":
return group.Insights[i].Commits > group.Insights[j].Commits
}
return group.Insights[i].Login < group.Insights[j].Login
})
}
}
}

0 comments on commit fa9808a

Please sign in to comment.