|
| 1 | +import fs from "fs/promises"; |
| 2 | +import { CONFIG } from "./config.js"; |
| 3 | +import { GitHubAPI } from "./githubApi.js"; |
| 4 | +import { MessageFormatter } from "./messageFormatter.js"; |
| 5 | +import { getUptime } from "./utils.js"; |
| 6 | + |
| 7 | +export class GitHubPoller { |
| 8 | + constructor(sock) { |
| 9 | + this.sock = sock; |
| 10 | + this.lastCommit = null; |
| 11 | + this.isPolling = false; |
| 12 | + this.retryCount = 0; |
| 13 | + this.stats = { |
| 14 | + totalChecks: 0, |
| 15 | + newCommits: 0, |
| 16 | + errors: 0, |
| 17 | + startTime: Date.now(), |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + async initialize() { |
| 22 | + try { |
| 23 | + const data = await fs.readFile(CONFIG.LAST_FILE, "utf8"); |
| 24 | + this.lastCommit = data.trim(); |
| 25 | + console.log(`[INIT] Loaded SHA: ${this.lastCommit?.slice(0, 7) || "none"}`); |
| 26 | + } catch { |
| 27 | + console.log("[INIT] Starting fresh - no previous commit"); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + async notifyNewCommit(commit) { |
| 32 | + const details = await GitHubAPI.getCommitDetails(commit.sha); |
| 33 | + const message = MessageFormatter.formatCommitNotification(commit, details); |
| 34 | + |
| 35 | + await this.sock.sendMessage(CONFIG.TARGET_JID, { text: message }); |
| 36 | + |
| 37 | + this.stats.newCommits++; |
| 38 | + console.log(`[✓] Commit sent: ${commit.sha.slice(0, 7)} | Total: ${this.stats.newCommits}`); |
| 39 | + } |
| 40 | + |
| 41 | + async check() { |
| 42 | + if (this.isPolling) return; |
| 43 | + this.isPolling = true; |
| 44 | + this.stats.totalChecks++; |
| 45 | + |
| 46 | + try { |
| 47 | + const commit = await GitHubAPI.fetchLatestCommit(); |
| 48 | + |
| 49 | + if (!commit) { |
| 50 | + console.log("[CHECK] No commits found"); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (commit.sha !== this.lastCommit) { |
| 55 | + this.lastCommit = commit.sha; |
| 56 | + await fs.writeFile(CONFIG.LAST_FILE, commit.sha); |
| 57 | + await this.notifyNewCommit(commit); |
| 58 | + this.retryCount = 0; |
| 59 | + } else { |
| 60 | + process.stdout.write(`\r[CHECK] No new commits | Checks: ${this.stats.totalChecks} | Uptime: ${getUptime(this.stats.startTime)}`); |
| 61 | + } |
| 62 | + } catch (err) { |
| 63 | + this.retryCount++; |
| 64 | + this.stats.errors++; |
| 65 | + console.error(`\n[ERROR] Poll failed (${this.retryCount}/${CONFIG.MAX_RETRIES}): ${err.message}`); |
| 66 | + |
| 67 | + if (this.retryCount >= CONFIG.MAX_RETRIES) { |
| 68 | + console.warn("[WARN] Max retries reached, resetting counter"); |
| 69 | + this.retryCount = 0; |
| 70 | + } |
| 71 | + } finally { |
| 72 | + this.isPolling = false; |
| 73 | + const delay = this.retryCount > 0 ? CONFIG.RETRY_DELAY : CONFIG.POLL_INTERVAL; |
| 74 | + setTimeout(() => this.check(), delay); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + start() { |
| 79 | + console.log(`[POLLER] Started polling every ${CONFIG.POLL_INTERVAL / 1000}s`); |
| 80 | + console.log(`[POLLER] Watching: ${CONFIG.OWNER}/${CONFIG.REPO}@${CONFIG.BRANCH}`); |
| 81 | + this.check(); |
| 82 | + } |
| 83 | + |
| 84 | + printStats() { |
| 85 | + console.log("\n╭─────────────────────────╮"); |
| 86 | + console.log("│ POLLING STATISTICS │"); |
| 87 | + console.log("├─────────────────────────┤"); |
| 88 | + console.log(`│ Total Checks: ${this.stats.totalChecks}`); |
| 89 | + console.log(`│ New Commits: ${this.stats.newCommits}`); |
| 90 | + console.log(`│ Errors: ${this.stats.errors}`); |
| 91 | + console.log(`│ Uptime: ${getUptime(this.stats.startTime)}`); |
| 92 | + console.log("╰─────────────────────────╯\n"); |
| 93 | + } |
| 94 | +} |
0 commit comments