File tree Expand file tree Collapse file tree 4 files changed +138
-0
lines changed Expand file tree Collapse file tree 4 files changed +138
-0
lines changed Original file line number Diff line number Diff line change @@ -97,6 +97,18 @@ module.exports = {
97
97
}
98
98
} ,
99
99
100
+ fetchRevisions : function ( context ) {
101
+ var redisDeployClient = this . readConfig ( 'redisDeployClient' ) ;
102
+ var keyPrefix = this . readConfig ( 'keyPrefix' ) ;
103
+
104
+ this . log ( 'Listing revisions for key: `' + keyPrefix + '`' ) ;
105
+ return Promise . resolve ( redisDeployClient . fetchRevisions ( keyPrefix ) )
106
+ . then ( function ( revisions ) {
107
+ return { revisions : revisions } ;
108
+ } )
109
+ . catch ( this . _errorMessage . bind ( this ) ) ;
110
+ } ,
111
+
100
112
_readFileContents : function ( path ) {
101
113
return readFile ( path )
102
114
. then ( function ( buffer ) {
Original file line number Diff line number Diff line change @@ -61,6 +61,22 @@ module.exports = CoreObject.extend({
61
61
. then ( this . _activateRevisionKey . bind ( this , currentKey , revisionKey ) ) ;
62
62
} ,
63
63
64
+ fetchRevisions : function ( keyPrefix ) {
65
+ var currentKey = keyPrefix + ':current' ;
66
+
67
+ return Promise . hash ( {
68
+ revisions : this . _listRevisions ( keyPrefix ) ,
69
+ current : this . _client . get ( currentKey )
70
+ } ) . then ( function ( results ) {
71
+ return results . revisions . map ( function ( revision ) {
72
+ return {
73
+ revision : revision ,
74
+ active : revision === results . current
75
+ } ;
76
+ } ) ;
77
+ } ) ;
78
+ } ,
79
+
64
80
_listRevisions : function ( keyPrefix ) {
65
81
var client = this . _client ;
66
82
return client . lrange ( keyPrefix , 0 , this . _maxNumberOfRecentUploads - 1 ) ;
Original file line number Diff line number Diff line change @@ -454,4 +454,50 @@ describe('redis plugin', function() {
454
454
assert . match ( messageOutput , / e m b e r d e p l o y : a c t i v a t e q a - - r e v i s i o n = 1 2 3 a b c / ) ;
455
455
} ) ;
456
456
} ) ;
457
+
458
+ describe ( 'fetchRevisions hook' , function ( ) {
459
+ it ( 'fills the revisions variable on context' , function ( ) {
460
+ var plugin ;
461
+ var context ;
462
+
463
+ plugin = subject . createDeployPlugin ( {
464
+ name : 'redis'
465
+ } ) ;
466
+
467
+ context = {
468
+ ui : mockUi ,
469
+ project : stubProject ,
470
+ config : {
471
+ redis : {
472
+ keyPrefix : 'test-prefix' ,
473
+ filePattern : 'index.html' ,
474
+ distDir : 'tests' ,
475
+ revisionKey : '123abc' ,
476
+ redisDeployClient : function ( context ) {
477
+ return {
478
+ fetchRevisions : function ( keyPrefix , revisionKey ) {
479
+ return Promise . resolve ( [ {
480
+ revision : 'a' ,
481
+ active : false
482
+ } ] ) ;
483
+ }
484
+ } ;
485
+ }
486
+ }
487
+ }
488
+ } ;
489
+ plugin . beforeHook ( context ) ;
490
+ plugin . configure ( context ) ;
491
+
492
+ return assert . isFulfilled ( plugin . fetchRevisions ( context ) )
493
+ . then ( function ( result ) {
494
+ assert . deepEqual ( result , {
495
+ revisions : [ {
496
+ "active" : false ,
497
+ "revision" : "a"
498
+ } ]
499
+ } ) ;
500
+ } ) ;
501
+ } ) ;
502
+ } ) ;
457
503
} ) ;
Original file line number Diff line number Diff line change @@ -178,4 +178,68 @@ describe('redis', function() {
178
178
} ) ;
179
179
} ) ;
180
180
} ) ;
181
+
182
+ describe ( '#fetchRevisions' , function ( ) {
183
+ it ( 'lists the last existing revisions' , function ( ) {
184
+ var recentRevisions = [ 'a' , 'b' , 'c' ] ;
185
+
186
+ var redis = new Redis ( { } , new FakeRedis ( FakeClient . extend ( {
187
+ lrange : function ( ) {
188
+ return recentRevisions ;
189
+ } ,
190
+ get : function ( ) {
191
+ }
192
+ } ) ) ) ;
193
+
194
+ var promise = redis . fetchRevisions ( 'key-prefix' ) ;
195
+ return assert . isFulfilled ( promise )
196
+ . then ( function ( result ) {
197
+ assert . deepEqual ( result , [
198
+ {
199
+ revision : 'a' ,
200
+ active : false
201
+ } ,
202
+ {
203
+ revision : 'b' ,
204
+ active : false
205
+ } ,
206
+ {
207
+ revision : 'c' ,
208
+ active : false
209
+ }
210
+ ]
211
+ ) ;
212
+ } ) ;
213
+ } ) ;
214
+
215
+ it ( 'lists revisions and marks the active one' , function ( ) {
216
+ var recentRevisions = [ 'a' , 'b' ] ;
217
+ var currentRevision = 'b' ;
218
+
219
+ var redis = new Redis ( { } , new FakeRedis ( FakeClient . extend ( {
220
+ lrange : function ( ) {
221
+ return recentRevisions ;
222
+ } ,
223
+ get : function ( ) {
224
+ return currentRevision ;
225
+ }
226
+ } ) ) ) ;
227
+
228
+ var promise = redis . fetchRevisions ( 'key-prefix' ) ;
229
+ return assert . isFulfilled ( promise )
230
+ . then ( function ( result ) {
231
+ assert . deepEqual ( result , [
232
+ {
233
+ revision : 'a' ,
234
+ active : false
235
+ } ,
236
+ {
237
+ revision : 'b' ,
238
+ active : true
239
+ }
240
+ ]
241
+ ) ;
242
+ } ) ;
243
+ } ) ;
244
+ } ) ;
181
245
} ) ;
You can’t perform that action at this time.
0 commit comments