( ctx context.Context, requestURL string, token string, dest any, )
| 385 | } |
| 386 | |
| 387 | func (g *githubProvider) decodeJSON( |
| 388 | ctx context.Context, |
| 389 | requestURL string, |
| 390 | token string, |
| 391 | dest any, |
| 392 | ) error { |
| 393 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil) |
| 394 | if err != nil { |
| 395 | return xerrors.Errorf("create github request: %w", err) |
| 396 | } |
| 397 | req.Header.Set("Accept", "application/vnd.github+json") |
| 398 | req.Header.Set("X-GitHub-Api-Version", "2022-11-28") |
| 399 | req.Header.Set("User-Agent", "coder-chat-diff-status") |
| 400 | if token != "" { |
| 401 | req.Header.Set("Authorization", "Bearer "+token) |
| 402 | } |
| 403 | |
| 404 | resp, err := g.httpClient.Do(req) |
| 405 | if err != nil { |
| 406 | return xerrors.Errorf("execute github request: %w", err) |
| 407 | } |
| 408 | defer resp.Body.Close() |
| 409 | |
| 410 | if resp.StatusCode != http.StatusOK { |
| 411 | if rlErr := checkRateLimitError(resp, g.clock, "X-Ratelimit-Reset"); rlErr != nil { |
| 412 | return rlErr |
| 413 | } |
| 414 | body, readErr := io.ReadAll(io.LimitReader(resp.Body, 8192)) |
| 415 | if readErr != nil { |
| 416 | return xerrors.Errorf( |
| 417 | "github request failed with status %d", |
| 418 | resp.StatusCode, |
| 419 | ) |
| 420 | } |
| 421 | return xerrors.Errorf( |
| 422 | "github request failed with status %d: %s", |
| 423 | resp.StatusCode, |
| 424 | strings.TrimSpace(string(body)), |
| 425 | ) |
| 426 | } |
| 427 | |
| 428 | if err := json.NewDecoder(resp.Body).Decode(dest); err != nil { |
| 429 | return xerrors.Errorf("decode github response: %w", err) |
| 430 | } |
| 431 | return nil |
| 432 | } |
| 433 | |
| 434 | func (g *githubProvider) fetchDiff( |
| 435 | ctx context.Context, |
no test coverage detected