Skip to content

Commit c37a6b2

Browse files
committed
🐛 fix gh issue link
♻️ improve settings processing
1 parent d88cce7 commit c37a6b2

4 files changed

Lines changed: 24 additions & 27 deletions

File tree

internal/settings.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type SettingsService struct {
1717
// NewSettingsService creates a new SettingsService instance.
1818
func NewSettingsService(settings *Settings) *SettingsService {
1919
settings.checkSettings()
20+
settings.setMaxAge()
21+
settings.setLabelSlice()
2022

2123
return &SettingsService{
2224
settings: settings,
@@ -31,14 +33,16 @@ type Settings struct {
3133
DBFile string
3234
// labels to look for
3335
// ex: "\"help-wanted\", \"help wanted\",\"junior friendly\",\"good first issue\""
34-
Labels string
36+
ConfiguredLabels string
3537
// optional
3638
MatrixRoomID string
3739
MatrixUsername string
3840
MatrixPassword string
3941
MatrixServer string
4042

41-
maxAge string
43+
// computed
44+
MaxAge string
45+
Labels []string
4246
}
4347

4448
// GetSettings : Get all the settings.
@@ -52,7 +56,7 @@ func (ss *SettingsService) Print() {
5256
log.Info(" Github token: **")
5357
log.Infof(" Interval: %d hours", ss.GetSettings().Interval)
5458
log.Infof(" Database file: %s", ss.GetSettings().DBFile)
55-
log.Infof(" Labels: %s", ss.GetSettings().Labels)
59+
log.Infof(" Labels: %s", ss.GetSettings().ConfiguredLabels)
5660
log.Infof(" Matrix room ID: %s", ss.GetSettings().MatrixRoomID)
5761
log.Infof(" Matrix username: %s", ss.GetSettings().MatrixUsername)
5862
log.Info(" Matrix password: ***")
@@ -68,27 +72,24 @@ func (s *Settings) IsMatrixConfigured() bool {
6872
}
6973

7074
// GetLabelSlice gets label slice from settings.
71-
func (s *Settings) GetLabelSlice() []string {
75+
func (s *Settings) setLabelSlice() {
7276
// Regex to extract labels inside double quotes
7377
re := regexp.MustCompile(`"([^"]+)"`)
74-
matches := re.FindAllStringSubmatch(s.Labels, -1)
78+
matches := re.FindAllStringSubmatch(s.ConfiguredLabels, -1)
7579

76-
var labels = make([]string, len(matches))
77-
for _, match := range matches {
78-
labels = append(labels, match[1])
80+
if matches == nil {
81+
log.Fatal("Invalid labels, format should be \"label1\", \"label2\", ...")
7982
}
8083

81-
return labels
84+
s.Labels = make([]string, len(matches))
85+
for i, match := range matches {
86+
s.Labels[i] = match[1]
87+
}
8288
}
8389

8490
// GetMaxAge gets the max time before which data is not fresh anymore.
85-
func (ss *SettingsService) GetMaxAge() string {
86-
s := ss.GetSettings()
87-
if s.maxAge == "" {
88-
s.maxAge = strconv.Itoa(hoursInSeconds * s.Interval)
89-
}
90-
91-
return s.maxAge
91+
func (s *Settings) setMaxAge() {
92+
s.MaxAge = strconv.Itoa(hoursInSeconds * s.Interval)
9293
}
9394

9495
func (s *Settings) checkSettings() {
@@ -108,11 +109,7 @@ func (s *Settings) checkSettings() {
108109
log.Warn("Matrix notif not fully configured")
109110
}
110111

111-
if s.Labels == "" {
112+
if s.ConfiguredLabels == "" {
112113
log.Fatal("Missing labels")
113114
}
114-
115-
if s.GetLabelSlice() == nil {
116-
log.Fatal("Invalid labels, format should be \"label1\", \"label2\", ...")
117-
}
118115
}

internal/stars.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (ghs *GhStarsService) buildQueryFromTemplate(repoCursor string) (string, er
151151
MaxIssues int
152152
}{
153153
RepoCursor: repoCursor,
154-
Labels: ghs.settingsService.GetSettings().Labels,
154+
Labels: ghs.settingsService.GetSettings().ConfiguredLabels,
155155
MaxIssues: MAX_ISSUES_PER_REPO,
156156
}
157157

internal/webpageHandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (wph *WebpageHandler) HandleWebPage(w http.ResponseWriter, r *http.Request)
6767
}
6868

6969
w.Header().Set("ETag", etag)
70-
w.Header().Set("Cache-Control", "public, max-age="+wph.settingsService.GetMaxAge())
70+
w.Header().Set("Cache-Control", "public, max-age="+wph.settingsService.settings.MaxAge)
7171

7272
err = tmpl.Execute(w, data)
7373
if err != nil {
@@ -97,14 +97,14 @@ func (wph *WebpageHandler) buildHelpIssuesLink(repoOwner string) string {
9797
// to have something like
9898
// https://github.com/OWNER/REPO/issues?q=is"issue state=open (label="good first issue" OR label="help wanted").
9999
func (wph *WebpageHandler) labelsToGhUrlParam() string {
100-
labelSettings := wph.settingsService.GetSettings().GetLabelSlice()
100+
labelSettings := wph.settingsService.GetSettings().Labels
101101

102102
var labels = make([]string, len(labelSettings))
103103

104-
for _, label := range labelSettings {
104+
for i, label := range labelSettings {
105105
// URL encode the label (replace spaces with %20)
106106
encodedLabel := strings.ReplaceAll(url.QueryEscape(label), "+", "%20")
107-
labels = append(labels, fmt.Sprintf("label:%%22%s%%22", encodedLabel))
107+
labels[i] = fmt.Sprintf("label:%%22%s%%22", encodedLabel)
108108
}
109109

110110
// Join labels with " OR " and wrap in parentheses

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
&internal.Settings{
4747
GhToken: *ghTokenFlag,
4848
Interval: *interval,
49-
Labels: *labels,
49+
ConfiguredLabels: *labels,
5050
DBFile: *dbFile,
5151
MatrixServer: *matrixServer,
5252
MatrixUsername: *matrixUsername,

0 commit comments

Comments
 (0)