Skip to content

Commit

Permalink
Support mulit-line args
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Aug 6, 2021
1 parent 145c88f commit 5f56300
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 46 deletions.
27 changes: 13 additions & 14 deletions generator/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func NewActivities(src *source.Source) *Activities {
}

func (a *Activities) Generate(ctx context.Context, w io.Writer, args profile_stats.Args) (err error) {
usernames, ok := args.String("username")
if !ok || usernames == "" {
usernames, ok := args.StringSlice("username")
if !ok {
return fmt.Errorf("no usernames")
}

Expand All @@ -49,15 +49,14 @@ func (a *Activities) Generate(ctx context.Context, w io.Writer, args profile_sta
}
}

repository, _ := args.String("repository")
branch, _ := args.String("branch")
labels, _ := args.String("labels")
labelsFilter, _ := args.String("labels_filter")
repository, _ := args.StringSlice("repository")
branch, _ := args.StringSlice("branch")
labels, _ := args.StringSlice("labels")
labelsFilter, _ := args.StringSlice("labels_filter")

var states []source.PullRequestState
statesRaw, ok := args.String("states")
statesSlice, ok := args.StringSlice("states")
if ok {
statesSlice := strings.Split(statesRaw, ",")
states = make([]source.PullRequestState, 0, len(statesSlice))
for _, state := range statesSlice {
s := source.PullRequestState(strings.ToUpper(state))
Expand All @@ -73,10 +72,10 @@ func (a *Activities) Generate(ctx context.Context, w io.Writer, args profile_sta
states = []source.PullRequestState{source.PullRequestStateOpen, source.PullRequestStateClosed, source.PullRequestStateMerged}
}

return a.Get(ctx, w, strings.Split(usernames, ","), size, states, repository, branch, labels, labelsFilter, last)
return a.Get(ctx, w, usernames, size, states, repository, branch, labels, labelsFilter, last)
}

func (a *Activities) Get(ctx context.Context, w io.Writer, usernames []string, size int, states []source.PullRequestState, repository, branch, labels, labelsFilter string, last time.Time) error {
func (a *Activities) Get(ctx context.Context, w io.Writer, usernames []string, size int, states []source.PullRequestState, repository, branch, labels, labelsFilter []string, last time.Time) error {
items := []*source.PullRequest{}

cbs := []source.PullRequestCallback{}
Expand Down Expand Up @@ -115,10 +114,10 @@ func (a *Activities) Get(ctx context.Context, w io.Writer, usernames []string, s
if pr.SortTime.Before(last) {
continue
}
if branch != "" && !utils.Match(branch, pr.BaseRef) {
if len(branch) != 0 && !utils.Match(branch, pr.BaseRef) {
continue
}
if labels != "" {
if len(labels) != 0 {
match := false
for _, label := range pr.Labels {
if utils.Match(labels, label) {
Expand All @@ -130,7 +129,7 @@ func (a *Activities) Get(ctx context.Context, w io.Writer, usernames []string, s
continue
}
}
if repository != "" {
if len(repository) != 0 {
repo := strings.TrimPrefix(strings.Split(pr.URL.Path, "/pull/")[0], "/")
if !utils.Match(repository, repo) {
continue
Expand All @@ -146,7 +145,7 @@ func (a *Activities) Get(ctx context.Context, w io.Writer, usernames []string, s
if n := attrs[username]["name"]; n != "" {
pr.Username = n
}
if labelsFilter != "" {
if len(labelsFilter) != 0 {
list := make([]string, 0, len(pr.Labels))
for _, label := range pr.Labels {
if utils.Match(labelsFilter, label) {
Expand Down
17 changes: 17 additions & 0 deletions generator/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ func (a args) String(name string) (string, bool) {
return val, true
}

func (a args) StringSlice(name string) ([]string, bool) {
val, ok := a.String(name)
if !ok {
return []string{}, false
}
vals := []string{}
for _, v := range strings.Split(val, "\n") {
for _, v := range strings.Split(v, ",") {
s := strings.TrimSpace(v)
if s != "" {
vals = append(vals, s)
}
}
}
return vals, len(vals) != 0
}

func (a args) Int(name string) (int, bool) {
raw, ok := a.String(name)
if !ok {
Expand Down
21 changes: 10 additions & 11 deletions generator/charts/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const (
)

func (a *Charts) Generate(ctx context.Context, w io.Writer, args profile_stats.Args) (err error) {
usernames, ok := args.String("username")
if !ok || usernames == "" {
usernames, ok := args.StringSlice("username")
if !ok {
return fmt.Errorf("no usernames")
}

Expand Down Expand Up @@ -59,13 +59,12 @@ func (a *Charts) Generate(ctx context.Context, w io.Writer, args profile_stats.A
}
}

repository, _ := args.String("repository")
branch, _ := args.String("branch")
repository, _ := args.StringSlice("repository")
branch, _ := args.StringSlice("branch")

var states []source.PullRequestState
statesRaw, ok := args.String("states")
statesSlice, ok := args.StringSlice("states")
if ok {
statesSlice := strings.Split(statesRaw, ",")
states = make([]source.PullRequestState, 0, len(statesSlice))
for _, state := range statesSlice {
s := source.PullRequestState(strings.ToUpper(state))
Expand All @@ -83,7 +82,7 @@ func (a *Charts) Generate(ctx context.Context, w io.Writer, args profile_stats.A

title, ok := args.String("title")
if !ok {
title = kind + " " + strings.ReplaceAll(statesRaw, ",", "/") + " in the last " + span + " in the " + repository
title = kind + " " + strings.Join(statesSlice, "/") + " in the last " + span + " in the " + strings.Join(repository, ",")
}

width, _ := args.Int("width")
Expand All @@ -101,10 +100,10 @@ func (a *Charts) Generate(ctx context.Context, w io.Writer, args profile_stats.A
maxVal = 49
}

return a.Get(ctx, w, title, strings.Split(usernames, ","), size, states, repository, branch, last, kind, width, height, maxVal)
return a.Get(ctx, w, title, usernames, size, states, repository, branch, last, kind, width, height, maxVal)
}

func (a *Charts) Get(ctx context.Context, w io.Writer, title string, usernames []string, size int, states []source.PullRequestState, repository, branch string, last time.Time, kind string, width, height, maxVal int) error {
func (a *Charts) Get(ctx context.Context, w io.Writer, title string, usernames []string, size int, states []source.PullRequestState, repository, branch []string, last time.Time, kind string, width, height, maxVal int) error {
data := render.ChartData{
Title: title,
ValueMessage: kind,
Expand Down Expand Up @@ -153,11 +152,11 @@ func (a *Charts) Get(ctx context.Context, w io.Writer, title string, usernames [
if pr.SortTime.Before(last) {
continue
}
if !utils.Match(branch, pr.BaseRef) {
if len(branch) != 0 && !utils.Match(branch, pr.BaseRef) {
continue
}
repo := strings.TrimPrefix(strings.Split(pr.URL.Path, "/pull/")[0], "/")
if !utils.Match(repository, repo) {
if len(repository) != 0 && !utils.Match(repository, repo) {
continue
}
if !before.IsZero() && !pr.SortTime.Before(before) {
Expand Down
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type Args interface {
String(name string) (string, bool)
StringSlice(name string) ([]string, bool)
Int(name string) (int, bool)
}

Expand Down
11 changes: 5 additions & 6 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
)

const (
star = "*"
negate = "^"
separate = ","
star = "*"
negate = "^"
)

func Match(format, value string) bool {
func Match(pattern []string, value string) bool {
matches := []string{}
exceptions := []string{}
for _, f := range strings.Split(format, separate) {
f := strings.TrimSpace(f)
for _, v := range pattern {
f := strings.TrimSpace(v)
if f == "" {
continue
}
Expand Down
36 changes: 21 additions & 15 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,71 @@
package utils

import "testing"
import (
"testing"
)

func TestMatch(t *testing.T) {
type args struct {
format string
value string
pattern []string
value string
}
tests := []struct {
name string
args args
want bool
}{
{
args: args{"", "x"},
args: args{[]string{""}, "x"},
want: false,
},
{
args: args{"*", "x"},
args: args{[]string{"*"}, "x"},
want: true,
},
{
args: args{"^*", "x"},
args: args{[]string{"^*"}, "x"},
want: false,
},
{
args: args{"x", "x"},
args: args{[]string{"x"}, "x"},
want: true,
},
{
args: args{"x*", "xy"},
args: args{[]string{"x*"}, "xy"},
want: true,
},
{
args: args{"*x", "yx"},
args: args{[]string{"*x"}, "yx"},
want: true,
},
{
args: args{"*x*", "yxy"},
args: args{[]string{"*x*"}, "yxy"},
want: true,
},
{
args: args{"x", "y"},
args: args{[]string{"x"}, "y"},
want: false,
},
{
args: args{"x*,^xy", "xy"},
args: args{[]string{"x*", "^xy"}, "xy"},
want: false,
},
{
args: args{"xy,^x*", "xy"},
args: args{[]string{"xy", "^x*"}, "xy"},
want: true,
},
{
args: args{"^z", "xy"},
args: args{[]string{"xy", "xz"}, "xz"},
want: true,
},
{
args: args{[]string{"^z"}, "xy"},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Match(tt.args.format, tt.args.value); got != tt.want {
if got := Match(tt.args.pattern, tt.args.value); got != tt.want {
t.Errorf("Match() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 5f56300

Please sign in to comment.