function parseYaml(yamlString) {
const lines = yamlString.split('\n');
const yamlObject = {};
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine) continue; // Skip empty lines
const colonIndex = trimmedLine.indexOf(':');
if (colonIndex === -1) {
// Handle comments or invalid lines
continue;
}
const key = trimmedLine.slice(0, colonIndex).trim();
const value = trimmedLine.slice(colonIndex + 1).trim();
yamlObject[key] = value;
}
return yamlObject;
}
async function fetchYamlFromRepo(token, repoOwner, repoName, filePath) {
const headers = {
Authorization: `Bearer ${token}`
};
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${filePath}`;
try {
const response = await fetch(url, { headers });
const data = await response.json();
const ymlData = Buffer.from(data.content, 'base64').toString('utf-8');
return parseYaml(ymlData)
} catch (error) {
console.error('Error fetching YAML:', error);
throw error;
}
}
exports.endpoint = async function(request, response) {
// Replace with your personal access token, repository owner, repository name, and file path
const token = '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';
const repoOwner = 'sokoobo';
const repoName = 'damzel-parsers';
const filePath = '.github/summary.yaml';
const yamlData = await fetchYamlFromRepo(token, repoOwner, repoName, filePath);
response.setHeader('Content-Type', 'application/json'); // Set appropriate content type
response.end(JSON.stringify({ subject: 'profiles', status: yamlData['total'], color: 'pink' }));
}