/** * Format commit message with emoji based on content * @param {string} message - Commit message * @param {Object} stats - Commit stats * @returns {string} - Emoji for the commit */ const getCommitEmoji = (message, stats) => { const msg = message.toLowerCase() if (msg.includes('merge')) return '๐Ÿ“Ž' if (msg.includes('fix')) return '๐Ÿ”ง' if (msg.includes('feat')) return 'โœจ' if (msg.includes('break')) return '๐Ÿ’ฅ' if (msg.includes('docs')) return '๐Ÿ“š' if (msg.includes('test')) return '๐Ÿงช' if (msg.includes('refactor')) return 'โ™ป๏ธ' if (stats.additions > 100 || stats.deletions > 100) return '๐Ÿ”จ' return '๐Ÿ“' } /** * Format commit stats * @param {Object} stats - Commit stats object * @returns {string} - Formatted stats string or empty string if no changes */ const formatStats = (stats = { additions: 0, deletions: 0 }) => { const { additions = 0, deletions = 0 } = stats if (additions === 0 && deletions === 0) return '' const parts = [] if (additions > 0) parts.push(`+${additions}`) if (deletions > 0) parts.push(`-${deletions}`) return parts.length ? `\`${parts.join('/')}\`` : '' } /** * Format commit message for Telegram * @param {Object} commit - Commit object * @param {string} repoUrl - Repository URL * @returns {string} - Formatted commit message */ const formatCommit = (commit, repoUrl) => { const commitUrl = `${repoUrl}/commit/${commit.id}` const emoji = getCommitEmoji(commit.message, commit.stats || {}) const stats = formatStats(commit.stats) return `${emoji} [${commit.id.substring(0, 7)}](${commitUrl}): ${commit.message}${stats ? ' ' + stats : ''}` } /** * Format webhook message for Telegram * @param {Object} data - Webhook payload * @param {Array} commits - Filtered commits * @returns {string} - Formatted message */ const formatMessage = (data, commits) => { const repoUrl = data.repository.html_url || data.repository.url // const repoName = data.repository.full_name const repoId = data.repository.id const branch = data.ref.split('/').pop() const branchUrl = `${repoUrl}/tree/${branch}` const totalStats = commits.reduce((acc, commit) => ({ additions: acc.additions + (commit.stats?.additions || 0), deletions: acc.deletions + (commit.stats?.deletions || 0) }), { additions: 0, deletions: 0 }) const stats = formatStats(totalStats) return [ `๐Ÿ”„ [${repoId}](${repoUrl}):[${branch}](${branchUrl}) ${commits.length} new commit${Array.from(commits.length.toString()).pop() === '1' ? '' : 's'}`, stats && commits.length > 1 ? `๐Ÿ“Š ${stats}` : '', commits.map(commit => formatCommit(commit, repoUrl)).join('\n') ].filter(Boolean).join('\n') } module.exports = { formatMessage, formatCommit, formatStats, getCommitEmoji }