ai-work-report-generator
Version:
Generate professional work reports from git commits using AI (OpenAI, Claude, Gemini, OpenRouter). Includes Slack integration.
127 lines (111 loc) • 2.71 kB
JavaScript
import { IncomingWebhook } from '@slack/webhook';
import chalk from 'chalk';
export async function sendToSlack(report, webhookUrl, options = {}) {
try {
const webhook = new IncomingWebhook(webhookUrl);
const blocks = formatReportForSlack(report, options);
await webhook.send({
text: 'Work Report Generated',
blocks: blocks,
username: options.username || 'Git Work Reporter',
icon_emoji: options.icon || ':chart_with_upwards_trend:'
});
return true;
} catch (error) {
throw new Error(`Failed to send to Slack: ${error.message}`);
}
}
function formatReportForSlack(report, options = {}) {
const date = new Date().toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
const blocks = [
{
type: 'header',
text: {
type: 'plain_text',
text: '📊 Work Report',
emoji: true
}
},
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `*Date:* ${date}`
}
]
},
{
type: 'divider'
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: formatReportContent(report)
}
}
];
if (options.author) {
blocks.push({
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `*Author:* ${options.author}`
}
]
});
}
if (options.repoName) {
blocks.push({
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `*Repository:* ${options.repoName}`
}
]
});
}
blocks.push({
type: 'divider'
});
blocks.push({
type: 'context',
elements: [
{
type: 'mrkdwn',
text: '_Generated by Git Work Reporter_'
}
]
});
return blocks;
}
function formatReportContent(report) {
// Convert markdown to Slack mrkdwn format
let formatted = report
.replace(/^#+ (.+)$/gm, '*$1*') // Headers to bold
.replace(/^\* /gm, '• ') // Asterisk bullets to bullet points
.replace(/^- /gm, '• ') // Dash bullets to bullet points
.replace(/\*\*(.+?)\*\*/g, '*$1*') // Bold text
.replace(/`(.+?)`/g, '`$1`'); // Keep inline code
// Limit length for Slack
if (formatted.length > 3000) {
formatted = formatted.substring(0, 2997) + '...';
}
return formatted;
}
export function validateWebhookUrl(url) {
if (!url) {
return false;
}
// Basic validation for Slack webhook URL format
const webhookPattern = /^https:\/\/hooks\.slack\.com\/services\/.+/;
return webhookPattern.test(url);
}