Skip to content

Commit ce75c33

Browse files
committed
api: create updateTopContributors and add cron job to GitHub Actions
1 parent 26e7a1d commit ce75c33

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Update Top Contributors
2+
3+
on:
4+
schedule:
5+
- cron: '5 0 * * *' # Runs at 00:05 UTC every day
6+
workflow_dispatch: # Allows manual trigger
7+
8+
jobs:
9+
update-contributors:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Update Contributors
14+
run: |
15+
curl -X POST ${{ secrets.VERCEL_DEPLOYMENT_URL }}/api/updateTopContributors \
16+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
17+
-H "Content-Type: application/json"

pages/api/updateTopContributors.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { fetchTopThreeUsersByPullRequests } from '../../utils/fetchTopThreeUsersByPullRequests';
2+
import { storeTopThreeUsersInDb } from '../../utils/fetchTopUsersFromDb';
3+
4+
export default async function handler(req, res) {
5+
const authHeader = req.headers.authorization;
6+
if (authHeader !== `Bearer ${process.env.GITHUB_TOKEN}`) {
7+
return res.status(401).json({ error: 'Unauthorized' });
8+
}
9+
10+
try {
11+
const data = await fetchTopThreeUsersByPullRequests('fork-commit-merge/fork-commit-merge');
12+
13+
if (data && data.length > 0) {
14+
await storeTopThreeUsersInDb(data);
15+
return res.status(200).json({ success: true, message: 'Contributors updated successfully' });
16+
}
17+
18+
return res.status(400).json({ error: 'No data retrieved' });
19+
} catch (error) {
20+
console.error('Update contributors error:', error);
21+
return res.status(500).json({ error: 'Internal server error' });
22+
}
23+
}

0 commit comments

Comments
 (0)