|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fetch from 'node-fetch' |
| 4 | + |
| 5 | +// —— Config from env / GitHub Action inputs —— |
| 6 | +const ORG = process.env.ORG |
| 7 | +const REPO = process.env.REPO |
| 8 | +const GITHUB_TOKEN = process.env.GITHUB_TOKEN |
| 9 | + |
| 10 | +// Debug logging |
| 11 | +console.log('🔍 Environment variables:') |
| 12 | +console.log(` ORG: ${process.env.ORG}`) |
| 13 | +console.log(` REPO: ${process.env.REPO}`) |
| 14 | +console.log(` GITHUB_TOKEN: ${GITHUB_TOKEN ? '[REDACTED]' : '(not provided)'}`) |
| 15 | + |
| 16 | +// Input validation function |
| 17 | +function validateInputs() { |
| 18 | + const errors = [] |
| 19 | + |
| 20 | + // Validate ORG |
| 21 | + if (!ORG) { |
| 22 | + errors.push('ORG is required but not provided') |
| 23 | + } else if (typeof ORG !== 'string' || ORG.trim() === '') { |
| 24 | + errors.push('ORG must be a non-empty string') |
| 25 | + } else if (!/^[a-zA-Z0-9_-]+$/.test(ORG.trim())) { |
| 26 | + errors.push('ORG must be a valid GitHub organization name (alphanumeric, hyphens, underscores only)') |
| 27 | + } |
| 28 | + |
| 29 | + // Validate REPO |
| 30 | + if (!REPO) { |
| 31 | + errors.push('REPO is required but not provided') |
| 32 | + } else if (typeof REPO !== 'string' || REPO.trim() === '') { |
| 33 | + errors.push('REPO must be a non-empty string') |
| 34 | + } else if (!/^[a-zA-Z0-9._-]+$/.test(REPO.trim())) { |
| 35 | + errors.push('REPO must be a valid GitHub repository name (alphanumeric, dots, hyphens, underscores only)') |
| 36 | + } |
| 37 | + |
| 38 | + // Validate GITHUB_TOKEN |
| 39 | + if (!GITHUB_TOKEN) { |
| 40 | + errors.push('GITHUB_TOKEN is required but not provided') |
| 41 | + } else if (typeof GITHUB_TOKEN !== 'string' || GITHUB_TOKEN.trim() === '') { |
| 42 | + errors.push('GITHUB_TOKEN must be a non-empty string') |
| 43 | + } |
| 44 | + |
| 45 | + return errors |
| 46 | +} |
| 47 | + |
| 48 | +// Parse and validate inputs |
| 49 | +const validationErrors = validateInputs() |
| 50 | + |
| 51 | +if (validationErrors.length > 0) { |
| 52 | + console.error('❌ Input validation failed:') |
| 53 | + validationErrors.forEach(error => console.error(` - ${error}`)) |
| 54 | + process.exit(1) |
| 55 | +} |
| 56 | + |
| 57 | +// Parse validated inputs |
| 58 | +const parsedOrg = ORG.trim() |
| 59 | +const parsedRepo = REPO.trim() |
| 60 | +const parsedGithubToken = GITHUB_TOKEN.trim() |
| 61 | + |
| 62 | +console.log('📊 Resolved values:') |
| 63 | +console.log(` ORG: ${parsedOrg}`) |
| 64 | +console.log(` REPO: ${parsedRepo}`) |
| 65 | + |
| 66 | +// Netlify function configuration - hardcoded URL |
| 67 | +const NETLIFY_FUNCTION_URL = 'https://glittering-chebakia-09bd42.netlify.app/.netlify/functions/github-stats-background' |
| 68 | + |
| 69 | +// Helper function to make HTTP requests |
| 70 | +async function makeRequest(url, options = {}) { |
| 71 | + try { |
| 72 | + const response = await fetch(url, { |
| 73 | + headers: { |
| 74 | + 'Content-Type': 'application/json', |
| 75 | + ...options.headers |
| 76 | + }, |
| 77 | + ...options |
| 78 | + }) |
| 79 | + |
| 80 | + if (!response.ok) { |
| 81 | + throw new Error(`HTTP ${response.status}: ${response.statusText}`) |
| 82 | + } |
| 83 | + |
| 84 | + // Check if response has content |
| 85 | + const contentType = response.headers.get('content-type') |
| 86 | + const text = await response.text() |
| 87 | + |
| 88 | + if (!text || text.trim() === '') { |
| 89 | + console.log('⚠️ Empty response received') |
| 90 | + return { success: true, message: 'Empty response' } |
| 91 | + } |
| 92 | + |
| 93 | + // Try to parse as JSON, but handle non-JSON responses gracefully |
| 94 | + try { |
| 95 | + return JSON.parse(text) |
| 96 | + } catch (parseError) { |
| 97 | + console.log(`⚠️ Non-JSON response received: ${text.substring(0, 200)}...`) |
| 98 | + return { success: true, message: 'Non-JSON response', raw: text } |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + console.error(`❌ Request failed: ${error.message}`) |
| 102 | + throw error |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Main function |
| 107 | +async function main() { |
| 108 | + console.log(`🚀 Starting GitHub stats collection for repository: ${parsedOrg}/${parsedRepo}`) |
| 109 | + |
| 110 | + // Trigger the Netlify background function |
| 111 | + console.log('📡 Triggering Netlify background function...') |
| 112 | + |
| 113 | + const functionUrl = new URL(NETLIFY_FUNCTION_URL) |
| 114 | + functionUrl.searchParams.set('org', parsedOrg) |
| 115 | + functionUrl.searchParams.set('repo', parsedRepo) |
| 116 | + functionUrl.searchParams.set('githubToken', parsedGithubToken) |
| 117 | + |
| 118 | + console.log(`🌐 Calling URL: ${functionUrl.toString().replace(parsedGithubToken, '[REDACTED]')}`) |
| 119 | + |
| 120 | + try { |
| 121 | + const functionResponse = await makeRequest(functionUrl.toString(), { |
| 122 | + method: 'GET' |
| 123 | + }) |
| 124 | + |
| 125 | + console.log('✅ Background function triggered successfully') |
| 126 | + console.log(`📊 Response: ${JSON.stringify(functionResponse, null, 2)}`) |
| 127 | + |
| 128 | + // If the response indicates success (even if it's not JSON), we're done |
| 129 | + if (functionResponse.success !== false) { |
| 130 | + console.log('✅ Background function appears to have been triggered successfully') |
| 131 | + console.log('🎉 GitHub stats collection initiated - the background function will handle the rest') |
| 132 | + process.exit(0) |
| 133 | + } else { |
| 134 | + console.error('❌ Background function returned an error') |
| 135 | + process.exit(1) |
| 136 | + } |
| 137 | + } catch (error) { |
| 138 | + console.error('❌ Failed to trigger background function:', error.message) |
| 139 | + process.exit(1) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// Run the main function |
| 144 | +main().catch(error => { |
| 145 | + console.error('❌ Fatal error:', error.message) |
| 146 | + process.exit(1) |
| 147 | +}) |
0 commit comments