-
-
Notifications
You must be signed in to change notification settings - Fork 36
291 lines (255 loc) · 10.9 KB
/
sync-modrinth.yml
File metadata and controls
291 lines (255 loc) · 10.9 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
name: Sync Modrinth releases to GitHub
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
sync-to-github:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Create sync script
run: |
cat > sync-modrinth.js << 'EOF'
const fs = require('fs');
const https = require('https');
const path = require('path');
const { execSync } = require('child_process');
// Constants
const MODRINTH_PROJECT_ID = process.env.MODRINTH_PROJECT_ID;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const GITHUB_REPO = process.env.GITHUB_REPOSITORY;
// Get existing GitHub releases
function getGitHubReleases() {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.github.com',
path: `/repos/${GITHUB_REPO}/releases`,
method: 'GET',
headers: {
'User-Agent': 'ModrinthSync',
'Authorization': `token ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(JSON.parse(data));
} else {
reject(new Error(`Status Code: ${res.statusCode}, Body: ${data}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
}
// Get Modrinth versions
function getModrinthVersions() {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.modrinth.com',
path: `/v2/project/${MODRINTH_PROJECT_ID}/version`,
method: 'GET',
headers: {
'User-Agent': 'ModrinthSync/1.0'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(JSON.parse(data));
} else {
reject(new Error(`Status Code: ${res.statusCode}, Body: ${data}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
}
// Download file from URL
function downloadFile(url, destination) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(destination);
https.get(url, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close();
resolve();
});
}).on('error', (err) => {
fs.unlink(destination);
reject(err);
});
});
}
// Create GitHub release
function createGitHubRelease(version) {
return new Promise(async (resolve, reject) => {
try {
// Download the file first
const fileName = path.basename(version.files[0].url);
const filePath = `./${fileName}`;
console.log(`Downloading file from ${version.files[0].url} to ${filePath}`);
await downloadFile(version.files[0].url, filePath);
// Create tag if it doesn't exist
try {
console.log(`Checking if tag ${version.version_number} exists...`);
execSync(`git ls-remote --tags origin refs/tags/${version.version_number}`);
console.log(`Tag ${version.version_number} exists.`);
} catch (err) {
console.log(`Creating tag ${version.version_number}...`);
execSync(`git tag ${version.version_number}`);
execSync(`git push origin ${version.version_number}`);
console.log(`Tag ${version.version_number} created.`);
}
// Prepare release data
const isPrerelease = version.version_type === 'alpha' || version.version_type === 'beta';
const releaseData = {
tag_name: version.version_number,
name: version.name || `Version ${version.version_number}`,
body: `${version.changelog || 'No changelog provided.'}`,
draft: false,
prerelease: isPrerelease
};
// Create the release
const options = {
hostname: 'api.github.com',
path: `/repos/${GITHUB_REPO}/releases`,
method: 'POST',
headers: {
'User-Agent': 'ModrinthSync',
'Authorization': `token ${GITHUB_TOKEN}`,
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.v3+json'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
const release = JSON.parse(data);
// Upload the asset
uploadAsset(release.upload_url.replace(/{.*}/, ''), filePath, fileName)
.then(() => resolve(release))
.catch(reject);
} else {
reject(new Error(`Status Code: ${res.statusCode}, Body: ${data}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.write(JSON.stringify(releaseData));
req.end();
} catch (error) {
reject(error);
}
});
}
// Upload asset to GitHub release
function uploadAsset(uploadUrl, filePath, fileName) {
return new Promise((resolve, reject) => {
const stats = fs.statSync(filePath);
const fileSize = stats.size;
const fileStream = fs.createReadStream(filePath);
const options = {
hostname: new URL(uploadUrl).hostname,
path: `${new URL(uploadUrl).pathname}?name=${encodeURIComponent(fileName)}`,
method: 'POST',
headers: {
'User-Agent': 'ModrinthSync',
'Authorization': `token ${GITHUB_TOKEN}`,
'Content-Type': 'application/java-archive',
'Content-Length': fileSize,
'Accept': 'application/vnd.github.v3+json'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
fs.unlinkSync(filePath); // Clean up
resolve(JSON.parse(data));
} else {
reject(new Error(`Status Code: ${res.statusCode}, Body: ${data}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
fileStream.pipe(req);
});
}
// Main function
async function syncModrinthToGitHub() {
try {
console.log('Starting Modrinth to GitHub sync...');
// Get existing GitHub releases
const githubReleases = await getGitHubReleases();
const existingTags = githubReleases.map(release => release.tag_name);
console.log(`Found ${existingTags.length} existing GitHub releases.`);
// Get Modrinth versions
const modrinthVersions = await getModrinthVersions();
console.log(`Found ${modrinthVersions.length} versions on Modrinth.`);
// Find versions that don't exist on GitHub and sort by date (oldest first)
const newVersions = modrinthVersions
.filter(version => !existingTags.includes(version.version_number))
.sort((a, b) => new Date(a.date_published) - new Date(b.date_published));
console.log(`Found ${newVersions.length} new versions to sync to GitHub.`);
// Create GitHub releases for new versions
for (const version of newVersions) {
try {
console.log(`Creating GitHub release for version ${version.version_number} (published ${new Date(version.date_published).toISOString()})...`);
await createGitHubRelease(version);
console.log(`Successfully created GitHub release for version ${version.version_number}.`);
} catch (err) {
console.error(`Error creating release for ${version.version_number}:`, err.message);
}
}
console.log('Sync completed.');
} catch (error) {
console.error('Sync failed:', error.message);
process.exit(1);
}
}
// Execute
syncModrinthToGitHub();
EOF
- name: Run sync script
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MODRINTH_PROJECT_ID: "9tQwxSFr"
run: node sync-modrinth.js