Add detailed logging for Gitea API errors to diagnose 401 issues.
Some checks failed
CI / test (pull_request) Has been cancelled
Some checks failed
CI / test (pull_request) Has been cancelled
Extracts token presence/length, response status, failed URL, and response body (first 500 chars) into structured logs. This will help identify whether token is missing, invalid, or if Gitea is rejecting for another reason. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a37dc8b503
commit
829e1ea1dc
|
|
@ -1,18 +1,33 @@
|
|||
import { log } from "../log"
|
||||
import type { Gitea } from "./types"
|
||||
|
||||
const giteaUrl = "https://git.nose.space"
|
||||
|
||||
export async function fetchPR(fullname: string, prNumber: number): Promise<Gitea.PullRequest> {
|
||||
const url = `${giteaUrl}/api/v1/repos/${fullname}/pulls/${prNumber}`
|
||||
function tokenPrefix(): string {
|
||||
const token = process.env.GITEA_API_TOKEN
|
||||
if (!token) return "MISSING"
|
||||
return `${token.slice(0, 4)}...${token.slice(-4)} (${token.length} chars)`
|
||||
}
|
||||
|
||||
async function giteaFetch(url: string): Promise<Response> {
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `token ${process.env.GITEA_API_TOKEN}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await response.text()
|
||||
log({ type: "gitea-api-error", status: response.status, url, body: body.slice(0, 500), tokenPrefix: tokenPrefix() })
|
||||
throw new Error(`Gitea API error: ${response.status} ${response.statusText}`)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export async function fetchPR(fullname: string, prNumber: number): Promise<Gitea.PullRequest> {
|
||||
const url = `${giteaUrl}/api/v1/repos/${fullname}/pulls/${prNumber}`
|
||||
const response = await giteaFetch(url)
|
||||
return response.json() as Promise<Gitea.PullRequest>
|
||||
}
|
||||
|
||||
|
|
@ -20,16 +35,8 @@ export async function fetchReviewComments(
|
|||
fullname: string,
|
||||
prNumber: number
|
||||
): Promise<Gitea.ReviewComment[]> {
|
||||
// First, fetch all reviews
|
||||
const reviewsUrl = `${giteaUrl}/api/v1/repos/${fullname}/pulls/${prNumber}/reviews`
|
||||
const reviewsResponse = await fetch(reviewsUrl, {
|
||||
headers: {
|
||||
Authorization: `token ${process.env.GITEA_API_TOKEN}`,
|
||||
},
|
||||
})
|
||||
if (!reviewsResponse.ok) {
|
||||
throw new Error(`Gitea API error: ${reviewsResponse.status} ${reviewsResponse.statusText}`)
|
||||
}
|
||||
const reviewsResponse = await giteaFetch(reviewsUrl)
|
||||
|
||||
const reviews = (await reviewsResponse.json()) as Array<{
|
||||
id: number
|
||||
|
|
@ -40,21 +47,12 @@ export async function fetchReviewComments(
|
|||
comments_count: number
|
||||
}>
|
||||
|
||||
// For each review, fetch its comments
|
||||
const allComments: Gitea.ReviewComment[] = []
|
||||
for (const review of reviews) {
|
||||
if (review.comments_count === 0) continue
|
||||
|
||||
const commentsUrl = `${giteaUrl}/api/v1/repos/${fullname}/pulls/${prNumber}/reviews/${review.id}/comments`
|
||||
const commentsResponse = await fetch(commentsUrl, {
|
||||
headers: {
|
||||
Authorization: `token ${process.env.GITEA_API_TOKEN}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!commentsResponse.ok) {
|
||||
throw new Error(`Gitea API error fetching review ${review.id} comments: ${commentsResponse.status} ${commentsResponse.statusText}`)
|
||||
}
|
||||
const commentsResponse = await giteaFetch(commentsUrl)
|
||||
const comments = (await commentsResponse.json()) as Gitea.ReviewComment[]
|
||||
allComments.push(...comments)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ export type LogEvent =
|
|||
| { type: "crash-log-found" }
|
||||
| { type: "startup"; detail: string }
|
||||
| { type: "discord-ready" }
|
||||
| { type: "gitea-api-error"; status: number; url: string; body: string; tokenPrefix: string }
|
||||
| { type: "error"; error: unknown; context?: string }
|
||||
|
||||
export type StoredLogEvent = LogEvent & { ts: string }
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user