Skip to content

Commit d222314

Browse files
authored
fix: memory leak (#506)
1 parent 9b89537 commit d222314

File tree

7 files changed

+10666
-6
lines changed

7 files changed

+10666
-6
lines changed

config/feature_flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import "os"
44

55
const FF_REPO_VIEW = "FF_REPO_VIEW"
66

7+
const FF_MOCK_DATA = "FF_MOCK_DATA"
8+
79
func IsFeatureEnabled(name string) bool {
810
_, ok := os.LookupEnv(name)
911
return ok

data/prapi.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package data
22

33
import (
4+
"crypto/tls"
45
"fmt"
6+
"net/http"
57
"net/url"
68
"time"
79

810
"github.com/charmbracelet/log"
911
gh "github.com/cli/go-gh/v2/pkg/api"
1012
graphql "github.com/cli/shurcooL-graphql"
1113
"github.com/shurcooL/githubv4"
14+
15+
"github.com/dlvhdr/gh-dash/v4/config"
1216
)
1317

1418
type PullRequestData struct {
@@ -191,9 +195,19 @@ type PullRequestsResponse struct {
191195
PageInfo PageInfo
192196
}
193197

198+
var client *gh.GraphQLClient
199+
194200
func FetchPullRequests(query string, limit int, pageInfo *PageInfo) (PullRequestsResponse, error) {
195201
var err error
196-
client, err := gh.DefaultGraphQLClient()
202+
if client == nil {
203+
if config.IsFeatureEnabled(config.FF_MOCK_DATA) {
204+
log.Debug("using mock data", "server", "https://localhost:3000")
205+
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
206+
client, err = gh.NewGraphQLClient(gh.ClientOptions{Host: "localhost:3000", AuthToken: "fake-token"})
207+
} else {
208+
client, err = gh.DefaultGraphQLClient()
209+
}
210+
}
197211

198212
if err != nil {
199213
return PullRequestsResponse{}, err
@@ -222,7 +236,7 @@ func FetchPullRequests(query string, limit int, pageInfo *PageInfo) (PullRequest
222236
if err != nil {
223237
return PullRequestsResponse{}, err
224238
}
225-
log.Debug("Successfully fetched PRs", "query", query, "count", queryResult.Search.IssueCount)
239+
log.Debug("Successfully fetched PRs", "count", queryResult.Search.IssueCount)
226240

227241
prs := make([]PullRequestData, 0, len(queryResult.Search.Nodes))
228242
for _, node := range queryResult.Search.Nodes {
@@ -264,7 +278,7 @@ func FetchPullRequest(prUrl string) (PullRequestData, error) {
264278
if err != nil {
265279
return PullRequestData{}, err
266280
}
267-
log.Debug("Successfully fetched PR", "url", prUrl, "data", queryResult.Resource.PullRequest)
281+
log.Debug("Successfully fetched PR", "url", prUrl)
268282

269283
return queryResult.Resource.PullRequest, nil
270284
}

imposters/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
imposters_path: "."
2+
port: 3000
3+
host: "localhost"
4+
watcher: true
5+
secure: true

imposters/gh-dash.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
prSections:
2+
- title: Mock Data
3+
filters: is:open

imposters/pr.imp.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[{
2+
"request": {
3+
"method": "POST",
4+
"endpoint": "/api/graphql"
5+
},
6+
"response": {
7+
"status": 200,
8+
"headers": {
9+
"Content-Type": "application/json"
10+
},
11+
"bodyFile": "pr.json"
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)