gh2tg/api/formatters.js

87 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-02-18 21:10:44 +00:00
/**
* 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()
2025-02-18 22:33:53 +00:00
if (msg.includes('merge') || msg.includes('merging')) return '📎'
2025-02-18 21:10:44 +00:00
if (msg.includes('fix')) return '🔧'
if (msg.includes('feat')) return '✨'
if (msg.includes('break')) return '💥'
2025-02-18 22:33:53 +00:00
if (msg.includes('docs') || msg.includes('documentation')) return '📚'
2025-02-18 21:10:44 +00:00
if (msg.includes('test')) return '🧪'
2025-02-18 22:27:50 +00:00
if (msg.includes('refactor')) return '🏭'
2025-02-18 22:33:53 +00:00
if (msg.includes('chore') || msg.includes('clean')) return '🧹'
if (msg.includes('style') || msg.includes('markup')) return '🎨'
if (msg.includes('perf') ) return '⚡'
2025-02-18 22:27:50 +00:00
if (msg.includes('build')) return '🏗️'
if (msg.includes('revert')) return '🤸'
if (msg.includes('release')) return '🏆'
2025-02-18 22:33:53 +00:00
if (msg.includes('wip') && !msg.includes('swipe')) return '🚧'
2025-02-18 21:10:44 +00:00
if (stats.additions > 100 || stats.deletions > 100) return '🔨'
return '📝'
}
/**
* Format commit stats
* @param {Object} stats - Commit stats object
2025-02-18 21:29:18 +00:00
* @returns {string} - Formatted stats string or empty string if no changes
2025-02-18 21:10:44 +00:00
*/
2025-02-18 21:29:18 +00:00
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('/')}\`` : ''
}
2025-02-18 21:10:44 +00:00
/**
* 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)
2025-02-18 21:29:18 +00:00
return `${emoji} [${commit.id.substring(0, 7)}](${commitUrl}): ${commit.message}${stats ? ' ' + stats : ''}`
2025-02-18 21:10:44 +00:00
}
/**
* 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
2025-02-18 22:37:07 +00:00
const repoName = data.repository.name.split('/').pop()
2025-02-18 21:10:44 +00:00
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 })
2025-02-18 21:29:18 +00:00
const stats = formatStats(totalStats)
2025-02-18 21:10:44 +00:00
return [
2025-02-18 22:37:07 +00:00
`🔄 [${repoName}](${repoUrl}):[${branch}](${branchUrl}) ${commits.length} new commit${Array.from(commits.length.toString()).pop() === '1' ? '' : 's'}`,
2025-02-18 21:29:18 +00:00
stats && commits.length > 1 ? `📊 ${stats}` : '',
2025-02-18 21:10:44 +00:00
commits.map(commit => formatCommit(commit, repoUrl)).join('\n')
].filter(Boolean).join('\n')
}
module.exports = {
formatMessage,
formatCommit,
formatStats,
getCommitEmoji
}