Last active
February 24, 2025 22:28
-
-
Save Sdy603/435cffb59aa4f9defeb4f2e8b980d682 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
exports.handler = async (event) => { | |
console.log('π₯ Received event:', JSON.stringify(event, null, 2)); | |
// Safe Access for Objects | |
const build = event.build || {}; | |
const task = event.task || {}; | |
const repository = event.repository || {}; | |
const user = build.user || {}; | |
// Map Cirrus CI webhook data to DX Pipelines API fields | |
const pipelineData = { | |
pipeline_name: build.changeMessageTitle || 'Unknown Pipeline', | |
pipeline_source: 'CirrusCI', | |
reference_id: build.id || 'N/A', | |
started_at: build.changeTimestamp ? (build.changeTimestamp / 1000).toString() : null, | |
status: mapStatus(build.status) || 'unknown', | |
finished_at: task.status === 'COMPLETED' && task.statusTimestamp | |
? (task.statusTimestamp / 1000).toString() | |
: null, | |
repository: repository.owner && repository.name | |
? `${repository.owner}/${repository.name}` | |
: 'Unknown Repository', | |
commit_sha: build.changeIdInRepo || 'N/A', | |
pr_number: null, // Not available in the provided payload | |
source_url: build.id ? `https://cirrus-ci.com/build/${build.id}` : 'N/A', | |
head_branch: build.branch || 'Unknown Branch', | |
email: 'N/A', // Not available in payload | |
github_username: user.username || 'N/A' | |
}; | |
console.log('π Mapped pipeline data:', JSON.stringify(pipelineData, null, 2)); | |
// π¨ **Exit Early If `finished_at` is Missing** | |
if (!pipelineData.finished_at) { | |
console.warn('β οΈ Skipping processing: `finished_at` is missing.'); | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ message: 'Skipping processing: finished_at is missing' }) | |
}; | |
} | |
// Function to map Cirrus CI status to DX API status | |
function mapStatus(status) { | |
const statusMap = { | |
'EXECUTING': 'running', | |
'COMPLETED': 'success', | |
'FAILED': 'failure', | |
'ABORTED': 'canceled', | |
'TIMED_OUT': 'failure' | |
}; | |
if (!statusMap[status]) { | |
console.warn(`β οΈ Unexpected status received: ${status}`); | |
} | |
return statusMap[status] || 'unknown'; | |
} | |
// Send request to DX Pipelines API | |
try { | |
const response = await axios.post('https://young-test.getdx.net/api/pipelineRuns.sync', pipelineData, { | |
headers: { | |
'Authorization': `Bearer DJjxrusGyNbqCCPRxXcSVmNiEJH2vdL1`, | |
'Content-Type': 'application/json' | |
} | |
}); | |
console.log('β DX API Response:', JSON.stringify(response.data, null, 2)); | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ message: 'Request sent successfully', data: response.data }) | |
}; | |
} catch (error) { | |
console.error('β Error sending request to DX API:', error.message); | |
console.error('π Error Details:', error.response ? JSON.stringify(error.response.data, null, 2) : 'No response data'); | |
return { | |
statusCode: 500, | |
body: JSON.stringify({ | |
message: 'Error sending request', | |
error: error.message, | |
details: error.response ? error.response.data : null | |
}) | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment