@@ -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+
1721type 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
8792func 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+ }
0 commit comments