diff --git a/api/formatters.js b/api/formatters.js index 6aea13c..b00136f 100644 --- a/api/formatters.js +++ b/api/formatters.js @@ -19,10 +19,18 @@ const getCommitEmoji = (message, stats) => { /** * Format commit stats * @param {Object} stats - Commit stats object - * @returns {string} - Formatted stats string + * @returns {string} - Formatted stats string or empty string if no changes */ -const formatStats = (stats = { additions: 0, deletions: 0 }) => - `+${stats.additions}/-${stats.deletions}` +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 @@ -34,7 +42,7 @@ 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}\`` + return `${emoji} [${commit.id.substring(0, 7)}](${commitUrl}): ${commit.message}${stats ? ' ' + stats : ''}` } /** @@ -45,7 +53,8 @@ const formatCommit = (commit, repoUrl) => { */ const formatMessage = (data, commits) => { const repoUrl = data.repository.html_url || data.repository.url - const repoName = data.repository.full_name + // const repoName = data.repository.full_name + const repoId = data.repository.id const branch = data.ref.split('/').pop() const branchUrl = `${repoUrl}/tree/${branch}` @@ -54,9 +63,11 @@ const formatMessage = (data, commits) => { deletions: acc.deletions + (commit.stats?.deletions || 0) }), { additions: 0, deletions: 0 }) + const stats = formatStats(totalStats) + return [ - `🔄 [${repoName}](${repoUrl}):[${branch}](${branchUrl}) ${commits.length} new commit${commits.length === 1 ? '' : 's'}`, - commits.length > 1 ? `📊 Changes: \`${formatStats(totalStats)}\`` : '', + `🔄 [${repoId}](${repoUrl}):[${branch}](${branchUrl}) ${commits.length} new commit${commits.length === 1 ? '' : 's'}`, + stats && commits.length > 1 ? `📊 ${stats}` : '', commits.map(commit => formatCommit(commit, repoUrl)).join('\n') ].filter(Boolean).join('\n') }