diff --git a/.github/scripts/tom-select-update.js b/.github/scripts/tom-select-update.js new file mode 100644 index 00000000..3ff1e21c --- /dev/null +++ b/.github/scripts/tom-select-update.js @@ -0,0 +1,80 @@ +const fs = require('fs'); +const https = require('https'); +const path = require('path'); + +// --- Utility: fetch URL following redirects --- +function fetchUrl(url) { + return new Promise((resolve, reject) => { + https.get(url, { headers: { 'User-Agent': 'ThunderAI-vendor-updater' } }, (res) => { + if (res.statusCode === 301 || res.statusCode === 302) { + return fetchUrl(res.headers.location).then(resolve).catch(reject); + } + const chunks = []; + res.on('data', chunk => chunks.push(chunk)); + res.on('end', () => resolve({ status: res.statusCode, body: Buffer.concat(chunks) })); + }).on('error', reject); + }); +} + +// --- Read VENDORS.md --- +const vendorsPath = 'VENDORS.md'; +const content = fs.readFileSync(vendorsPath, 'utf8'); + +// --- Find all entries: file + source --- +const entryRegex = /file:\s*(.+?)\r?\nsource:\s*(https?:\/\/.+)/g; +const entries = []; +let match; +while ((match = entryRegex.exec(content)) !== null) { + entries.push({ + file: match[1].trim(), + source: match[2].trim(), + }); +} + +// --- Filter tom-select entries only --- +const filteredEntries = entries.filter(e => e.file.toLowerCase().includes('tom-select')); + +if (filteredEntries.length === 0) { + console.log('No entries found in VENDORS.md'); + process.exit(0); +} + +// --- Main --- +(async () => { + let hasErrors = false; + + for (const entry of filteredEntries) { + const filePath = entry.file.replace(/\\/g, '/'); + console.log(`\nProcessing: ${filePath}`); + console.log(` Source: ${entry.source}`); + + // Create directory if it doesn't exist + const dir = path.dirname(filePath); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + console.log(` → Directory created: ${dir}`); + } + + // Download file + try { + const res = await fetchUrl(entry.source); + if (res.status === 200) { + fs.writeFileSync(filePath, res.body); + console.log(` ✓ File updated (${res.body.length} bytes)`); + } else { + console.error(` ✗ Download failed: HTTP ${res.status}`); + hasErrors = true; + } + } catch (err) { + console.error(` ✗ Error: ${err.message}`); + hasErrors = true; + } + } + + if (hasErrors) { + console.error('\nSome files were not updated.'); + process.exit(1); + } else { + console.log('\nAll files updated successfully.'); + } +})(); \ No newline at end of file diff --git a/.github/workflows/tom-select-update.yml b/.github/workflows/tom-select-update.yml new file mode 100644 index 00000000..7a16a980 --- /dev/null +++ b/.github/workflows/tom-select-update.yml @@ -0,0 +1,81 @@ +name: Update Tom Select Files + +on: + workflow_dispatch: + push: + paths: + - 'VENDORS.md' + +jobs: + update-vendors: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.ref }} + token: ${{ secrets.GITHUB_TOKEN }} + + - uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Check and update vendor files + run: node .github/scripts/update-vendors.js + + - name: Commit and push to PR branch + id: commit + run: | + BASE_BRANCH="${{ github.ref_name }}" + PR_BRANCH="update-vendors-${BASE_BRANCH}" + + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + # If there are no changes, exit without doing anything + if git diff --quiet; then + echo "No changes to vendor files." + echo "has_changes=false" >> $GITHUB_OUTPUT + exit 0 + fi + + # Crea o aggiorna la branch PR + git checkout -B "$PR_BRANCH" + git add -A + git commit -m "chore: update vendor files from VENDORS.md" + git push origin "$PR_BRANCH" --force + + echo "has_changes=true" >> $GITHUB_OUTPUT + echo "pr_branch=$PR_BRANCH" >> $GITHUB_OUTPUT + echo "base_branch=$BASE_BRANCH" >> $GITHUB_OUTPUT + + - name: Create Pull Request via API + if: steps.commit.outputs.has_changes == 'true' + run: | + # Try to create the PR; if it already exists (422) ignore the error + HTTP_CODE=$(curl -s -o /tmp/pr_response.json -w "%{http_code}" \ + -X POST \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/${{ github.repository }}/pulls \ + -d "{ + \"title\": \"chore: update vendor files from VENDORS.md\", + \"body\": \"Vendor files updated automatically after a change to \`VENDORS.md\`.\n\nPlease review the downloaded files before merging.\", + \"head\": \"${{ steps.commit.outputs.pr_branch }}\", + \"base\": \"${{ steps.commit.outputs.base_branch }}\" + }") + + cat /tmp/pr_response.json + + if [ "$HTTP_CODE" = "201" ]; then + echo "PR created successfully." + elif [ "$HTTP_CODE" = "422" ]; then + echo "PR already exists, updated with the new commit." + else + echo "Error creating PR: HTTP $HTTP_CODE" + exit 1 + fi \ No newline at end of file