-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubApi.js
More file actions
55 lines (45 loc) · 1.5 KB
/
githubApi.js
File metadata and controls
55 lines (45 loc) · 1.5 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
import fetch from "node-fetch";
import { CONFIG } from "./config.js";
export class GitHubAPI {
static getHeaders() {
const headers = {
"User-Agent": "GitHub-Polling-Bot",
"Accept": "application/vnd.github.v3+json",
};
if (CONFIG.GITHUB_TOKEN) {
headers["Authorization"] = `token ${CONFIG.GITHUB_TOKEN}`;
}
return headers;
}
static async fetchLatestCommit() {
const res = await fetch(
`https://v-api-github-com.286600.xyz/repos/${CONFIG.OWNER}/${CONFIG.REPO}/commits?sha=${CONFIG.BRANCH}&per_page=1`,
{ headers: this.getHeaders(), timeout: 15_000 }
);
if (!res.ok) {
const remaining = res.headers.get("x-ratelimit-remaining");
const reset = res.headers.get("x-ratelimit-reset");
if (res.status === 403 && remaining === "0") {
const resetDate = new Date(reset * 1000);
throw new Error(`Rate limit exceeded. Reset at ${resetDate.toLocaleTimeString()}`);
}
throw new Error(`GitHub API error: ${res.status} ${res.statusText}`);
}
const commits = await res.json();
return commits[0];
}
static async getCommitDetails(sha) {
try {
const res = await fetch(
`https://v-api-github-com.286600.xyz/repos/${CONFIG.OWNER}/${CONFIG.REPO}/commits/${sha}`,
{ headers: this.getHeaders(), timeout: 10_000 }
);
if (res.ok) {
return await res.json();
}
} catch (err) {
console.warn("[WARN] Failed to fetch commit details:", err.message);
}
return null;
}
}