Skip to content

Commit 2e30eb2

Browse files
committed
✨ parametrize labels
1 parent d1ab141 commit 2e30eb2

5 files changed

Lines changed: 68 additions & 16 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
contents: read
1212
packages: write
1313
steps:
14-
- uses: actions/checkout@v4
14+
- uses: actions/checkout@v6
1515

1616
- name: lint
1717
uses: golangci/golangci-lint-action@v8

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,27 @@
1010

1111
Given a github token and matrix credentials
1212

13-
- Once a day, check `help-wanted` issues of the repo stared by the user
13+
- Once a day, check configured labels `help-wanted` issues of the repo stared by the user
1414
- Update info in an sqlite database
1515
- Send message to a matrix room with link and title
1616
- Serves an interface listing open help wanting issues
1717

18+
## Configuration
19+
20+
```.env
21+
GITHUB_TOKEN=
22+
23+
MATRIX_HOMESERVER=
24+
MATRIX_USERNAME=
25+
MATRIX_PASSWORD=
26+
MATRIX_ROOMID =
27+
28+
DB_FILE=
29+
30+
# coma separated labels with quotes to look for
31+
LABELS='"help-wanted","junior friendly","good first issue"'
32+
```
33+
1834
## How
1935

2036
Github graphql request
@@ -27,7 +43,7 @@ Github graphql request
2743
nameWithOwner
2844
description
2945
stargazerCount
30-
issues(states: OPEN, labels: ["help-wanted"], first: 5) {
46+
issues(states: OPEN, labels: ["$labels"], first: 5) {
3147
nodes {
3248
title
3349
url
@@ -74,8 +90,3 @@ Create migration
7490
```bash
7591
docker run -v $(pwd)/migrations:/migrations --network host migrate/migrate -path=/migrations -database "sqlite://db/help-the-stars.db" create -ext sql -dir /migrations -seq MIGRATION_NAME
7692
```
77-
78-
## todo
79-
80-
- [ ] parametrize labels Junior friendly, good first issue
81-
- [ ] golangci-lint

internal/datacontroller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (d *DataController) Worker() {
6666
d.GetAndSaveIssues()
6767

6868
} else {
69-
fmt.Print(".")
69+
log.Debug(".")
7070
time.Sleep(time.Duration(internalSeconds) * time.Millisecond)
7171
}
7272
}

internal/stars.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"golang.org/x/oauth2"
1515
)
1616

17+
const MAX_RETRY = 3
18+
const BACKOFF_DELAY = 10 * time.Second
19+
const MAX_ISSUES_PER_REPO = 5
20+
1721
type GhIssue struct {
1822
Title string
1923
Url string
@@ -44,17 +48,17 @@ type GhQuery struct {
4448
}
4549
}
4650

47-
const graphqlUrl = "https://api.github.com/graphql"
51+
const GRAPHQL_URL = "https://api.github.com/graphql"
4852

49-
const graphqlTemplate = `
53+
const GRAPHQL_TEMPLATE = `
5054
{
5155
viewer {
5256
starredRepositories(first: 50, after: "{{.RepoCursor}}") {
5357
nodes {
5458
nameWithOwner
5559
description
5660
stargazerCount
57-
issues(states: OPEN, labels: ["help-wanted"], first: 5) {
61+
issues(states: OPEN, labels: [{{.Labels}}], first: {{.MaxIssues}}) {
5862
nodes {
5963
title
6064
labels(first: 5) {
@@ -81,14 +85,19 @@ const graphqlTemplate = `
8185
}
8286
}
8387
`
88+
8489
// Parse the template once at package initialization
85-
var tmpl = template.Must(template.New("graphql").Parse(graphqlTemplate))
90+
var tmpl = template.Must(template.New("graphql").Parse(GRAPHQL_TEMPLATE))
8691

8792
func buildQueryFromTemplate(repoCursor string) (string, error) {
8893
data := struct {
8994
RepoCursor string
95+
Labels string
96+
MaxIssues int
9097
}{
9198
RepoCursor: repoCursor,
99+
Labels: GetSetting("LABELS"),
100+
MaxIssues: MAX_ISSUES_PER_REPO,
92101
}
93102

94103
var query bytes.Buffer
@@ -145,7 +154,7 @@ func fetchQueryResults(cursor string) (GhQuery, error) {
145154
}
146155

147156
// Create the HTTP request
148-
req, err := http.NewRequest("POST", graphqlUrl, bytes.NewBuffer(requestBody))
157+
req, err := http.NewRequest("POST", GRAPHQL_URL, bytes.NewBuffer(requestBody))
149158
if err != nil {
150159
log.Fatalf("Error creating request: %v", err)
151160
}
@@ -154,11 +163,11 @@ func fetchQueryResults(cursor string) (GhQuery, error) {
154163
req.Header.Set("Content-Type", "application/json")
155164

156165
// Send the request
157-
resp, err := httpClient.Do(req)
166+
resp, err := doWithRetry(httpClient, req)
158167
if err != nil {
159168
log.Fatalf("Error sending request: %v", err)
160169
}
161-
defer resp.Body.Close()
170+
defer closeBody(resp.Body)
162171

163172
// Read the response
164173
body, err := io.ReadAll(resp.Body)
@@ -174,3 +183,29 @@ func fetchQueryResults(cursor string) (GhQuery, error) {
174183

175184
return queryResult, nil
176185
}
186+
187+
func doWithRetry(http *http.Client, req *http.Request) (*http.Response, error) {
188+
i := 1
189+
for {
190+
resp, err := http.Do(req)
191+
if err == nil {
192+
if resp.StatusCode >= 500 {
193+
log.Fatalf("Github server error: %d", resp.StatusCode)
194+
}
195+
return resp, nil
196+
}
197+
198+
i++
199+
if i >= MAX_RETRY {
200+
return nil, err
201+
}
202+
time.Sleep(BACKOFF_DELAY * time.Duration(i))
203+
204+
}
205+
}
206+
207+
func closeBody(body io.ReadCloser) {
208+
if err := body.Close(); err != nil {
209+
log.Warn("Error closing response body: %v", err)
210+
}
211+
}

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ func main() {
2020
flag.Usage = func() {
2121
fmt.Println("Usage of Help the start ⭐")
2222
fmt.Println(" https://github.com/ad2ien/help-the-stars/")
23+
fmt.Println(".env example :")
24+
fmt.Println("\tMATRIX_TOKEN=your-matrix-token")
25+
fmt.Println("\tMATRIX_USERID=your-matrix-userid")
26+
fmt.Println("\tMATRIX_ROOMID=your-matrix-roomid")
27+
fmt.Println("\tDB_FILE=db/help-the-stars-dev.db")
28+
fmt.Println("\tLABELS='\"help-wanted\",\"junior friendly\",\"good first issue\"'")
2329
fmt.Println("\nFlags:")
2430
flag.PrintDefaults()
2531
}

0 commit comments

Comments
 (0)