-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubPoller.js
More file actions
94 lines (82 loc) · 3.13 KB
/
GitHubPoller.js
File metadata and controls
94 lines (82 loc) · 3.13 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import fs from "fs/promises";
import { CONFIG } from "./config.js";
import { GitHubAPI } from "./githubApi.js";
import { MessageFormatter } from "./messageFormatter.js";
import { getUptime } from "./utils.js";
export class GitHubPoller {
constructor(sock) {
this.sock = sock;
this.lastCommit = null;
this.isPolling = false;
this.retryCount = 0;
this.stats = {
totalChecks: 0,
newCommits: 0,
errors: 0,
startTime: Date.now(),
};
}
async initialize() {
try {
const data = await fs.readFile(CONFIG.LAST_FILE, "utf8");
this.lastCommit = data.trim();
console.log(`[INIT] Loaded SHA: ${this.lastCommit?.slice(0, 7) || "none"}`);
} catch {
console.log("[INIT] Starting fresh - no previous commit");
}
}
async notifyNewCommit(commit) {
const details = await GitHubAPI.getCommitDetails(commit.sha);
const message = MessageFormatter.formatCommitNotification(commit, details);
await this.sock.sendMessage(CONFIG.TARGET_JID, { text: message });
this.stats.newCommits++;
console.log(`[✓] Commit sent: ${commit.sha.slice(0, 7)} | Total: ${this.stats.newCommits}`);
}
async check() {
if (this.isPolling) return;
this.isPolling = true;
this.stats.totalChecks++;
try {
const commit = await GitHubAPI.fetchLatestCommit();
if (!commit) {
console.log("[CHECK] No commits found");
return;
}
if (commit.sha !== this.lastCommit) {
this.lastCommit = commit.sha;
await fs.writeFile(CONFIG.LAST_FILE, commit.sha);
await this.notifyNewCommit(commit);
this.retryCount = 0;
} else {
process.stdout.write(`\r[CHECK] No new commits | Checks: ${this.stats.totalChecks} | Uptime: ${getUptime(this.stats.startTime)}`);
}
} catch (err) {
this.retryCount++;
this.stats.errors++;
console.error(`\n[ERROR] Poll failed (${this.retryCount}/${CONFIG.MAX_RETRIES}): ${err.message}`);
if (this.retryCount >= CONFIG.MAX_RETRIES) {
console.warn("[WARN] Max retries reached, resetting counter");
this.retryCount = 0;
}
} finally {
this.isPolling = false;
const delay = this.retryCount > 0 ? CONFIG.RETRY_DELAY : CONFIG.POLL_INTERVAL;
setTimeout(() => this.check(), delay);
}
}
start() {
console.log(`[POLLER] Started polling every ${CONFIG.POLL_INTERVAL / 1000}s`);
console.log(`[POLLER] Watching: ${CONFIG.OWNER}/${CONFIG.REPO}@${CONFIG.BRANCH}`);
this.check();
}
printStats() {
console.log("\n╭─────────────────────────╮");
console.log("│ POLLING STATISTICS │");
console.log("├─────────────────────────┤");
console.log(`│ Total Checks: ${this.stats.totalChecks}`);
console.log(`│ New Commits: ${this.stats.newCommits}`);
console.log(`│ Errors: ${this.stats.errors}`);
console.log(`│ Uptime: ${getUptime(this.stats.startTime)}`);
console.log("╰─────────────────────────╯\n");
}
}