81 lines
2.8 KiB
JavaScript
81 lines
2.8 KiB
JavaScript
/**
|
|
* 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
|
|
}
|