|
| 1 | +import { info, setFailed } from '@actions/core' |
| 2 | +import { getOctokit } from '@actions/github' |
| 3 | +import { WebClient } from '@slack/web-api' |
| 4 | +import { daysAgo } from './lib/utils' |
| 5 | +import { SlimIssue } from './types' |
| 6 | + |
| 7 | +const DAYS_WINDOW = 90 |
| 8 | + |
| 9 | +function generateText(issues: SlimIssue[]) { |
| 10 | + let text = `*A list of the top 10 issues sorted by the most reactions over the last ${DAYS_WINDOW} days:*\n\n` |
| 11 | + |
| 12 | + // Format date as "X days ago" |
| 13 | + const formattedDaysAgo = (dateString: string) => { |
| 14 | + const date = new Date(dateString) |
| 15 | + const now = new Date() |
| 16 | + const diffTime = Math.abs(now.getTime() - date.getTime()) |
| 17 | + const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) |
| 18 | + return `${diffDays} day${diffDays > 1 ? 's' : ''} ago` |
| 19 | + } |
| 20 | + |
| 21 | + issues.forEach((issue) => { |
| 22 | + text += `• ${issue?.reactions?.total_count || 0} 👍 ${issue.title} - <${issue.html_url}|#${issue.number}>, ${formattedDaysAgo(issue.created_at)}\n` |
| 23 | + }) |
| 24 | + |
| 25 | + return text.trim() |
| 26 | +} |
| 27 | + |
| 28 | +export async function run() { |
| 29 | + try { |
| 30 | + if (!process.env.GITHUB_TOKEN) throw new TypeError('GITHUB_TOKEN not set') |
| 31 | + if (!process.env.SLACK_TOKEN) throw new TypeError('SLACK_TOKEN not set') |
| 32 | + |
| 33 | + const octoClient = getOctokit(process.env.GITHUB_TOKEN) |
| 34 | + const slackClient = new WebClient(process.env.SLACK_TOKEN) |
| 35 | + |
| 36 | + const { data } = await octoClient.rest.search.issuesAndPullRequests({ |
| 37 | + order: 'desc', |
| 38 | + per_page: 10, |
| 39 | + q: `repo:payloadcms/payload is:issue is:open created:>=${daysAgo(DAYS_WINDOW)}`, |
| 40 | + sort: 'reactions', |
| 41 | + }) |
| 42 | + |
| 43 | + if (!data.items.length) { |
| 44 | + info(`No popular issues`) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + const messageText = generateText(data.items) |
| 49 | + console.log(messageText) |
| 50 | + |
| 51 | + await slackClient.chat.postMessage({ |
| 52 | + text: messageText, |
| 53 | + channel: process.env.DEBUG === 'true' ? '#test-slack-notifications' : '#dev-feed', |
| 54 | + icon_emoji: ':github:', |
| 55 | + username: 'GitHub Notifier', |
| 56 | + }) |
| 57 | + |
| 58 | + info(`Posted to Slack!`) |
| 59 | + } catch (error) { |
| 60 | + setFailed(error as Error) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +run() |
0 commit comments