-
Notifications
You must be signed in to change notification settings - Fork 0
/
createCourseplayRelease.js
96 lines (78 loc) · 3.04 KB
/
createCourseplayRelease.js
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
// Webhook for the Courseplay GitHub repository.
//
// On each commit create a deployable Courseplay zip file and
// upload it to GitHub as a release.
//
const secret = process.env.SECRET
let privateKey = process.env.PRIVATE_KEY
const fs = require('fs')
try {
privateKey = fs.readFileSync('courseplay-release-creator-private-key.pem')
console.log('Read private key from file.')
} catch (e) {
console.log('No private key file could be read')
}
const {App} = require('@octokit/app')
const {createNodeMiddleware} = require('@octokit/webhooks')
const app = new App({appId: "115455", privateKey: privateKey, webhooks: {secret}})
const http = require('http');
const execSync = require('child_process').execSync;
async function createRelease(octokit, tag, name, description, sha, repo, filename) {
let result = await octokit.request('POST /repos/Courseplay/' + repo + '/releases', {
tag_name: tag,
// draft : true,
prerelease: false,
name: name,
body: description,
target_commitish: sha
});
const stats = fs.statSync('/tmp/' + filename);
const zipFile = fs.readFileSync('/tmp/' + filename)
result = await octokit.request('POST ' + result.data.upload_url, {
headers: {
'content-length' : stats.size,
'content-type' : 'application/zip'
},
data: zipFile,
name: filename
})
console.log(result)
}
app.webhooks.on('push', ({octokit, payload}) => {
let branch = payload.ref.replace('refs/heads/', '')
console.log('Push event for ' + payload.ref);
let zipFileName, releasePrefix;
if (payload.repository.name === 'courseplay') {
zipFileName = 'FS19_Courseplay.zip'
releasePrefix = 'Courseplay for FS19 v'
} else if (payload.repository.name === 'Courseplay_FS22') {
zipFileName = 'FS22_Courseplay.zip'
releasePrefix = 'Courseplay for FS22 v'
} else if (payload.repository.name === 'Courseplay_FS25') {
zipFileName = 'FS25_Courseplay.zip'
releasePrefix = 'Courseplay for FS25 v'
} else {
console.log('Not creating release for ' + payload.repository.name)
return
}
if (branch !== 'master' && branch !== 'main' && branch !== 'ModHubFS19') {
console.log('Not creating release from ' + payload.ref + ', only from master, main and ModHubFS19.')
return
}
execSync(__dirname + '/packCourseplay ' + branch + ' ' + __dirname + '/exclude.lst ' + zipFileName + ' ' + payload.repository.name);
fs.readFile('/tmp/courseplay_version', (err, data) => {
if (err) throw err;
const courseplayVersion = data.toString().replace(/^\s+|\s+$/g, ''); //remove newlines
console.log('Creating release ' + courseplayVersion + ' at ' + payload.repository.name);
createRelease(octokit, courseplayVersion, releasePrefix + courseplayVersion,
payload.head_commit.message, payload.head_commit.id, payload.repository.name, zipFileName)
.then(() => {
console.log('Release created')
})
.catch(e => {
// most likely the release already exists, this was just a subsequent push to master
console.log('Release not created, error: ' + e);
});
})
})
http.createServer(createNodeMiddleware(app.webhooks)).listen(8080);