-
-
Notifications
You must be signed in to change notification settings - Fork 55
104 lines (92 loc) · 3.72 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 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)
);
// 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_${currentYear}.json`]);
await exec.exec('git', ['commit', '-m', `Update vote counts for ${currentYear} - ${date}`]);
await exec.exec('git', ['push']);