Update Vote Counts #1325
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: "*/15 * * * *" | |
workflow_dispatch: # Allow manual triggers | |
# push: | |
# branches: | |
# - main | |
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: | |
REPO_ID: ${{ secrets.REPO_ID }} | |
with: | |
script: | | |
const fs = require('fs').promises; | |
// Get the current year | |
const currentYear = new Date().getFullYear(); | |
// Get all discussions in the Flutter of the Year category | |
const allDiscussions = []; | |
let hasNextPage = true; | |
let cursor = null; | |
while (hasNextPage) { | |
try { | |
const result = await github.graphql(` | |
query($owner: String!, $repo: String!, $cursor: String) { | |
repository(owner: $owner, name: $repo) { | |
discussions(first: 50, after: $cursor) { | |
nodes { | |
id | |
title | |
resourcePath | |
upvoteCount | |
} | |
pageInfo { | |
hasNextPage | |
endCursor | |
} | |
} | |
} | |
} | |
`, { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
cursor: cursor | |
}); | |
console.log('GraphQL Response:', JSON.stringify(result, null, 2)); | |
console.log('Owner:', context.repo.owner); | |
console.log('Repo:', context.repo.repo); | |
if (!result || !result.repository || !result.repository.discussions) { | |
throw new Error('Invalid response structure'); | |
} | |
const { repository: { discussions } } = result; | |
allDiscussions.push(...discussions.nodes); | |
hasNextPage = discussions.pageInfo.hasNextPage; | |
cursor = discussions.pageInfo.endCursor; | |
} catch (error) { | |
console.error('Error:', error); | |
console.error('Context:', { | |
owner: context.repo.owner, | |
repo: context.repo.repo | |
}); | |
throw error; | |
} | |
} | |
// Create votes.json with the current vote counts | |
const votes = {}; | |
for (const discussion of allDiscussions) { | |
if (discussion.title.startsWith('Vote ' + currentYear)) { | |
const appName = discussion.title.split(': ')[1].split(' by ')[0]; | |
votes[appName] = { | |
slug: discussion.resourcePath, | |
votes: discussion.upvoteCount | |
}; | |
} | |
} | |
// Write the votes to a JSON file | |
await fs.writeFile( | |
`src/data/votes_${currentYear}.json`, | |
JSON.stringify(votes, null, 2) | |
); | |
// Check if there are any changes to commit | |
const { exitCode } = await exec.getExecOutput('git', ['status', '--porcelain']); | |
if (exitCode === 0) { | |
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_${currentYear}.json`]); | |
try { | |
await exec.exec('git', ['commit', '-m', `Update vote counts for ${currentYear} - ${date}`]); | |
await exec.exec('git', ['push']); | |
} catch (error) { | |
console.log('No changes to commit'); | |
} | |
} else { | |
console.log('Error checking git status'); | |
} |