Skip to content

Commit

Permalink
adding label flag
Browse files Browse the repository at this point in the history
  • Loading branch information
enrichman committed Jun 7, 2022
1 parent 2553315 commit f89832e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
24 changes: 18 additions & 6 deletions pkg/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewRootCommand() *cobra.Command {
}

func NewListCommand() *cobra.Command {
return &cobra.Command{
listCmd := &cobra.Command{
Use: "list",
Short: "list partecipants",
Args: UserRepoArg(),
Expand All @@ -41,12 +41,14 @@ func NewListCommand() *cobra.Command {
user, repo := userRepo[0], userRepo[1]

client := github.NewClient(nil)
allIssues, err := raffle.GetAllIssues(client, user, repo)

labels, _ := cmd.Flags().GetStringSlice("label")
issues, err := raffle.GetIssues(client, user, repo, labels)
if err != nil {
return err
}

users := raffle.GetUsersFromIssues(allIssues)
users := raffle.GetUsersFromIssues(issues)
sort.Strings(users)

fmt.Printf("There are %d partecipants:\n", len(users))
Expand All @@ -58,10 +60,14 @@ func NewListCommand() *cobra.Command {
return nil
},
}

listCmd.Flags().StringSlice("label", []string{}, "labels")

return listCmd
}

func NewRunCommand() *cobra.Command {
return &cobra.Command{
runCmd := &cobra.Command{
Use: "run",
Short: "run raffle",
Args: UserRepoArg(),
Expand All @@ -72,12 +78,14 @@ func NewRunCommand() *cobra.Command {
user, repo := userRepo[0], userRepo[1]

client := github.NewClient(nil)
allIssues, err := raffle.GetAllIssues(client, user, repo)

labels, _ := cmd.Flags().GetStringSlice("label")
issues, err := raffle.GetIssues(client, user, repo, labels)
if err != nil {
return err
}

users := raffle.GetUsersFromIssues(allIssues)
users := raffle.GetUsersFromIssues(issues)

rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(users), func(i, j int) {
Expand All @@ -87,6 +95,10 @@ func NewRunCommand() *cobra.Command {
return nil
},
}

runCmd.Flags().StringSlice("label", []string{}, "labels")

return runCmd
}

func UserRepoArg() cobra.PositionalArgs {
Expand Down
3 changes: 2 additions & 1 deletion pkg/raffle/raffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"github.com/google/go-github/v45/github"
)

func GetAllIssues(client *github.Client, user, repo string) ([]*github.Issue, error) {
func GetIssues(client *github.Client, user, repo string, labels []string) ([]*github.Issue, error) {
opt := &github.IssueListByRepoOptions{
ListOptions: github.ListOptions{PerPage: 50},
Labels: labels,
}

var allIssues []*github.Issue
Expand Down

0 comments on commit f89832e

Please sign in to comment.