dmitri.shuralyov.com/service/change/...

Adjust state filter for changes.
dmitshur committed 2 years ago commit 6ec226ef3dbf9214ddd88bd064e93e00998effaa
changes.go
@@ -61,19 +61,23 @@ const (
 	MergedState State = "merged"
 )
 
 // ListOptions are options for list operations.
 type ListOptions struct {
-	State StateFilter
+	Filter StateFilter
 }
 
 // StateFilter is a filter by state.
-type StateFilter State
+type StateFilter string
 
 const (
-	// AllStates is a state filter that includes all issues.
-	AllStates StateFilter = "all"
+	// FilterOpen is a state filter that includes open changes.
+	FilterOpen StateFilter = "open"
+	// FilterClosedMerged is a state filter that includes closed and merged changes.
+	FilterClosedMerged StateFilter = "closed|merged"
+	// FilterAll is a state filter that includes all changes.
+	FilterAll StateFilter = "all"
 )
 
 type GetDiffOptions struct {
 	// Commit is the commit ID of the commit to fetch.
 	Commit string
gerritapi/gerritapi.go
@@ -40,18 +40,17 @@ type service struct {
 }
 
 func (s service) List(ctx context.Context, rs string, opt changes.ListOptions) ([]changes.Change, error) {
 	project := project(rs)
 	var query string
-	switch opt.State {
-	case changes.StateFilter(changes.OpenState):
+	switch opt.Filter {
+	case changes.FilterOpen:
 		query = fmt.Sprintf("project:%s status:open", project)
-	case changes.StateFilter(changes.ClosedState):
+	case changes.FilterClosedMerged:
+		// "status:closed" is equivalent to "(status:abandoned OR status:merged)".
 		query = fmt.Sprintf("project:%s status:closed", project)
-	case changes.StateFilter(changes.MergedState):
-		query = fmt.Sprintf("project:%s status:merged", project)
-	case changes.AllStates:
+	case changes.FilterAll:
 		query = fmt.Sprintf("project:%s", project)
 	}
 	cs, _, err := s.cl.Changes.QueryChanges(&gerrit.QueryChangeOptions{
 		QueryOptions: gerrit.QueryOptions{
 			Query: []string{query},
githubapi/githubapi.go
@@ -55,22 +55,20 @@ func (s service) List(ctx context.Context, rs string, opt changes.ListOptions) (
 	if err != nil {
 		// TODO: Map to 400 Bad Request HTTP error.
 		return nil, err
 	}
 	var states []githubql.PullRequestState
-	switch opt.State {
-	case changes.StateFilter(changes.OpenState):
+	switch opt.Filter {
+	case changes.FilterOpen:
 		states = []githubql.PullRequestState{githubql.PullRequestStateOpen}
-	case changes.StateFilter(changes.ClosedState):
-		states = []githubql.PullRequestState{githubql.PullRequestStateClosed}
-	case changes.StateFilter(changes.MergedState):
-		states = []githubql.PullRequestState{githubql.PullRequestStateMerged}
-	case changes.AllStates:
+	case changes.FilterClosedMerged:
+		states = []githubql.PullRequestState{githubql.PullRequestStateClosed, githubql.PullRequestStateMerged}
+	case changes.FilterAll:
 		states = nil // No states to filter the PRs by.
 	default:
 		// TODO: Map to 400 Bad Request HTTP error.
-		return nil, fmt.Errorf("opt.State has unsupported value %q", opt.State)
+		return nil, fmt.Errorf("opt.State has unsupported value %q", opt.Filter)
 	}
 	var q struct {
 		Repository struct {
 			PullRequests struct {
 				Nodes []struct {
@@ -128,22 +126,20 @@ func (s service) Count(ctx context.Context, rs string, opt changes.ListOptions)
 	if err != nil {
 		// TODO: Map to 400 Bad Request HTTP error.
 		return 0, err
 	}
 	var states []githubql.PullRequestState
-	switch opt.State {
-	case changes.StateFilter(changes.OpenState):
+	switch opt.Filter {
+	case changes.FilterOpen:
 		states = []githubql.PullRequestState{githubql.PullRequestStateOpen}
-	case changes.StateFilter(changes.ClosedState):
-		states = []githubql.PullRequestState{githubql.PullRequestStateClosed}
-	case changes.StateFilter(changes.MergedState):
-		states = []githubql.PullRequestState{githubql.PullRequestStateMerged}
-	case changes.AllStates:
+	case changes.FilterClosedMerged:
+		states = []githubql.PullRequestState{githubql.PullRequestStateClosed, githubql.PullRequestStateMerged}
+	case changes.FilterAll:
 		states = nil // No states to filter the PRs by.
 	default:
 		// TODO: Map to 400 Bad Request HTTP error.
-		return 0, fmt.Errorf("opt.State has unsupported value %q", opt.State)
+		return 0, fmt.Errorf("opt.State has unsupported value %q", opt.Filter)
 	}
 	var q struct {
 		Repository struct {
 			PullRequests struct {
 				TotalCount uint64
maintner/maintner.go
@@ -38,15 +38,13 @@ func (s service) List(ctx context.Context, repo string, opt changes.ListOptions)
 			log.Printf("empty status for CL %d\n", cl.Number)
 			return nil
 		}
 		state := state(cl.Status)
 		switch {
-		case opt.State == changes.StateFilter(changes.OpenState) && state != changes.OpenState:
+		case opt.Filter == changes.FilterOpen && state != changes.OpenState:
 			return nil
-		case opt.State == changes.StateFilter(changes.ClosedState) && state != changes.ClosedState:
-			return nil
-		case opt.State == changes.StateFilter(changes.MergedState) && state != changes.MergedState:
+		case opt.Filter == changes.FilterClosedMerged && !(state == changes.ClosedState || state == changes.MergedState):
 			return nil
 		}
 
 		is = append(is, changes.Change{
 			ID:    uint64(cl.Number),
@@ -80,15 +78,13 @@ func (s service) Count(_ context.Context, repo string, opt changes.ListOptions)
 		if cl.Status == "" {
 			return nil
 		}
 		state := state(cl.Status)
 		switch {
-		case opt.State == changes.StateFilter(changes.OpenState) && state != changes.OpenState:
-			return nil
-		case opt.State == changes.StateFilter(changes.ClosedState) && state != changes.ClosedState:
+		case opt.Filter == changes.FilterOpen && state != changes.OpenState:
 			return nil
-		case opt.State == changes.StateFilter(changes.MergedState) && state != changes.MergedState:
+		case opt.Filter == changes.FilterClosedMerged && !(state == changes.ClosedState || state == changes.MergedState):
 			return nil
 		}
 
 		count++