@@ -88,7 +88,6 @@ func PRDataFromPRX(prData *prx.PullRequestData) cost.PRData {
8888// Returns:
8989// - cost.PRData with all information needed for cost calculation
9090func FetchPRData (ctx context.Context , prURL string , token string , updatedAt time.Time ) (cost.PRData , error ) {
91- // Parse the PR URL to extract owner, repo, and PR number
9291 owner , repo , number , err := parsePRURL (prURL )
9392 if err != nil {
9493 slog .Error ("Failed to parse PR URL" , "url" , prURL , "error" , err )
@@ -97,46 +96,33 @@ func FetchPRData(ctx context.Context, prURL string, token string, updatedAt time
9796
9897 slog .Debug ("Parsed PR URL" , "owner" , owner , "repo" , repo , "number" , number )
9998
100- // Get cache directory from user's cache directory
101- userCacheDir , err := os .UserCacheDir ()
102- if err != nil {
103- slog .Warn ("Failed to get cache directory, using non-cached client" , "error" , err )
104- // Fallback to non-cached client
105- client := prx .NewClient (token )
106- prData , err := client .PullRequest (ctx , owner , repo , number )
107- if err != nil {
108- slog .Error ("GitHub API call failed" , "owner" , owner , "repo" , repo , "pr" , number , "error" , err )
109- return cost.PRData {}, fmt .Errorf ("failed to fetch PR data: %w" , err )
99+ // Try to use cache client if possible, fall back to non-cached client
100+ var prData * prx.PullRequestData
101+ cacheDir := ""
102+ if userCacheDir , err := os .UserCacheDir (); err == nil {
103+ cacheDir = filepath .Join (userCacheDir , "prcost" )
104+ if err := os .MkdirAll (cacheDir , 0o700 ); err != nil {
105+ slog .Warn ("Failed to create cache directory, using non-cached client" , "error" , err )
106+ cacheDir = ""
110107 }
111- result := PRDataFromPRX ( prData )
112- return result , nil
108+ } else {
109+ slog . Warn ( "Failed to get cache directory, using non-cached client" , "error" , err )
113110 }
114111
115- cacheDir := filepath .Join (userCacheDir , "prcost" )
116- if err := os .MkdirAll (cacheDir , 0o700 ); err != nil {
117- slog .Warn ("Failed to create cache directory, using non-cached client" , "error" , err )
118- // Fallback to non-cached client
119- client := prx .NewClient (token )
120- prData , err := client .PullRequest (ctx , owner , repo , number )
121- if err != nil {
122- slog .Error ("GitHub API call failed" , "owner" , owner , "repo" , repo , "pr" , number , "error" , err )
123- return cost.PRData {}, fmt .Errorf ("failed to fetch PR data: %w" , err )
112+ if cacheDir != "" {
113+ client , cErr := prx .NewCacheClient (token , cacheDir )
114+ if cErr != nil {
115+ slog .Error ("Failed to create cache client" , "error" , cErr )
116+ return cost.PRData {}, fmt .Errorf ("failed to create cache client: %w" , cErr )
124117 }
125- result := PRDataFromPRX (prData )
126- return result , nil
127- }
128-
129- // Create prx cache client for disk-based caching
130- client , err := prx .NewCacheClient (token , cacheDir )
131- if err != nil {
132- slog .Error ("Failed to create cache client" , "error" , err )
133- return cost.PRData {}, fmt .Errorf ("failed to create cache client: %w" , err )
118+ slog .Debug ("Calling GitHub API via prx cache client" ,
119+ "owner" , owner , "repo" , repo , "pr" , number , "updated_at" , updatedAt .Format (time .RFC3339 ))
120+ prData , err = client .PullRequest (ctx , owner , repo , number , updatedAt )
121+ } else {
122+ client := prx .NewClient (token )
123+ prData , err = client .PullRequest (ctx , owner , repo , number )
134124 }
135125
136- // Fetch PR data using prx (prx has built-in retry logic and caching)
137- // Pass updatedAt for effective cache validation
138- slog .Debug ("Calling GitHub API via prx cache client" , "owner" , owner , "repo" , repo , "pr" , number , "updated_at" , updatedAt .Format (time .RFC3339 ))
139- prData , err := client .PullRequest (ctx , owner , repo , number , updatedAt )
140126 if err != nil {
141127 slog .Error ("GitHub API call failed" , "owner" , owner , "repo" , repo , "pr" , number , "error" , err )
142128 return cost.PRData {}, fmt .Errorf ("failed to fetch PR data: %w" , err )
@@ -148,7 +134,6 @@ func FetchPRData(ctx context.Context, prURL string, token string, updatedAt time
148134 "author" , prData .PullRequest .Author ,
149135 "total_events" , len (prData .Events ))
150136
151- // Convert to cost.PRData
152137 result := PRDataFromPRX (prData )
153138 slog .Debug ("Converted PR data" , "human_events" , len (result .Events ))
154139 return result , nil
0 commit comments