-
Notifications
You must be signed in to change notification settings - Fork 23
134 lines (113 loc) · 4.51 KB
/
message_bot.yml
File metadata and controls
134 lines (113 loc) · 4.51 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
# .github/workflows/discussion-monitor.yml
name: Discussion Monitor
on:
schedule:
- cron: '0 9 * * *'
jobs:
check-discussions:
runs-on: ubuntu-latest
permissions:
contents: read
discussions: read
steps:
- name: Check for new discussions
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_WEBHOOK_URL }}
run: |
npm install @octokit/rest
cat > check.js << 'EOF'
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
async function check() {
const since = new Date();
since.setHours(since.getHours() - 25);
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
console.log(`Checking discussions for ${owner}/${repo}`);
try {
const response = await octokit.graphql(`
query {
repository(owner: "${owner}", name: "${repo}") {
discussions(first: 10, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
createdAt
title
url
number
author {
login
}
category {
name
emoji
}
}
}
}
}
`);
console.log('Full GraphQL response:', JSON.stringify(response, null, 2));
if (!response || !response.repository) {
console.log('Repository not found or discussions not enabled');
console.log('Response keys:', Object.keys(response || {}));
return;
}
if (!response.repository.discussions) {
console.log('Discussions not available for this repository');
return;
}
const newDiscussions = response.repository.discussions.nodes.filter(
d => new Date(d.createdAt) > since
);
console.log(`Found ${newDiscussions.length} new discussions`);
if (newDiscussions.length > 0) {
let message;
if (newDiscussions.length === 1) {
const discussion = newDiscussions[0];
message = {
text: `${discussion.category.emoji || '💬'} New Discussion on ${owner}/${repo}
**[${discussion.title}](${discussion.url})**
👤 **Author:** ${discussion.author.login}
📂 **Category:** ${discussion.category.name}
🔢 **Discussion #${discussion.number}**`,
username: "GitHub Discussion Bot",
icon_emoji: ":github:"
};
} else {
const discussionList = newDiscussions.map(d =>
`- ${d.category.emoji || '💬'} **[${d.title}](${d.url})** by ${d.author.login}`
).join('\n');
message = {
text: `🎉 ${newDiscussions.length} New Discussions on ${owner}/${repo}
${discussionList}
---
*Check out the latest discussions above!*`,
username: "GitHub Discussion Bot",
icon_emoji: ":github:"
};
}
const response = await fetch(process.env.MATTERMOST_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(message)
});
console.log('Mattermost notification sent');
}
} catch (error) {
console.error('Error details:', error);
if (process.env.MATTERMOST_WEBHOOK_URL) {
await fetch(process.env.MATTERMOST_WEBHOOK_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: '⚠️ **GitHub Discussion Bot Error**\nThere was an error checking discussions. Please check the workflow logs.',
username: "GitHub Discussion Bot",
icon_emoji: ":warning:"
})
});
}
}
}
check().catch(console.error);
EOF
node check.js