From d10c1ef0bc69d3acd2e9f3ebdab7eee813debe19 Mon Sep 17 00:00:00 2001 From: John McBride Date: Thu, 26 Oct 2023 13:59:43 -0600 Subject: [PATCH] feat: Add filtering for usernames on user-contributions Signed-off-by: John McBride --- cmd/insights/user-contributions.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/insights/user-contributions.go b/cmd/insights/user-contributions.go index 4ab4bb7..4fb2171 100644 --- a/cmd/insights/user-contributions.go +++ b/cmd/insights/user-contributions.go @@ -24,6 +24,12 @@ type userContributionsOptions struct { // Repos is the array of git repository urls Repos []string + // Users is the list of usernames to filter for + Users []string + + // usersMap is a fast access set of usernames built from the Users string slice + usersMap map[string]struct{} + // FilePath is the path to yaml file containing an array of git repository urls FilePath string @@ -61,6 +67,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") return cmd } @@ -71,6 +78,12 @@ func (opts *userContributionsOptions) run(ctx context.Context) error { return err } + opts.usersMap = make(map[string]struct{}) + for _, username := range opts.Users { + // For fast access to list of users to filter out, uses an empty struct + opts.usersMap[username] = struct{}{} + } + var ( waitGroup = new(sync.WaitGroup) errorChan = make(chan error, len(repositories)) @@ -257,12 +270,15 @@ func findAllUserContributionsInsights(ctx context.Context, opts *userContributio } for _, data := range dataPoints { - repoUserContributionsInsightGroup.Insights = append(repoUserContributionsInsightGroup.Insights, userContributionsInsights{ - Login: *data.Login, - Commits: int(data.Commits), - PrsCreated: int(data.PrsCreated), - TotalContributions: int(data.Commits) + int(data.PrsCreated), - }) + _, ok := opts.usersMap[*data.Login] + if len(opts.usersMap) == 0 || ok { + repoUserContributionsInsightGroup.Insights = append(repoUserContributionsInsightGroup.Insights, userContributionsInsights{ + Login: *data.Login, + Commits: int(data.Commits), + PrsCreated: int(data.PrsCreated), + TotalContributions: int(data.Commits) + int(data.PrsCreated), + }) + } } return repoUserContributionsInsightGroup, nil