Skip to content

Commit b955bfa

Browse files
Update generate-profiles.yml
1 parent 6454961 commit b955bfa

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

.github/workflows/generate-profiles.yml

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ on:
77
- cron: '0 0 * * *'
88

99
permissions:
10-
contents: write # Required for pushing changes
11-
pull-requests: write # Required for creating pull requests
12-
packages: read # Required for reading package information
10+
contents: write
11+
pull-requests: write
12+
packages: read
1313
actions: read
1414

1515
jobs:
@@ -20,7 +20,7 @@ jobs:
2020
uses: actions/checkout@v4
2121
with:
2222
token: ${{ secrets.IMAGE_TOKEN || github.token }}
23-
fetch-depth: 0 # Fetch all history for all branches and tags
23+
fetch-depth: 0
2424

2525
- name: Setup Node.js
2626
uses: actions/setup-node@v4
@@ -34,34 +34,50 @@ jobs:
3434
- name: Read user mappings
3535
id: user-mappings
3636
run: |
37-
# Read the user-mapping.json file
3837
MAPPINGS=$(jq -r 'to_entries | map("\(.key)=\(.value)") | join(" ")' data/user-mapping.json)
3938
echo "mappings=${MAPPINGS}" >> $GITHUB_OUTPUT
4039
40+
- name: Configure Git
41+
run: |
42+
git config --global user.name "github-actions[bot]"
43+
git config --global user.email "[email protected]"
44+
git config --global --add safe.directory /github/workspace
45+
46+
- name: Stage any existing changes
47+
run: |
48+
git config --global --add safe.directory /github/workspace
49+
git add .
50+
51+
- name: Commit any existing changes
52+
run: |
53+
git diff-index --quiet HEAD || git commit -m "chore: update generated files [skip ci]"
54+
55+
- name: Pull latest changes
56+
run: git pull origin main --rebase
57+
4158
- name: Generate profile images
4259
env:
4360
GITHUB_REPOSITORY: ${{ github.repository }}
4461
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}
4562
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN || secrets.IMAGE_TOKEN }}
4663
run: |
47-
# Install required dependencies
4864
npm install node-fetch@2
4965
50-
# Create the generation script
5166
cat > generate-images.js << 'EOL'
5267
const fs = require('fs');
5368
const fetch = require('node-fetch');
5469
const { execSync } = require('child_process');
5570
56-
// Get the API URL from environment variables
5771
const API_URL = process.env.API_URL || 'http://localhost:3000';
5872
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
73+
const REPO_OWNER = 'Open-Dev-Society';
74+
const REPO_NAME = 'openreadme';
5975
6076
async function generateProfileImage(username, userId) {
6177
try {
6278
console.log(`Generating image for ${username}...`);
6379
64-
// First, get the user data from GitHub API
80+
// Get user data from GitHub API
6581
const userResponse = await fetch(`https://api.github.com/users/${username}`, {
6682
headers: {
6783
'Authorization': `token ${GITHUB_TOKEN}`,
@@ -75,26 +91,28 @@ jobs:
7591
7692
const userData = await userResponse.json();
7793
78-
// Prepare the data for the OpenReadme API
94+
// Build API URL with repository parameters
7995
const params = new URLSearchParams({
8096
n: userData.name || username,
8197
i: userData.avatar_url || '',
8298
github: username,
8399
x: userData.twitter_username || '',
84100
l: userData.blog || userData.html_url,
85101
p: userData.html_url,
86-
t: 'classic' // Default theme
102+
t: 'classic',
103+
repo: `${REPO_OWNER}/${REPO_NAME}`,
104+
path: 'stats/usage-log.json'
87105
});
88106
89107
const apiUrl = `${API_URL}?${params.toString()}`;
90108
console.log(`Calling API: ${apiUrl}`);
91109
92-
// Call the OpenReadme API with proper headers
93110
const response = await fetch(apiUrl, {
94111
method: 'GET',
95112
headers: {
96113
'Accept': 'application/json',
97-
'Content-Type': 'application/json'
114+
'Content-Type': 'application/json',
115+
'Authorization': `token ${GITHUB_TOKEN}`
98116
}
99117
});
100118
@@ -107,7 +125,6 @@ jobs:
107125
108126
const result = await response.json();
109127
console.log(`✅ Successfully generated image for ${username}: ${result.url}`);
110-
111128
return result.url;
112129
113130
} catch (error) {
@@ -123,7 +140,6 @@ jobs:
123140
124141
for (const mapping of mappings) {
125142
if (!mapping) continue;
126-
127143
const [username, userId] = mapping.split('=');
128144
if (!username || !userId) continue;
129145
@@ -132,38 +148,28 @@ jobs:
132148
try {
133149
const imageUrl = await generateProfileImage(username, userId);
134150
if (imageUrl) {
135-
// Here you could update the user-mapping.json with the new image URL if needed
136151
console.log(`Image URL: ${imageUrl}`);
137152
}
138-
139-
// Add delay to avoid rate limiting (1 second between requests)
140153
await new Promise(resolve => setTimeout(resolve, 1000));
141-
142154
} catch (error) {
143155
console.error(`Error processing ${username}:`, error);
144156
}
145157
}
146-
147158
console.log('\n--- All users processed ---');
148159
})();
149160
EOL
150161
151-
# Run the generation script
152162
MAPPINGS="${{ steps.user-mappings.outputs.mappings }}" \
153163
API_URL="https://openreadme.vercel.app/api/openreadme" \
154164
node generate-images.js
155165
156-
- name: Pull latest changes
157-
run: git pull origin main --rebase
158166
- name: Commit and push changes
159167
if: github.event_name == 'workflow_dispatch' || github.ref == 'refs/heads/main'
160168
env:
161169
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN || secrets.IMAGE_TOKEN }}
162170
run: |
163-
git config --global user.name "github-actions[bot]"
164-
git config --global user.email "[email protected]"
165-
git config --global --add safe.directory /github/workspace
166171
git add .
167-
git diff --quiet && git diff --staged --quiet || \
168-
(git commit -m "chore: update profile images [skip ci]" && \
169-
git push https://${{ github.actor }}:[email protected]/${{ github.repository }}.git HEAD:main)
172+
if ! git diff-index --quiet HEAD --; then
173+
git commit -m "chore: update profile images [skip ci]" && \
174+
git push https://${{ github.actor }}:[email protected]/${{ github.repository }}.git HEAD:main
175+
fi

0 commit comments

Comments
 (0)