-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (146 loc) · 4.46 KB
/
Copy pathindex.js
File metadata and controls
161 lines (146 loc) · 4.46 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require("dotenv").config();
const axios = require("axios");
const fs = require("fs").promises;
const cron = require("node-cron");
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
const GITHUB_ORG = process.env.GITHUB_ORG;
const PROJECT_NUMBER = parseInt(process.env.PROJECT_NUMBER, 10);
const STATE_FILE_PATH = "./data/project_state.json";
const GITHUB_API_URL = "https://api.github.com/graphql";
const GITHUB_QUERY = `
query getProjectItems($org: String!, $number: Int!) {
organization(login: $org) {
projectV2(number: $number) {
items(first: 100) {
nodes {
id
content {
... on DraftIssue { title }
... on Issue { title }
... on PullRequest { title }
}
fieldValueByName(name: "Status") {
... on ProjectV2ItemFieldSingleSelectValue {
name
}
}
}
}
}
}
}
`;
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function loadPreviousState() {
try {
const data = await fs.readFile(STATE_FILE_PATH, "utf8");
return JSON.parse(data);
} catch (error) {
console.log("Previous state file not found. Assuming first run.");
return {};
}
}
async function saveCurrentState(currentState) {
await fs.writeFile(STATE_FILE_PATH, JSON.stringify(currentState, null, 2));
}
async function fetchCurrentState() {
const response = await axios.post(
GITHUB_API_URL,
{
query: GITHUB_QUERY,
variables: { org: GITHUB_ORG, number: PROJECT_NUMBER },
},
{
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
},
},
);
const items = response.data.data.organization.projectV2.items.nodes;
const currentState = {};
for (const item of items) {
if (item.content) {
currentState[item.id] = {
title: item.content.title,
status: item.fieldValueByName?.name || "NULL",
};
}
}
return currentState;
}
async function sendToDiscord(embed) {
try {
await axios.post(DISCORD_WEBHOOK_URL, { embeds: [embed] });
console.log(`Notification sent for: ${embed.description.split("\n")[0]}`);
} catch (error) {
console.error(
`Error sending notification for ${embed.description.split("\n")[0]}: ${error.message}`,
);
}
}
async function runCheck() {
console.log("Running check...");
const previousState = await loadPreviousState();
const currentState = await fetchCurrentState();
const changesToSend = [];
for (const itemId in currentState) {
const currentItem = currentState[itemId];
const previousItem = previousState[itemId];
let embed = null;
if (!previousItem) {
console.log(`New item detected: ${currentItem.title}`);
embed = {
title: "✅ New Task Added",
description: `**Task:** ${currentItem.title}\n**Status:** ${currentItem.status}`,
color: 0x3498db,
};
} else if (currentItem.status !== previousItem.status) {
console.log(
`Status changed for ${currentItem.title}: ${previousItem.status} -> ${currentItem.status}`,
);
const newStatus = currentItem.status;
if (newStatus === "Todo") {
embed = {
title: "🟢 Task Moved",
description: `**Task:** ${currentItem.title}\n**New Status:** ${newStatus}`,
color: 0x368cfe,
};
}
if (newStatus === "Pending" || newStatus === "In Progress") {
embed = {
title: "🟡 Task Moved",
description: `**Task:** ${currentItem.title}\n**New Status:** ${newStatus}`,
color: 0xfee75c,
};
} else if (newStatus === "Done") {
embed = {
title: "🟣 Task Completed!",
description: `**Task:** ${currentItem.title}`,
color: 0x800080,
};
}
}
if (embed) {
changesToSend.push(embed);
}
}
if (changesToSend.length > 0) {
console.log(
`Found ${changesToSend.length} changes. Sending notifications sequentially...`,
);
for (const embed of changesToSend) {
await sendToDiscord(embed);
await sleep(1000);
}
} else {
console.log("No changes detected.");
}
await saveCurrentState(currentState);
console.log("Check complete. Current state saved.");
}
cron.schedule("*/10 * * * *", () => {
runCheck().catch((error) => {
console.error("An error occurred during the scheduled run:", error);
});
});