-
-
Notifications
You must be signed in to change notification settings - Fork 55
80 lines (71 loc) · 2.77 KB
/
update-votes.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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:
REPO_ID: ${{ secrets.REPO_ID }}
with:
script: |
const fs = require('fs').promises;
// Get all discussions in the Flutter of the Year category
const allDiscussions = [];
let hasNextPage = true;
let cursor = null;
while (hasNextPage) {
const { data: { repository: { discussions } } } = await github.graphql(`
query($owner: String!, $repo: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
discussions(first: 50, after: $cursor) {
nodes {
title
reactions(content: THUMBS_UP) {
totalCount
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`, {
owner: context.repo.owner,
repo: context.repo.repo,
cursor: cursor
});
allDiscussions.push(...discussions.nodes);
hasNextPage = discussions.pageInfo.hasNextPage;
cursor = discussions.pageInfo.endCursor;
}
// Create votes.json with the current vote counts
const votes = {};
for (const discussion of allDiscussions) {
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']);