7
7
"fmt"
8
8
"log/slog"
9
9
"net/http"
10
+ "regexp"
10
11
"sort"
11
12
"strconv"
12
13
"strings"
@@ -21,10 +22,12 @@ import (
21
22
githook "github.com/go-playground/webhooks/v6/github"
22
23
"github.com/google/go-github/v59/github"
23
24
"github.com/sashabaranov/go-openai"
25
+ "golang.org/x/exp/maps"
26
+ "gopkg.in/yaml.v3"
24
27
)
25
28
26
29
type repoAddr struct {
27
- install , user , repo string
30
+ InstallID , User , Repo string
28
31
}
29
32
30
33
type Webhook struct {
@@ -68,15 +71,59 @@ func filterIssues(slice []*github.Issue, f func(*github.Issue) bool) []*github.I
68
71
}
69
72
70
73
type InferRequest struct {
71
- InstallID string `json:"install_id"`
72
- User string `json:"user"`
73
- Repo string `json:"repo"`
74
- Issue int `json:"issue"`
74
+ InstallID , User , Repo string
75
+ Issue int `json:"issue"`
75
76
}
76
77
77
78
type InferResponse struct {
78
- SetLabels []string `json:"set_labels,omitempty"`
79
- TokensUsed int `json:"tokens_used,omitempty"`
79
+ SetLabels []string `json:"set_labels,omitempty"`
80
+ TokensUsed int `json:"tokens_used,omitempty"`
81
+ DisabledLabels []string `json:"disabled_labels,omitempty"`
82
+ }
83
+
84
+ type repoConfig struct {
85
+ Exclude []regexp.Regexp `json:"exclude"`
86
+ }
87
+
88
+ func (c * repoConfig ) checkLabel (label string ) bool {
89
+ for _ , re := range c .Exclude {
90
+ if re .MatchString (label ) {
91
+ return false
92
+ }
93
+ }
94
+ return true
95
+ }
96
+
97
+ func (s * Webhook ) getRepoConfig (ctx context.Context , client * github.Client ,
98
+ owner , repo string ,
99
+ ) (* repoConfig , error ) {
100
+ fileContent , _ , _ , err := client .Repositories .GetContents (
101
+ ctx ,
102
+ owner ,
103
+ repo ,
104
+ ".github/labeler.yml" ,
105
+ & github.RepositoryContentGetOptions {},
106
+ )
107
+ if err != nil {
108
+ var githubErr * github.ErrorResponse
109
+ if errors .As (err , & githubErr ) && githubErr .Response .StatusCode == http .StatusNotFound {
110
+ return & repoConfig {}, nil
111
+ }
112
+ return nil , fmt .Errorf ("get contents: %w" , err )
113
+ }
114
+
115
+ content , err := fileContent .GetContent ()
116
+ if err != nil {
117
+ return nil , fmt .Errorf ("unmarshal content: %w" , err )
118
+ }
119
+
120
+ var config repoConfig
121
+ err = yaml .Unmarshal (
122
+ []byte (content ),
123
+ & config ,
124
+ )
125
+
126
+ return & config , err
80
127
}
81
128
82
129
func (s * Webhook ) Infer (ctx context.Context , req * InferRequest ) (* InferResponse , error ) {
@@ -87,10 +134,15 @@ func (s *Webhook) Infer(ctx context.Context, req *InferRequest) (*InferResponse,
87
134
88
135
githubClient := github .NewClient (instConfig .Client (ctx ))
89
136
137
+ config , err := s .getRepoConfig (ctx , githubClient , req .User , req .Repo )
138
+ if err != nil {
139
+ return nil , fmt .Errorf ("get repo config: %w" , err )
140
+ }
141
+
90
142
lastIssues , err := s .recentIssuesCache .Do (repoAddr {
91
- install : req .InstallID ,
92
- user : req .User ,
93
- repo : req .Repo ,
143
+ InstallID : req .InstallID ,
144
+ User : req .User ,
145
+ Repo : req .Repo ,
94
146
}, func () ([]* github.Issue , error ) {
95
147
return ghapi .Page (
96
148
ctx ,
@@ -116,9 +168,9 @@ func (s *Webhook) Infer(ctx context.Context, req *InferRequest) (*InferResponse,
116
168
}
117
169
118
170
labels , err := s .repoLabelsCache .Do (repoAddr {
119
- install : req .InstallID ,
120
- user : req .User ,
121
- repo : req .Repo ,
171
+ InstallID : req .InstallID ,
172
+ User : req .User ,
173
+ Repo : req .Repo ,
122
174
}, func () ([]* github.Label , error ) {
123
175
return ghapi .Page (
124
176
ctx ,
@@ -209,6 +261,9 @@ retryAI:
209
261
if strings .Contains (label .GetDescription (), magicDisableString ) {
210
262
disabledLabels [label .GetName ()] = struct {}{}
211
263
}
264
+ if ! config .checkLabel (label .GetName ()) {
265
+ disabledLabels [label .GetName ()] = struct {}{}
266
+ }
212
267
}
213
268
214
269
// Remove any labels that are disabled.
@@ -221,8 +276,9 @@ retryAI:
221
276
}
222
277
223
278
return & InferResponse {
224
- SetLabels : newLabels ,
225
- TokensUsed : resp .Usage .TotalTokens ,
279
+ SetLabels : newLabels ,
280
+ TokensUsed : resp .Usage .TotalTokens ,
281
+ DisabledLabels : maps .Keys (disabledLabels ),
226
282
}, nil
227
283
}
228
284
0 commit comments