Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add sort capabilities to user contributions #65

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
}
}
}
Loading