-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (61 loc) · 2.42 KB
/
index.js
File metadata and controls
78 lines (61 loc) · 2.42 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
import dotenv from "dotenv";
import axios from "axios";
import { Telegraf } from 'telegraf';
import fs from "fs";
dotenv.config();
// read config
const config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
const repo = config.repo;
const filterLabels = config.labels || []; // labels to track
// init telegram bot
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
// GitHub API URL (fetch newest created issues first, up to 100)
const GITHUB_API = `https://api.github.com/repos/${repo}/issues?sort=created&direction=desc&per_page=100`;
async function checkLatestIssue() {
try {
// Read last stored issue ID
let lastIssueId = 0;
if (fs.existsSync("last_issue.txt")) {
lastIssueId = parseInt(fs.readFileSync("last_issue.txt", "utf8"));
}
const response = await axios.get(GITHUB_API, {
headers: {
"Accept": "application/vnd.github+json",
"Authorization": `Bearer ${process.env.GITHUB_TOKEN}`,
},
});
let issues = response.data;
// FILTER OUT PRs
issues = issues.filter((i) => !i.pull_request);
// FILTER BY LABELS (case-insensitive)
if (filterLabels.length > 0) {
issues = issues.filter((issue) => {
const issueLabels = issue.labels.map((l) => l.name.toLowerCase().trim());
return filterLabels.some((label) => issueLabels.includes(label.toLowerCase().trim()));
});
}
if (issues.length === 0) {
console.log("No issues found matching your labels.");
return;
}
// FILTER only NEW issues (id > lastIssueId)
const newIssues = issues.filter((issue) => issue.id > lastIssueId);
if (newIssues.length === 0) {
console.log("No new issues.");
return;
}
// Notify all new issues
for (const issue of newIssues.reverse()) {
console.log(`New issue: #${issue.number} - ${issue.title}`);
console.log(`URL: ${issue.html_url}`);
const message = `New issue detected!\n\n#${issue.number} - ${issue.title}\n${issue.html_url}`;
await bot.telegram.sendMessage(process.env.TELEGRAM_CHAT_ID, message);
console.log(`Sent: #${issue.number} - ${issue.title}`);
// update lastIssueId after each message
fs.writeFileSync("last_issue.txt", String(issue.id));
}
} catch (err) {
console.error("Error fetching issues:", err.response ? err.response.data : err);
}
}
checkLatestIssue();