47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
const { formatMessage } = require('./formatters')
|
|
|
|
/**
|
|
* Normalize GitHub webhook payload to common format
|
|
* @param {Object} payload - GitHub webhook payload
|
|
* @returns {Object} - Normalized webhook data
|
|
*/
|
|
const normalizeGithubPayload = (payload) => ({
|
|
repository: {
|
|
full_name: payload.repository.full_name,
|
|
html_url: payload.repository.html_url
|
|
},
|
|
ref: payload.ref,
|
|
commits: payload.commits.map(commit => ({
|
|
id: commit.id,
|
|
message: commit.message,
|
|
stats: {
|
|
additions: commit.stats?.additions || 0,
|
|
deletions: commit.stats?.deletions || 0
|
|
}
|
|
}))
|
|
})
|
|
|
|
/**
|
|
* Handle GitHub webhook
|
|
* @param {Object} payload - Webhook payload
|
|
* @param {Set} reportedCommits - Set of reported commit hashes
|
|
* @param {Map} commitTimestamps - Map of commit timestamps
|
|
* @returns {string|null} - Formatted message or null if no new commits
|
|
*/
|
|
const handleGithub = (payload, reportedCommits, commitTimestamps) => {
|
|
const data = normalizeGithubPayload(payload)
|
|
|
|
// Filter new commits
|
|
const newCommits = data.commits.filter(commit => {
|
|
if (reportedCommits.has(commit.id)) return false
|
|
reportedCommits.add(commit.id)
|
|
commitTimestamps.set(commit.id, Date.now())
|
|
return true
|
|
})
|
|
|
|
if (newCommits.length === 0) return null
|
|
|
|
return formatMessage(data, newCommits)
|
|
}
|
|
|
|
module.exports = { handleGithub }
|