This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
forked from max-mapper/electron-prebuilt-updater
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
159 lines (148 loc) · 4.95 KB
/
index.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
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
'use strict'
const GitHubApi = require('github')
const Promise = require('bluebird')
const bodyParser = require('body-parser')
const crypto = require('crypto')
const express = require('express')
const fs = Promise.promisifyAll(require('fs'))
const npm = require('npm')
const path = require('path')
const semver = require('semver')
const app = express()
const github = new GitHubApi({
version: '3.0.0',
headers: {
'User-Agent': 'electron-prebuilt-updater'
}
})
const apiKey = process.env.API_KEY
const email = process.env.EMAIL
const secret = process.env.SECRET
const token = process.env.TOKEN
Promise.longStackTraces()
app.use(bodyParser.json())
app.set('port', (process.env.PORT || 5000))
app.post('/', function (req, res) {
let packageName = req.query.packageName
let owner = req.query.owner
let repo = req.query.repo
let hubSignature = req.headers['x-hub-signature'].replace('sha1=', '')
let signature = crypto.createHmac('sha1', secret)
.update(JSON.stringify(req.body))
.digest('hex')
console.error('post body', JSON.stringify(req.body))
console.error('post headers', JSON.stringify(req.headers))
if (req.body.release && signature === hubSignature) {
let createReleaseAsync = Promise.promisify(github.releases.createRelease)
let getContentAsync = Promise.promisify(github.repos.getContent)
let updateFileAsync = Promise.promisify(github.repos.updateFile)
let newVersion = req.body.release.tag_name.replace('v', '')
let draft = req.body.release.draft
let prerelease = req.body.release.prerelease
let npmrc = path.resolve(process.env.HOME, '.npmrc')
if (draft) {
return res.status(403).send('This service ignores draft releases')
}
github.authenticate({ type: 'oauth', token: token })
getContentAsync({
user: owner,
repo: repo,
path: 'package.json'
})
.catch(function (err) {
console.error('Failed to get remote file: package.json')
throw err
})
.then(function (file) {
let content = JSON.parse(new Buffer(file.content, 'base64').toString())
content.version = newVersion
return updateFileAsync({
user: owner,
repo: repo,
path: 'package.json',
message: `Update to Electron v${newVersion}`,
content: new Buffer(JSON.stringify(content, null, ' '))
.toString('base64'),
sha: file.sha
})
.catch(function (err) {
console.error('Failed to update remote file: package.json')
throw err
})
})
.then(function () {
return fs.statAsync(npmrc)
})
.catch(function (err) {
if (err.code === 'ENOENT') return null
else {
console.error(`Failed to stat file: ${npmrc}`)
throw err
}
})
.then(function (stat) {
if (!stat) {
let content = `_auth=${apiKey}\nemail=${email}`
return fs.writeFileAsync(npmrc, content)
.catch(function (err) {
console.error(`Failed to write file: ${npmrc}`)
throw err
})
}
})
.then(function () {
return createReleaseAsync({
owner: owner,
repo: repo,
tag_name: req.body.release.tag_name,
name: req.body.release.name,
body: `[${newVersion} Release Notes](https://github.com/electron/electron/releases/v${newVersion})`,
prerelease: prerelease
})
.catch(function (err) {
console.error('Failed to create release')
throw err
})
})
.then(function (release) {
var npmConfig = {}
if (prerelease) npmConfig.tag = 'beta'
npm.load(npmConfig, function (err) {
if (err) {
console.error('Failed to load npm')
throw err
}
const publishAsync = Promise.promisify(npm.commands.publish)
const viewAsync = Promise.promisify(npm.commands.view)
return viewAsync([`${packageName}@latest`])
.catch(function (err) {
console.error('Failed to get package info')
throw err
})
.then(function (response) {
const info = response[0]
const lastVersion = info[Object.keys(info)[0]]['dist-tags'].latest
return publishAsync([release.tarball_url])
.catch(function (err) {
console.error('Failed to publish package')
throw err
})
.then(function () {
if (!prerelease && semver.gt(lastVersion, newVersion)) {
const execSync = require('child_process').execSync
execSync(`${__dirname}/node_modules/.bin/npm dist-tags add ${packageName}@${lastVersion} latest`)
}
})
})
})
})
.then(function () {
return res.send(`Update to Electron v${newVersion}`)
})
} else {
return res.status(403).send('This service only responds to release events')
}
})
app.listen(app.get('port'), function () {
console.log('application running on port', app.get('port'))
})