|
5 | 5 | "context" |
6 | 6 | "errors" |
7 | 7 | "fmt" |
| 8 | + "log/slog" |
8 | 9 | "os" |
9 | 10 | "strconv" |
10 | 11 | "strings" |
@@ -54,60 +55,63 @@ func PRDataFromPRX(prData *prx.PullRequestData) cost.PRData { |
54 | 55 | // - cost.PRData with all information needed for cost calculation |
55 | 56 | func FetchPRData(ctx context.Context, prURL string, token string) (cost.PRData, error) { |
56 | 57 | // Parse the PR URL to extract owner, repo, and PR number |
57 | | - parts, err := parsePRURL(prURL) |
| 58 | + owner, repo, number, err := parsePRURL(prURL) |
58 | 59 | if err != nil { |
| 60 | + slog.Error("Failed to parse PR URL", "url", prURL, "error", err) |
59 | 61 | return cost.PRData{}, fmt.Errorf("invalid PR URL: %w", err) |
60 | 62 | } |
61 | 63 |
|
| 64 | + slog.Debug("Parsed PR URL", "owner", owner, "repo", repo, "number", number) |
| 65 | + |
62 | 66 | // Create prx client |
63 | 67 | client := prx.NewClient(token) |
64 | 68 |
|
65 | | - // Fetch PR data using prx |
66 | | - prData, err := client.PullRequest(ctx, parts.owner, parts.repo, parts.number) |
| 69 | + // Fetch PR data using prx (prx has built-in retry logic) |
| 70 | + slog.Debug("Calling GitHub API via prx", "owner", owner, "repo", repo, "pr", number) |
| 71 | + prData, err := client.PullRequest(ctx, owner, repo, number) |
67 | 72 | if err != nil { |
| 73 | + slog.Error("GitHub API call failed", "owner", owner, "repo", repo, "pr", number, "error", err) |
68 | 74 | return cost.PRData{}, fmt.Errorf("failed to fetch PR data: %w", err) |
69 | 75 | } |
70 | 76 |
|
71 | | - // Convert to cost.PRData |
72 | | - return PRDataFromPRX(prData), nil |
73 | | -} |
| 77 | + slog.Debug("GitHub API call successful", |
| 78 | + "additions", prData.PullRequest.Additions, |
| 79 | + "author", prData.PullRequest.Author, |
| 80 | + "total_events", len(prData.Events)) |
74 | 81 |
|
75 | | -// prParts holds the parsed components of a GitHub PR URL. |
76 | | -type prParts struct { |
77 | | - owner string |
78 | | - repo string |
79 | | - number int |
| 82 | + // Convert to cost.PRData |
| 83 | + result := PRDataFromPRX(prData) |
| 84 | + slog.Debug("Converted PR data", "human_events", len(result.Events)) |
| 85 | + return result, nil |
80 | 86 | } |
81 | 87 |
|
82 | 88 | // parsePRURL extracts owner, repo, and PR number from a GitHub PR URL. |
83 | 89 | // Expected format: https://github.com/owner/repo/pull/123 |
84 | | -func parsePRURL(prURL string) (prParts, error) { |
| 90 | +// |
| 91 | +//nolint:revive // Four return values is simpler than creating a struct wrapper |
| 92 | +func parsePRURL(prURL string) (owner, repo string, number int, err error) { |
85 | 93 | // Remove protocol prefix |
86 | 94 | prURL = strings.TrimPrefix(prURL, "https://") |
87 | 95 | prURL = strings.TrimPrefix(prURL, "http://") |
88 | 96 |
|
89 | 97 | // Remove github.com prefix |
90 | 98 | if !strings.HasPrefix(prURL, "github.com/") { |
91 | | - return prParts{}, errors.New("URL must be from github.com") |
| 99 | + return "", "", 0, errors.New("URL must be from github.com") |
92 | 100 | } |
93 | 101 | prURL = strings.TrimPrefix(prURL, "github.com/") |
94 | 102 |
|
95 | 103 | // Split by / |
96 | 104 | parts := strings.Split(prURL, "/") |
97 | 105 | if len(parts) < 4 || parts[2] != "pull" { |
98 | | - return prParts{}, errors.New("expected format: https://github.com/owner/repo/pull/123") |
| 106 | + return "", "", 0, errors.New("expected format: https://github.com/owner/repo/pull/123") |
99 | 107 | } |
100 | 108 |
|
101 | | - number, err := strconv.Atoi(parts[3]) |
| 109 | + number, err = strconv.Atoi(parts[3]) |
102 | 110 | if err != nil { |
103 | | - return prParts{}, fmt.Errorf("invalid PR number: %w", err) |
| 111 | + return "", "", 0, fmt.Errorf("invalid PR number: %w", err) |
104 | 112 | } |
105 | 113 |
|
106 | | - return prParts{ |
107 | | - owner: parts[0], |
108 | | - repo: parts[1], |
109 | | - number: number, |
110 | | - }, nil |
| 114 | + return parts[0], parts[1], number, nil |
111 | 115 | } |
112 | 116 |
|
113 | 117 | // FetchPRDataWithDefaults is a convenience function that uses environment variables |
|
0 commit comments