init
This commit is contained in:
69
api/formatters.js
Normal file
69
api/formatters.js
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* 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('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
|
||||
*/
|
||||
const formatStats = (stats = { additions: 0, deletions: 0 }) =>
|
||||
`+${stats.additions}/-${stats.deletions}`
|
||||
|
||||
/**
|
||||
* 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}\``
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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 })
|
||||
|
||||
return [
|
||||
`🔄 [${repoName}](${repoUrl}):[${branch}](${branchUrl}) ${commits.length} new commit${commits.length === 1 ? '' : 's'}`,
|
||||
commits.length > 1 ? `📊 Changes: \`${formatStats(totalStats)}\`` : '',
|
||||
commits.map(commit => formatCommit(commit, repoUrl)).join('\n')
|
||||
].filter(Boolean).join('\n')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatMessage,
|
||||
formatCommit,
|
||||
formatStats,
|
||||
getCommitEmoji
|
||||
}
|
Reference in New Issue
Block a user