Skip to content

Commit 1fa8554

Browse files
authored
Merge pull request #7 from github/golangci
feat(golangci): add linter and action to run golangci-lint
2 parents c31db8d + 9f994f7 commit 1fa8554

File tree

5 files changed

+106
-12
lines changed

5 files changed

+106
-12
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: golangci-lint
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
golangci-lint:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
persist-credentials: false
19+
20+
- name: setup go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version-file: 'go.mod'
24+
cache: true
25+
26+
- name: run golangci-lint
27+
uses: golangci/golangci-lint-action@v7

internal/cmd/match_criteria.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,32 @@ func branchMatchesCriteria(branch string) bool {
6060
}
6161

6262
func labelsMatch(prLabels, ignoreLabels, selectLabels []string) bool {
63+
// If no ignoreLabels or selectLabels are specified, all labels pass this check
64+
if len(ignoreLabels) == 0 && len(selectLabels) == 0 {
65+
return true
66+
}
67+
68+
// If the pull request contains any of the ignore labels, it doesn't match
6369
for _, l := range ignoreLabels {
64-
if i := slices.Index(prLabels, l); i != -1 {
70+
if slices.Contains(prLabels, l) {
6571
return false
6672
}
6773
}
6874

69-
for _, l := range selectLabels {
70-
found := false
71-
if i := slices.Index(prLabels, l); i != -1 {
72-
found = true
73-
break
74-
}
75+
// If selectLabels are specified but the pull request has no labels, it doesn't match
76+
if len(selectLabels) > 0 && len(prLabels) == 0 {
77+
return false
78+
}
7579

76-
if !found {
77-
return false
80+
// If the pull request contains any of the select labels, it matches
81+
for _, l := range selectLabels {
82+
if slices.Contains(prLabels, l) {
83+
return true
7884
}
7985
}
8086

81-
return true
87+
// If none of the select labels are found, it doesn't match
88+
return len(selectLabels) == 0
8289
}
8390

8491
// GraphQL response structure for PR status info

internal/cmd/match_criteria_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,60 @@ func TestLabelsMatch(t *testing.T) {
4444
selectLabels: []string{"a", "c"},
4545
want: true,
4646
},
47+
{
48+
prLabels: []string{},
49+
ignoreLabels: []string{"b"},
50+
selectLabels: []string{"a", "c"},
51+
want: false,
52+
},
53+
{
54+
prLabels: []string{},
55+
ignoreLabels: []string{"b"},
56+
selectLabels: []string{},
57+
want: true,
58+
},
59+
{
60+
prLabels: []string{},
61+
ignoreLabels: []string{},
62+
selectLabels: []string{"a"},
63+
want: false,
64+
},
65+
{
66+
prLabels: []string{},
67+
ignoreLabels: []string{},
68+
selectLabels: []string{},
69+
want: true,
70+
},
71+
{
72+
prLabels: []string{"a"},
73+
selectLabels: []string{"a"},
74+
ignoreLabels: []string{"b"},
75+
want: true,
76+
},
77+
{
78+
prLabels: []string{"a"},
79+
selectLabels: []string{"a"},
80+
ignoreLabels: []string{"a"},
81+
want: false,
82+
},
83+
{
84+
prLabels: []string{"a"},
85+
selectLabels: []string{},
86+
ignoreLabels: []string{},
87+
want: true,
88+
},
89+
{
90+
prLabels: []string{"a"},
91+
selectLabels: []string{"a"},
92+
ignoreLabels: []string{},
93+
want: true,
94+
},
95+
{
96+
prLabels: []string{"a", "b", "c"},
97+
selectLabels: []string{"a", "b"},
98+
ignoreLabels: []string{"c"},
99+
want: false,
100+
},
47101
}
48102

49103
for _, test := range tests {

internal/cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ func runCombine(cmd *cobra.Command, args []string) error {
147147

148148
Logger.Debug("starting gh-combine", "version", version.String())
149149

150-
// if the dependabot flag is set and branchPrefix is not already set, set the branch prefix to "dependabot/"
151150
if dependabot && branchPrefix == "" {
151+
branchPrefix = "dependabot/"
152152
}
153153

154154
// Input validation
@@ -181,11 +181,15 @@ func runCombine(cmd *cobra.Command, args []string) error {
181181
func executeCombineCommand(ctx context.Context, spinner *Spinner, repos []string) error {
182182
// Create GitHub API client
183183
restClient, err := api.DefaultRESTClient()
184-
graphQlClient, err := api.DefaultGraphQLClient()
185184
if err != nil {
186185
return fmt.Errorf("failed to create REST client: %w", err)
187186
}
188187

188+
graphQlClient, err := api.DefaultGraphQLClient()
189+
if err != nil {
190+
return fmt.Errorf("failed to create GraphQLClient client: %w", err)
191+
}
192+
189193
for _, repo := range repos {
190194
// Check if context was cancelled (CTRL+C pressed)
191195
select {

script/lint

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
set -e
44

55
go fmt ./...
6+
go mod tidy
7+
golangci-lint run --fix

0 commit comments

Comments
 (0)