|
| 1 | +import { context, getOctokit } from '@actions/github' |
| 2 | +import { info, setFailed } from '@actions/core' |
| 3 | +import { WebClient } from '@slack/web-api' |
| 4 | +import { formattedDate, daysAgo } from './lib/utils' |
| 5 | +import { SlimIssue } from './types' |
| 6 | + |
| 7 | +function generateBlocks(issues: SlimIssue[]) { |
| 8 | + const blocks = [ |
| 9 | + { |
| 10 | + type: 'section', |
| 11 | + text: { |
| 12 | + type: 'mrkdwn', |
| 13 | + text: '*A list of the top 15 issues sorted by the most reactions over the last 30 days.', |
| 14 | + }, |
| 15 | + }, |
| 16 | + { |
| 17 | + type: 'divider', |
| 18 | + }, |
| 19 | + ] |
| 20 | + |
| 21 | + let text = '' |
| 22 | + |
| 23 | + issues.forEach((issue, i) => { |
| 24 | + text += `${i + 1}. [<${issue.html_url}|#${issue.number}>, ${ |
| 25 | + issue?.reactions?.total_count || 0 |
| 26 | + } reactions, ${formattedDate(issue.created_at)}]: ${issue.title}\n` |
| 27 | + }) |
| 28 | + |
| 29 | + blocks.push({ |
| 30 | + type: 'section', |
| 31 | + text: { |
| 32 | + type: 'mrkdwn', |
| 33 | + text: text, |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + return blocks |
| 38 | +} |
| 39 | + |
| 40 | +export async function run() { |
| 41 | + try { |
| 42 | + if (!process.env.GITHUB_TOKEN) throw new TypeError('GITHUB_TOKEN not set') |
| 43 | + if (!process.env.SLACK_TOKEN) throw new TypeError('SLACK_TOKEN not set') |
| 44 | + |
| 45 | + const octoClient = getOctokit(process.env.GITHUB_TOKEN) |
| 46 | + const slackClient = new WebClient(process.env.SLACK_TOKEN) |
| 47 | + |
| 48 | + const { owner, repo } = context.repo |
| 49 | + const { data } = await octoClient.rest.search.issuesAndPullRequests({ |
| 50 | + order: 'desc', |
| 51 | + per_page: 15, |
| 52 | + q: `repo:${owner}/${repo} is:issue is:open created:>=${daysAgo(30)}`, |
| 53 | + sort: 'reactions', |
| 54 | + }) |
| 55 | + |
| 56 | + if (!data.items.length) { |
| 57 | + info(`No popular issues`) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + console.log( |
| 62 | + data.items |
| 63 | + .map( |
| 64 | + (i) => |
| 65 | + `#${i.number}: ${i.title}, reactions: ${i.reactions?.total_count} - link: ${i.html_url}`, |
| 66 | + ) |
| 67 | + .join('\n'), |
| 68 | + ) |
| 69 | + |
| 70 | + await slackClient.chat.postMessage({ |
| 71 | + blocks: generateBlocks(data.items), |
| 72 | + channel: '#test-notifications', |
| 73 | + icon_emoji: ':github:', |
| 74 | + username: 'GitHub Notifier', |
| 75 | + }) |
| 76 | + |
| 77 | + info(`Posted to Slack!`) |
| 78 | + } catch (error) { |
| 79 | + setFailed(error as Error) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +run() |
0 commit comments