-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-utils.ts
More file actions
75 lines (69 loc) · 2.66 KB
/
fetch-utils.ts
File metadata and controls
75 lines (69 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import axios from "axios";
// Fetch data with rate limit handling
export const fetchWithRateLimit = async (
url: string,
headers: { Authorization: string; Accept: string },
params?: any
): Promise<any> => {
try {
const response = await axios.get(url, { headers, params });
return response.data;
} catch (error: any) {
// Handle rate limit errors
if (
error.response?.status === 403 &&
error.response.headers["x-ratelimit-remaining"] === "0"
) {
const resetTime = parseInt(
error.response.headers["x-ratelimit-reset"],
10
);
const sleepTime =
Math.max(0, resetTime - Math.floor(Date.now() / 1000)) + 1;
console.log(`Rate limit exceeded. Sleeping for ${sleepTime} seconds...`);
await new Promise((resolve) => setTimeout(resolve, sleepTime * 1000));
return fetchWithRateLimit(url, headers, params);
}
// Add retries for server errors (5xx)
if (error.response?.status >= 500 && error.response?.status < 600) {
const retryDelay = 5000; // 5 seconds
console.log(`Server error ${error.response.status}, retrying in ${retryDelay/1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
return fetchWithRateLimit(url, headers, params);
}
// Log other API errors with minimal details
console.error(`GitHub API request failed for ${url}: ${error.message}`);
throw error;
}
};
// Unified function to fetch reactions for a comment
export const fetchCommentReactions = async (
owner: string,
repo: string,
comment: any,
headers: { Authorization: string; Accept: string }
): Promise<any[]> => {
try {
// Review comments have a path property or are explicitly marked as review type
const isReviewComment = !!comment.path || (typeof comment === 'object' && comment.type === 'review');
const commentId = typeof comment === 'object' ? comment.id : comment;
const url = isReviewComment
? `https://api.github.com/repos/${owner}/${repo}/pulls/comments/${commentId}/reactions`
: `https://api.github.com/repos/${owner}/${repo}/issues/comments/${commentId}/reactions`;
return await fetchWithRateLimit(url, headers);
} catch (error: any) {
// If it's a 404, the comment was likely deleted
if (error.response?.status === 404) {
console.log(
`Comment ${typeof comment === 'object' ? comment.id : comment} was deleted or is no longer accessible`
);
return [];
}
// For other errors, log more details
console.error(
`Error fetching reactions for comment ${typeof comment === 'object' ? comment.id : comment}:`,
error.message
);
return [];
}
};