Skip to content

Commit

Permalink
fix: memory leak (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr authored Jan 11, 2025
1 parent 9b89537 commit d222314
Show file tree
Hide file tree
Showing 7 changed files with 10,666 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "os"

const FF_REPO_VIEW = "FF_REPO_VIEW"

const FF_MOCK_DATA = "FF_MOCK_DATA"

func IsFeatureEnabled(name string) bool {
_, ok := os.LookupEnv(name)
return ok
Expand Down
20 changes: 17 additions & 3 deletions data/prapi.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package data

import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"time"

"github.com/charmbracelet/log"
gh "github.com/cli/go-gh/v2/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
"github.com/shurcooL/githubv4"

"github.com/dlvhdr/gh-dash/v4/config"
)

type PullRequestData struct {
Expand Down Expand Up @@ -191,9 +195,19 @@ type PullRequestsResponse struct {
PageInfo PageInfo
}

var client *gh.GraphQLClient

func FetchPullRequests(query string, limit int, pageInfo *PageInfo) (PullRequestsResponse, error) {
var err error
client, err := gh.DefaultGraphQLClient()
if client == nil {
if config.IsFeatureEnabled(config.FF_MOCK_DATA) {
log.Debug("using mock data", "server", "https://localhost:3000")
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client, err = gh.NewGraphQLClient(gh.ClientOptions{Host: "localhost:3000", AuthToken: "fake-token"})
} else {
client, err = gh.DefaultGraphQLClient()
}
}

if err != nil {
return PullRequestsResponse{}, err
Expand Down Expand Up @@ -222,7 +236,7 @@ func FetchPullRequests(query string, limit int, pageInfo *PageInfo) (PullRequest
if err != nil {
return PullRequestsResponse{}, err
}
log.Debug("Successfully fetched PRs", "query", query, "count", queryResult.Search.IssueCount)
log.Debug("Successfully fetched PRs", "count", queryResult.Search.IssueCount)

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

return queryResult.Resource.PullRequest, nil
}
5 changes: 5 additions & 0 deletions imposters/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
imposters_path: "."
port: 3000
host: "localhost"
watcher: true
secure: true
3 changes: 3 additions & 0 deletions imposters/gh-dash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
prSections:
- title: Mock Data
filters: is:open
14 changes: 14 additions & 0 deletions imposters/pr.imp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[{
"request": {
"method": "POST",
"endpoint": "/api/graphql"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFile": "pr.json"
}
}
]
Loading

0 comments on commit d222314

Please sign in to comment.