Skip to content

Commit 30392cc

Browse files
committed
split out more logic to their own files
1 parent a6f66ce commit 30392cc

File tree

3 files changed

+163
-152
lines changed

3 files changed

+163
-152
lines changed

internal/cmd/match_criteria.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package cmd
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
// checks if a PR matches all filtering criteria
9+
func PrMatchesCriteria(branch string, prLabels []struct{ Name string }) bool {
10+
// Check branch criteria if any are specified
11+
if !branchMatchesCriteria(branch) {
12+
return false
13+
}
14+
15+
// Check label criteria if any are specified
16+
if !labelsMatchCriteria(prLabels) {
17+
return false
18+
}
19+
20+
return true
21+
}
22+
23+
// checks if a branch matches the branch filtering criteria
24+
func branchMatchesCriteria(branch string) bool {
25+
// If no branch filters are specified, all branches pass this check
26+
if branchPrefix == "" && branchSuffix == "" && branchRegex == "" {
27+
return true
28+
}
29+
30+
// Apply branch prefix filter if specified
31+
if branchPrefix != "" && !strings.HasPrefix(branch, branchPrefix) {
32+
return false
33+
}
34+
35+
// Apply branch suffix filter if specified
36+
if branchSuffix != "" && !strings.HasSuffix(branch, branchSuffix) {
37+
return false
38+
}
39+
40+
// Apply branch regex filter if specified
41+
if branchRegex != "" {
42+
regex, err := regexp.Compile(branchRegex)
43+
if err != nil {
44+
Logger.Warn("Invalid regex pattern", "pattern", branchRegex, "error", err)
45+
return false
46+
}
47+
48+
if !regex.MatchString(branch) {
49+
return false
50+
}
51+
}
52+
53+
return true
54+
}
55+
56+
// labelsMatchCriteria checks if PR labels match the label filtering criteria
57+
func labelsMatchCriteria(prLabels []struct{ Name string }) bool {
58+
// If no label filters are specified, all PRs pass this check
59+
if ignoreLabel == "" && len(ignoreLabels) == 0 &&
60+
selectLabel == "" && len(selectLabels) == 0 {
61+
return true
62+
}
63+
64+
// Check for ignore label (singular)
65+
if ignoreLabel != "" {
66+
for _, label := range prLabels {
67+
if label.Name == ignoreLabel {
68+
return false
69+
}
70+
}
71+
}
72+
73+
// Check for ignore labels (plural)
74+
if len(ignoreLabels) > 0 {
75+
for _, ignoreL := range ignoreLabels {
76+
for _, prLabel := range prLabels {
77+
if prLabel.Name == ignoreL {
78+
return false
79+
}
80+
}
81+
}
82+
}
83+
84+
// Check for select label (singular)
85+
if selectLabel != "" {
86+
found := false
87+
for _, label := range prLabels {
88+
if label.Name == selectLabel {
89+
found = true
90+
break
91+
}
92+
}
93+
if !found {
94+
return false
95+
}
96+
}
97+
98+
// Check for select labels (plural)
99+
if len(selectLabels) > 0 {
100+
for _, requiredLabel := range selectLabels {
101+
found := false
102+
for _, prLabel := range prLabels {
103+
if prLabel.Name == requiredLabel {
104+
found = true
105+
break
106+
}
107+
}
108+
if !found {
109+
return false
110+
}
111+
}
112+
}
113+
114+
return true
115+
}

internal/cmd/repos.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
// ParseRepositories parses repository names from arguments or file
10+
func ParseRepositories(args []string, reposFile string) ([]string, error) {
11+
var repos []string
12+
13+
// Parse from command line arguments
14+
if len(args) > 0 {
15+
// Check if repos are comma-separated
16+
for _, arg := range args {
17+
if strings.Contains(arg, ",") {
18+
splitRepos := strings.Split(arg, ",")
19+
for _, repo := range splitRepos {
20+
if trimmedRepo := strings.TrimSpace(repo); trimmedRepo != "" {
21+
repos = append(repos, trimmedRepo)
22+
}
23+
}
24+
} else {
25+
repos = append(repos, arg)
26+
}
27+
}
28+
}
29+
30+
// Parse from file if specified
31+
if reposFile != "" {
32+
fileContent, err := os.ReadFile(reposFile)
33+
if err != nil {
34+
return nil, fmt.Errorf("failed to read repositories file: %w", err)
35+
}
36+
37+
lines := strings.Split(string(fileContent), "\n")
38+
for _, line := range lines {
39+
if trimmedLine := strings.TrimSpace(line); trimmedLine != "" && !strings.HasPrefix(trimmedLine, "#") {
40+
repos = append(repos, trimmedLine)
41+
}
42+
}
43+
}
44+
45+
return repos, nil
46+
}

internal/cmd/root.go

Lines changed: 2 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
8-
"regexp"
97
"strings"
108

119
"github.com/cli/go-gh/v2/pkg/api"
@@ -132,7 +130,7 @@ func runCombine(cmd *cobra.Command, args []string) error {
132130
defer spinner.Stop()
133131

134132
// Parse repositories from args or file
135-
repos, err := parseRepositories(args)
133+
repos, err := ParseRepositories(args, reposFile)
136134
if err != nil {
137135
return fmt.Errorf("failed to parse repositories: %w", err)
138136
}
@@ -149,45 +147,6 @@ func runCombine(cmd *cobra.Command, args []string) error {
149147
return nil
150148
}
151149

152-
// parseRepositories parses repository names from arguments or file
153-
func parseRepositories(args []string) ([]string, error) {
154-
var repos []string
155-
156-
// Parse from command line arguments
157-
if len(args) > 0 {
158-
// Check if repos are comma-separated
159-
for _, arg := range args {
160-
if strings.Contains(arg, ",") {
161-
splitRepos := strings.Split(arg, ",")
162-
for _, repo := range splitRepos {
163-
if trimmedRepo := strings.TrimSpace(repo); trimmedRepo != "" {
164-
repos = append(repos, trimmedRepo)
165-
}
166-
}
167-
} else {
168-
repos = append(repos, arg)
169-
}
170-
}
171-
}
172-
173-
// Parse from file if specified
174-
if reposFile != "" {
175-
fileContent, err := os.ReadFile(reposFile)
176-
if err != nil {
177-
return nil, fmt.Errorf("failed to read repositories file: %w", err)
178-
}
179-
180-
lines := strings.Split(string(fileContent), "\n")
181-
for _, line := range lines {
182-
if trimmedLine := strings.TrimSpace(line); trimmedLine != "" && !strings.HasPrefix(trimmedLine, "#") {
183-
repos = append(repos, trimmedLine)
184-
}
185-
}
186-
}
187-
188-
return repos, nil
189-
}
190-
191150
// executeCombineCommand performs the actual API calls and processing
192151
func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string) error {
193152
// Create GitHub API client
@@ -245,7 +204,7 @@ func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string
245204
branch := pull.Head.Ref
246205

247206
// Check if PR matches all filtering criteria
248-
if !prMatchesCriteria(branch, pull.Labels) {
207+
if !PrMatchesCriteria(branch, pull.Labels) {
249208
continue
250209
}
251210

@@ -283,112 +242,3 @@ func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string
283242

284243
return nil
285244
}
286-
287-
// prMatchesCriteria checks if a PR matches all filtering criteria
288-
func prMatchesCriteria(branch string, prLabels []struct{ Name string }) bool {
289-
// Check branch criteria if any are specified
290-
if !branchMatchesCriteria(branch) {
291-
return false
292-
}
293-
294-
// Check label criteria if any are specified
295-
if !labelsMatchCriteria(prLabels) {
296-
return false
297-
}
298-
299-
return true
300-
}
301-
302-
// branchMatchesCriteria checks if a branch matches the branch filtering criteria
303-
func branchMatchesCriteria(branch string) bool {
304-
// If no branch filters are specified, all branches pass this check
305-
if branchPrefix == "" && branchSuffix == "" && branchRegex == "" {
306-
return true
307-
}
308-
309-
// Apply branch prefix filter if specified
310-
if branchPrefix != "" && !strings.HasPrefix(branch, branchPrefix) {
311-
return false
312-
}
313-
314-
// Apply branch suffix filter if specified
315-
if branchSuffix != "" && !strings.HasSuffix(branch, branchSuffix) {
316-
return false
317-
}
318-
319-
// Apply branch regex filter if specified
320-
if branchRegex != "" {
321-
regex, err := regexp.Compile(branchRegex)
322-
if err != nil {
323-
Logger.Warn("Invalid regex pattern", "pattern", branchRegex, "error", err)
324-
return false
325-
}
326-
327-
if !regex.MatchString(branch) {
328-
return false
329-
}
330-
}
331-
332-
return true
333-
}
334-
335-
// labelsMatchCriteria checks if PR labels match the label filtering criteria
336-
func labelsMatchCriteria(prLabels []struct{ Name string }) bool {
337-
// If no label filters are specified, all PRs pass this check
338-
if ignoreLabel == "" && len(ignoreLabels) == 0 &&
339-
selectLabel == "" && len(selectLabels) == 0 {
340-
return true
341-
}
342-
343-
// Check for ignore label (singular)
344-
if ignoreLabel != "" {
345-
for _, label := range prLabels {
346-
if label.Name == ignoreLabel {
347-
return false
348-
}
349-
}
350-
}
351-
352-
// Check for ignore labels (plural)
353-
if len(ignoreLabels) > 0 {
354-
for _, ignoreL := range ignoreLabels {
355-
for _, prLabel := range prLabels {
356-
if prLabel.Name == ignoreL {
357-
return false
358-
}
359-
}
360-
}
361-
}
362-
363-
// Check for select label (singular)
364-
if selectLabel != "" {
365-
found := false
366-
for _, label := range prLabels {
367-
if label.Name == selectLabel {
368-
found = true
369-
break
370-
}
371-
}
372-
if !found {
373-
return false
374-
}
375-
}
376-
377-
// Check for select labels (plural)
378-
if len(selectLabels) > 0 {
379-
for _, requiredLabel := range selectLabels {
380-
found := false
381-
for _, prLabel := range prLabels {
382-
if prLabel.Name == requiredLabel {
383-
found = true
384-
break
385-
}
386-
}
387-
if !found {
388-
return false
389-
}
390-
}
391-
}
392-
393-
return true
394-
}

0 commit comments

Comments
 (0)