|
1 | 1 | var router = require('express').Router(), |
2 | | - async = require('async'), |
3 | | - Game = require('../../models/game'), |
4 | | - Group = require('../../models/group'), |
5 | | - Release = require('../../models/release'), |
6 | | - response = require('../../helpers/response'), |
7 | | - log = require('../../helpers/logger'); |
| 2 | + async = require('async'), |
| 3 | + Game = require('../../models/game'), |
| 4 | + Group = require('../../models/group'), |
| 5 | + Release = require('../../models/release'), |
| 6 | + response = require('../../helpers/response'), |
| 7 | + log = require('../../helpers/logger'); |
8 | 8 |
|
9 | 9 | router.use(function(req, res, next) { |
10 | | - res.header("Access-Control-Allow-Origin", "*"); |
11 | | - res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); |
12 | | - next(); |
| 10 | + res.header('Access-Control-Allow-Origin', '*'); |
| 11 | + res.header( |
| 12 | + 'Access-Control-Allow-Headers', |
| 13 | + 'Origin, X-Requested-With, Content-Type, Accept' |
| 14 | + ); |
| 15 | + next(); |
13 | 16 | }); |
14 | 17 |
|
15 | | -router.post('/:slug', function(req, res) |
16 | | -{ |
17 | | - req.checkBody('status', 'Status must be one of: "dev", "qa", "stage", "prod"').isStatus(); |
18 | | - req.checkBody('commitId', 'Commit ID must be a valid Git commit has').isCommit(); |
19 | | - req.checkBody('token', 'Token is required').isToken(); |
20 | | - req.checkBody('branch', 'Branch is required and must be a string').isBranch(); |
21 | | - |
22 | | - if (req.body.verison) |
23 | | - req.checkBody('version', 'Not a properly formatted Semantic Version').isSemver(); |
| 18 | +router.post('/:slug', function(req, res) { |
| 19 | + req |
| 20 | + .checkBody('status', 'Status must be one of: "dev", "qa", "stage", "prod"') |
| 21 | + .isStatus(); |
| 22 | + req |
| 23 | + .checkBody('commitId', 'Commit ID must be a valid Git commit has') |
| 24 | + .isCommit(); |
| 25 | + req.checkBody('token', 'Token is required').isToken(); |
| 26 | + req.checkBody('branch', 'Branch is required and must be a string').isBranch(); |
24 | 27 |
|
25 | | - var errors = req.validationErrors(); |
| 28 | + if (req.body.verison) |
| 29 | + req |
| 30 | + .checkBody('version', 'Not a properly formatted Semantic Version') |
| 31 | + .isSemver(); |
26 | 32 |
|
27 | | - if (errors) |
28 | | - { |
29 | | - log.error("Validation error adding release from token " + req.body.token); |
30 | | - log.error(errors); |
| 33 | + var errors = req.validationErrors(); |
31 | 34 |
|
32 | | - if (req.body.redirect) |
33 | | - { |
34 | | - log.warn('Redirecting to ' + req.body.redirect); |
35 | | - res.redirect(req.body.redirect); |
36 | | - } |
37 | | - else |
38 | | - { |
39 | | - res.send({ |
40 | | - success:false, |
41 | | - data: errors |
42 | | - }); |
43 | | - } |
44 | | - return; |
45 | | - } |
| 35 | + if (errors) { |
| 36 | + log.error('Validation error adding release from token ' + req.body.token); |
| 37 | + log.error(errors); |
46 | 38 |
|
47 | | - async.waterfall([ |
48 | | - function(done) |
49 | | - { |
50 | | - Game.getBySlugOrBundleId(req.params.slug, done).select('-thumbnail'); |
51 | | - }, |
52 | | - function(game, done) |
53 | | - { |
54 | | - game.hasPermission(req.body.token, done); |
55 | | - }, |
56 | | - function(game, done) |
57 | | - { |
58 | | - // Better handling of a unique commitId |
59 | | - Release.getByCommitId(req.body.commitId, function(err, release) |
60 | | - { |
61 | | - if (!!req.body.warnUniqueCommit && !!release) |
62 | | - { |
63 | | - done('The Commit ID is already taken'); |
64 | | - } |
65 | | - else |
66 | | - { |
67 | | - done(err, game, release); |
68 | | - } |
69 | | - }); |
70 | | - }, |
71 | | - function(game, release, done) |
72 | | - { |
73 | | - // If we already have a release |
74 | | - // lets just modify the updated timestamp |
75 | | - // and leave everything else the same |
76 | | - if (release) |
77 | | - { |
78 | | - release.updated = Date.now(); |
79 | | - release.save(function(err) |
80 | | - { |
81 | | - done(err, game); |
82 | | - }); |
83 | | - return; |
84 | | - } |
85 | | - var values = Object.assign({}, values, req.body); |
86 | | - values.game = game._id; |
87 | | - delete values.token; |
88 | | - values.created = values.updated = Date.now(); |
| 39 | + if (req.body.redirect) { |
| 40 | + log.warn('Redirecting to ' + req.body.redirect); |
| 41 | + res.redirect(req.body.redirect); |
| 42 | + } else { |
| 43 | + res.send({ |
| 44 | + success: false, |
| 45 | + data: errors |
| 46 | + }); |
| 47 | + } |
| 48 | + return; |
| 49 | + } |
89 | 50 |
|
90 | | - // If the capabilities aren't set, inherit the |
91 | | - // default game capabilities |
92 | | - if (!values.capabilities) |
93 | | - { |
94 | | - values.capabilities = game.capabilities.toObject(); |
95 | | - } |
96 | | - // Or else update the game defaults |
97 | | - else |
98 | | - { |
99 | | - Object.assign( |
100 | | - game.capabilities, |
101 | | - Object.assign({}, values.capabilities) |
102 | | - ); |
103 | | - game.save(); |
104 | | - } |
105 | | - var newRelease = new Release(values); |
106 | | - newRelease.save(function(err, release) |
107 | | - { |
108 | | - if (err) return done(err, game); |
| 51 | + async.waterfall( |
| 52 | + [ |
| 53 | + function(done) { |
| 54 | + Game.getBySlugOrBundleId(req.params.slug, done).select('-thumbnail'); |
| 55 | + }, |
| 56 | + function(game, done) { |
| 57 | + game.hasPermission(req.body.token, done); |
| 58 | + }, |
| 59 | + function(game, done) { |
| 60 | + // Better handling of a unique commitId |
| 61 | + Release.getByCommitId(req.body.commitId, function(err, release) { |
| 62 | + if (!!req.body.warnUniqueCommit && !!release) { |
| 63 | + done('The Commit ID is already taken'); |
| 64 | + } else { |
| 65 | + done(err, game, release); |
| 66 | + } |
| 67 | + }); |
| 68 | + }, |
| 69 | + async function(game, release, done) { |
| 70 | + // If we already have a release |
| 71 | + // lets just modify the updated timestamp |
| 72 | + // and leave everything else the same |
| 73 | + if (release) { |
| 74 | + release.updated = Date.now(); |
| 75 | + release.save(function(err) { |
| 76 | + done(err, game); |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | + var values = Object.assign({}, values, req.body); |
| 81 | + values.game = game._id; |
| 82 | + delete values.token; |
| 83 | + values.created = values.updated = Date.now(); |
109 | 84 |
|
110 | | - game.releases.push(release._id); |
111 | | - game.updated = Date.now(); |
112 | | - game.save(function(err, result) |
113 | | - { |
114 | | - done(err, game); |
115 | | - }); |
116 | | - }); |
117 | | - }, |
118 | | - function(game, done) |
119 | | - { |
120 | | - Release.getByIdsAndStatus(game.releases, "dev", function(err, releases) |
121 | | - { |
122 | | - var maxDevReleases = CONFIGURATION.maxDevReleases; |
123 | | - if (releases.length > maxDevReleases) |
124 | | - { |
125 | | - let toSave = []; |
126 | | - while (toSave.length < maxDevReleases){ |
127 | | - toSave.push(releases.pop()); |
128 | | - } |
129 | | - releases.forEach(function(release) |
130 | | - { |
131 | | - Release.removeById(release._id, function(){}); |
132 | | - }); |
133 | | - } |
134 | | - done(null, game); |
135 | | - }); |
136 | | - } |
137 | | - ], |
138 | | - function(err, result) |
139 | | - { |
140 | | - if (err) |
141 | | - { |
142 | | - log.error('Unable to add the release for token ' + req.body.token); |
143 | | - log.error(err); |
| 85 | + // If the capabilities aren't set, inherit the |
| 86 | + // default game capabilities |
| 87 | + if (!values.capabilities) { |
| 88 | + values.capabilities = game.capabilities.toObject(); |
| 89 | + } else { |
| 90 | + // Or else update the game defaults |
| 91 | + Object.assign( |
| 92 | + game.capabilities, |
| 93 | + Object.assign({}, values.capabilities) |
| 94 | + ); |
| 95 | + await game.save(); |
| 96 | + } |
| 97 | + var newRelease = new Release(values); |
| 98 | + newRelease.save(function(err, release) { |
| 99 | + if (err) { |
| 100 | + return done(err, game); |
| 101 | + } |
144 | 102 |
|
145 | | - if (req.body.redirect) |
146 | | - { |
147 | | - res.redirect(req.body.redirect); |
148 | | - } |
149 | | - else |
150 | | - { |
151 | | - res.status(500).send({ |
152 | | - success:false, |
153 | | - data: 'Unable to add the release' |
154 | | - }); |
155 | | - } |
156 | | - return; |
157 | | - } |
| 103 | + game.releases.push(release._id); |
| 104 | + game.updated = Date.now(); |
| 105 | + game.save(function(err) { |
| 106 | + done(err, game); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }, |
| 110 | + function(game, done) { |
| 111 | + Release.getByIdsAndStatus(game.releases, 'dev', function( |
| 112 | + err, |
| 113 | + releases |
| 114 | + ) { |
| 115 | + var maxDevReleases = CONFIGURATION.maxDevReleases; |
| 116 | + if (releases.length > maxDevReleases) { |
| 117 | + let toSave = []; |
| 118 | + while (toSave.length < maxDevReleases) { |
| 119 | + toSave.push(releases.pop()); |
| 120 | + } |
| 121 | + releases.forEach(function(release) { |
| 122 | + Release.removeById(release._id, function() {}); |
| 123 | + }); |
| 124 | + } |
| 125 | + done(null, game); |
| 126 | + }); |
| 127 | + } |
| 128 | + ], |
| 129 | + function(err, result) { |
| 130 | + if (err) { |
| 131 | + log.error('Unable to add the release for token ' + req.body.token); |
| 132 | + log.error(err); |
158 | 133 |
|
159 | | - if (req.body.redirect) |
160 | | - { |
161 | | - res.redirect(req.body.redirect); |
162 | | - } |
163 | | - else |
164 | | - { |
165 | | - res.send({ |
166 | | - success: true, |
167 | | - data: result |
168 | | - }); |
169 | | - } |
170 | | - }); |
| 134 | + if (req.body.redirect) { |
| 135 | + res.redirect(req.body.redirect); |
| 136 | + } else { |
| 137 | + res.status(500).send({ |
| 138 | + success: false, |
| 139 | + data: 'Unable to add the release' |
| 140 | + }); |
| 141 | + } |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + if (req.body.redirect) { |
| 146 | + res.redirect(req.body.redirect); |
| 147 | + } else { |
| 148 | + res.send({ |
| 149 | + success: true, |
| 150 | + data: result |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | + ); |
171 | 155 | }); |
172 | 156 |
|
173 | | -router.get('/:slugOrBundleId', function(req, res) |
174 | | -{ |
175 | | - req.checkQuery('token').optional().isToken(); |
176 | | - req.checkQuery('status').optional().isStatus(); |
177 | | - req.checkQuery('commitId').optional().isCommit(); |
178 | | - req.checkQuery('version').optional().isSemver(); |
179 | | - if (req.validationErrors()) |
180 | | - { |
181 | | - return response.call(res, "Invalid arguments"); |
182 | | - } |
183 | | - Release.getByGame( |
184 | | - req.params.slugOrBundleId, |
185 | | - { |
186 | | - version: req.query.version, |
187 | | - commitId: req.query.commitId, |
188 | | - archive: req.query.archive, |
189 | | - status: req.query.status || 'prod', |
190 | | - token: req.query.token, |
191 | | - debug: req.query.debug |
192 | | - }, |
193 | | - response.bind(res) |
194 | | - ); |
| 157 | +router.get('/:slugOrBundleId', function(req, res) { |
| 158 | + req |
| 159 | + .checkQuery('token') |
| 160 | + .optional() |
| 161 | + .isToken(); |
| 162 | + req |
| 163 | + .checkQuery('status') |
| 164 | + .optional() |
| 165 | + .isStatus(); |
| 166 | + req |
| 167 | + .checkQuery('commitId') |
| 168 | + .optional() |
| 169 | + .isCommit(); |
| 170 | + req |
| 171 | + .checkQuery('version') |
| 172 | + .optional() |
| 173 | + .isSemver(); |
| 174 | + if (req.validationErrors()) { |
| 175 | + return response.call(res, 'Invalid arguments'); |
| 176 | + } |
| 177 | + Release.getByGame( |
| 178 | + req.params.slugOrBundleId, |
| 179 | + { |
| 180 | + version: req.query.version, |
| 181 | + commitId: req.query.commitId, |
| 182 | + archive: req.query.archive, |
| 183 | + status: req.query.status || 'prod', |
| 184 | + token: req.query.token, |
| 185 | + debug: req.query.debug |
| 186 | + }, |
| 187 | + response.bind(res) |
| 188 | + ); |
195 | 189 | }); |
196 | 190 |
|
197 | 191 | module.exports = router; |
0 commit comments