Skip to content

Instantly share code, notes, and snippets.

@Sdy603
Created August 29, 2024 12:11
Show Gist options
  • Save Sdy603/435cffb59aa4f9defeb4f2e8b980d682 to your computer and use it in GitHub Desktop.
Save Sdy603/435cffb59aa4f9defeb4f2e8b980d682 to your computer and use it in GitHub Desktop.
const axios = require('axios');
exports.handler = async (event) => {
console.log(event);
console.log('Received event:', JSON.stringify(event, null, 2));
// Log key-value pairs
Object.entries(event).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
// Map Cirrus CI webhook data to the DX Pipelines API fields
const pipelineData = {
pipeline_name: event.build.changeMessageTitle, // Updated to map to changeMessageTitle
pipeline_source: 'CirrusCI',
reference_id: event.build.id, // Updated to use build.id as reference_id
started_at: (event.build.changeTimestamp / 1000).toString(), // Convert milliseconds to seconds
status: mapStatus(event.build.status), // Convert CirrusCI status to API status
finished_at: event.task.status === 'COMPLETED' ? (event.task.statusTimestamp / 1000).toString() : null, // Use task's statusTimestamp if status is COMPLETED
repository: `${event.repository.owner}/${event.repository.name}`, // Concatenate owner and name for repository
commit_sha: event.build.changeIdInRepo, // Mapped to commit SHA
pr_number: null, // Not available in the provided payload
source_url: `https://cirrus-ci.com/build/${event.build.id}`, // Constructed source URL
head_branch: event.build.branch, // Mapped to branch
email: 'N/A', // Not available in payload
github_username: event.build.user.username // Mapped to GitHub username
};
console.log('Mapped pipeline data:', JSON.stringify(pipelineData, null, 2));
// Function to map Cirrus CI status to the API status
function mapStatus(status) {
switch (status) {
case 'EXECUTING':
return 'running';
case 'COMPLETED':
return 'success'; // Adjust as needed for your API's statuses
case 'FAILED':
return 'failure';
default:
return 'unknown';
}
}
// Send a request to the 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);
return {
statusCode: 500,
body: JSON.stringify({ message: 'Error sending request', error: error.message })
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment