Skip to content

Commit 9c29a67

Browse files
authored
Merge pull request #27 from github/repos-parsing
feat(repo): parse repos to actual repos
2 parents a4b0107 + bbfa9bc commit 9c29a67

File tree

3 files changed

+147
-230
lines changed

3 files changed

+147
-230
lines changed

internal/cmd/repos.go

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,83 @@ import (
44
"fmt"
55
"os"
66
"strings"
7+
8+
"github.com/github/gh-combine/internal/github"
79
)
810

9-
// ParseRepositories parses repository names from arguments or file with support for default owner
10-
func ParseRepositories(args []string, reposFile string, defaultOwner string) ([]string, error) {
11-
// Explicitly initialize repos as an empty slice
12-
repos := []string{}
11+
var (
12+
ErrEmptyRepositoriesFilePath = fmt.Errorf("empty repositories file path")
13+
)
1314

14-
// If both args and reposFile are empty, return an empty slice
15+
func ParseRepositories(args []string, path string) ([]github.Repo, error) {
1516
if len(args) == 0 && reposFile == "" {
16-
return repos, nil
17+
return nil, nil
1718
}
1819

19-
// Parse from command line arguments
20-
if len(args) > 0 {
21-
// Check if repos are comma-separated
22-
for _, arg := range args {
23-
if strings.Contains(arg, ",") {
24-
splitRepos := strings.Split(arg, ",")
25-
for _, repo := range splitRepos {
26-
if trimmedRepo := strings.TrimSpace(repo); trimmedRepo != "" {
27-
repos = append(repos, applyDefaultOwner(trimmedRepo, defaultOwner))
28-
}
29-
}
30-
} else {
31-
repos = append(repos, applyDefaultOwner(arg, defaultOwner))
32-
}
33-
}
20+
argsRepos, err := parseRepositoriesArgs(args)
21+
if err != nil {
22+
return nil, err
3423
}
3524

36-
// Parse from file if specified
37-
if reposFile != "" {
38-
fileContent, err := os.ReadFile(reposFile)
39-
if err != nil {
40-
return nil, fmt.Errorf("failed to read repositories file: %w", err)
41-
}
25+
fileRepos, err := parseRepositoriesFile(path)
26+
if err != nil {
27+
return nil, err
28+
}
4229

43-
lines := strings.Split(string(fileContent), "\n")
44-
for _, line := range lines {
45-
// Trim whitespace and ignore comments
46-
trimmedLine := strings.TrimSpace(line)
47-
if trimmedLine == "" || strings.HasPrefix(trimmedLine, "#") {
48-
continue
49-
}
30+
return append(argsRepos, fileRepos...), nil
31+
}
5032

51-
// Remove inline comments
52-
if idx := strings.Index(trimmedLine, "#"); idx != -1 {
53-
trimmedLine = strings.TrimSpace(trimmedLine[:idx])
54-
}
33+
func parseRepositoriesArgs(args []string) ([]github.Repo, error) {
34+
repos := []github.Repo{}
35+
36+
for _, arg := range args {
37+
for _, rawRepo := range strings.Split(arg, ",") {
5538

56-
if trimmedLine != "" {
57-
repos = append(repos, applyDefaultOwner(trimmedLine, defaultOwner))
39+
repo, err := github.ParseRepo(rawRepo)
40+
if err != nil {
41+
return nil, err
5842
}
43+
44+
repos = append(repos, repo)
5945
}
6046
}
6147

6248
return repos, nil
6349
}
6450

65-
// applyDefaultOwner adds the default owner to a repo name if it doesn't already have an owner
66-
func applyDefaultOwner(repo string, defaultOwner string) string {
67-
if defaultOwner == "" || strings.Contains(repo, "/") {
68-
return repo
51+
// TODO: this should be removed to accept `gh-combine < repos` instead.
52+
func parseRepositoriesFile(path string) ([]github.Repo, error) {
53+
if path == "" {
54+
return nil, nil
6955
}
70-
return defaultOwner + "/" + repo
56+
57+
repos := []github.Repo{}
58+
59+
fileContent, err := os.ReadFile(path)
60+
if err != nil {
61+
return nil, fmt.Errorf("failed to read repositories file %s: %w", path, err)
62+
}
63+
64+
lines := strings.Split(string(fileContent), "\n")
65+
for _, line := range lines {
66+
line = strings.TrimSpace(line)
67+
68+
if line == "" || strings.HasPrefix(line, "#") {
69+
continue
70+
}
71+
72+
// Remove inline comments
73+
if idx := strings.Index(line, "#"); idx != -1 {
74+
line = strings.TrimSpace(line[:idx])
75+
}
76+
77+
repo, err := github.ParseRepo(line)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
repos = append(repos, repo)
83+
}
84+
85+
return repos, nil
7186
}

0 commit comments

Comments
 (0)