Update Vote Counts #2
This file contains 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
name: Update Vote Counts | |
on: | |
schedule: | |
- cron: '0 */6 * * *' # Run every 6 hours | |
workflow_dispatch: # Allow manual triggers | |
jobs: | |
update-votes: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
discussions: read | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Update Vote Counts | |
uses: actions/github-script@v7 | |
env: | |
CAT_ID: ${{ secrets.CAT_ID }} # Use the same secret as create-discussion | |
with: | |
script: | | |
const fs = require('fs').promises; | |
const path = require('path'); | |
const yaml = require('js-yaml'); | |
// Get all discussions in the Flutter of the Year category | |
const discussions = await github.paginate(github.rest.discussions.listForRepo, { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
category_id: process.env.CAT_ID // Use the environment variable | |
}); | |
// Create votes.json with the current vote counts | |
const votes = {}; | |
for (const discussion of discussions) { | |
if (discussion.title.startsWith('Vote: ')) { | |
const appName = discussion.title.replace('Vote: ', '').split(' by ')[0]; | |
// Count thumbs up reactions | |
const reactions = await github.rest.reactions.listForDiscussion({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
discussion_number: discussion.number | |
}); | |
votes[appName] = reactions.data.filter(r => r.content === '+1').length; | |
} | |
} | |
// Write the votes to a JSON file | |
await fs.writeFile( | |
'src/data/votes.json', | |
JSON.stringify(votes, null, 2) | |
); | |
// Commit and push the changes | |
const date = new Date().toISOString(); | |
await exec.exec('git', ['config', 'user.name', 'github-actions[bot]']); | |
await exec.exec('git', ['config', 'user.email', 'github-actions[bot]@users.noreply.github.com']); | |
await exec.exec('git', ['add', 'src/data/votes.json']); | |
await exec.exec('git', ['commit', '-m', `Update vote counts - ${date}`]); | |
await exec.exec('git', ['push']); |