Skip to content

Update Vote Counts

Update Vote Counts #4

Workflow file for this run

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:
CATEGORY_ID: ${{ secrets.CAT_ID }}
with:
script: |
const fs = require('fs').promises;
// Get all discussions in the Flutter of the Year category
const { data: { repository: { discussions } } } = await github.graphql(`
query($owner: String!, $repo: String!, $categoryId: String!) {
repository(owner: $owner, name: $repo) {
discussions(first: 100, categoryId: $categoryId) {
nodes {
title
number
reactions(content: THUMBS_UP, first: 100) {
totalCount
}
}
}
}
}
`, {
owner: context.repo.owner,
repo: context.repo.repo,
categoryId: process.env.CATEGORY_ID
});
// Create votes.json with the current vote counts
const votes = {};
for (const discussion of discussions.nodes) {
if (discussion.title.startsWith('Vote: ')) {
const appName = discussion.title.replace('Vote: ', '').split(' by ')[0];
votes[appName] = discussion.reactions.totalCount;
}
}
// 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']);