Skip to content

Commit d6561e7

Browse files
committed
Partial tracking of processed versions.
1 parent d319733 commit d6561e7

File tree

3 files changed

+95
-24
lines changed

3 files changed

+95
-24
lines changed

src/main.js

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,8 @@ function processPlugin( repo, fn ) {
286286
return fn( null );
287287
}
288288

289-
var allErrors = [],
290-
// TODO: track our actions so we can process metadata in done()
291-
plugins = {},
289+
// TODO: track our actions so we can process metadata in done()
290+
var plugins = {},
292291
waiting = versions.length;
293292

294293
function progress() {
@@ -299,6 +298,7 @@ function processPlugin( repo, fn ) {
299298
}
300299

301300
function done() {
301+
// TODO: track invalid versions (user error, not system error)
302302
// TODO: update versionless post to have latest version
303303
// TODO: update metadata in WP
304304
// - don't list pre-release versions that are older than latest stable
@@ -314,43 +314,78 @@ function processPlugin( repo, fn ) {
314314
return progress();
315315
}
316316

317-
var package = data.package;
318-
319317
if ( data.errors.length ) {
320-
allErrors.concat( data.errors );
318+
// TODO: report errors to user
321319
return progress();
322320
}
323321

324-
// find out who owns this plugin
325-
// if there is no owner, then set the user as the owner
326-
pluginsDb.getOrSetOwner( package.name, repoDetails.userName, function( error, owner ) {
322+
wordpress.getVersions( data.package.name, function( error, versions ) {
327323
if ( error ) {
328324
// TODO: log failure for retry
329325
return progress();
330326
}
331327

332-
// the plugin is owned by someone else
333-
if ( owner !== repoDetails.userName ) {
328+
// this version has already been processed
329+
// TODO: when should we start passing around the clean version?
330+
if ( versions.indexOf( semver.clean( version ) ) !== -1 ) {
334331
return progress();
335332
}
336333

337-
// TODO: track action in sqlite
338-
339-
// add additional metadata and generate the plugin page
340-
package._downloadUrl = repoDetails.downloadUrl( version );
341-
_generatePage( package, function( error, data ) {
334+
_addPluginVersion( version, data.package, function( error ) {
342335
if ( error ) {
343-
// TODO: log failure for retry
344336
return progress();
345337
}
346338

347-
wordpress.addVersionedPlugin( data, function( error ) {
348-
if ( error ) {
349-
// TODO: log failure for retry
350-
}
351-
console.log( "Added " + data.pluginName + " " + data.version );
352-
progress();
353-
});
339+
var name = data.package.name;
340+
if ( !plugins[ name ] ) {
341+
plugins[ name ] = [];
342+
}
343+
plugins[ name ].push( semver.clean( version ) );
344+
progress();
345+
});
346+
});
347+
});
348+
});
349+
}
350+
351+
function _addPluginVersion( version, package, fn ) {
352+
// find out who owns this plugin
353+
// if there is no owner, then set the user as the owner
354+
pluginsDb.getOrSetOwner( package.name, repoDetails.userName, function( error, owner ) {
355+
if ( error ) {
356+
// TODO: log failure for retry
357+
return fn( error );
358+
}
359+
360+
// the plugin is owned by someone else
361+
if ( owner !== repoDetails.userName ) {
362+
// TODO: report error to user
363+
return fn( createError( "Plugin owned by someone else.", "NOT_OWNER", {
364+
owner: owner
365+
}));
366+
}
367+
368+
pluginsDb.addVersion( repoDetails, package, function( error ) {
369+
if ( error ) {
370+
// TODO: log failure for retry
371+
return fn( error );
372+
}
373+
374+
// add additional metadata and generate the plugin page
375+
package._downloadUrl = repoDetails.downloadUrl( version );
376+
_generatePage( package, function( error, data ) {
377+
if ( error ) {
378+
// TODO: log failure for retry
379+
return fn( error );
380+
}
381+
382+
wordpress.addVersionedPlugin( data, function( error ) {
383+
if ( error ) {
384+
// TODO: log failure for retry
385+
return fn( error );
386+
}
387+
console.log( "Added " + data.pluginName + " " + data.version );
388+
fn();
354389
});
355390
});
356391
});

src/pluginsdb.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,26 @@ var pluginsDb = module.exports = {
6767

6868
fn( error );
6969
});
70+
}),
71+
72+
addVersion: auto(function( repoDetails, package, fn ) {
73+
// TODO: verify what data we need to store (build restore functionality to confirm)
74+
var data = JSON.stringify({
75+
git: repoDetails.git,
76+
package: package
77+
});
78+
// TODO: should we track timestamps so we can replay actions since a specific date?
79+
db.run( "INSERT INTO actions( action, data ) VALUES( ?, ? )",
80+
[ "addVersion", data ], function( error ) {
81+
if ( error ) {
82+
return fn( error );
83+
}
84+
85+
fn( null );
86+
});
87+
}),
88+
89+
getAllActions: auto(function( fn ) {
90+
db.all( "SELECT * FROM actions", fn );
7091
})
7192
};

src/wordpress.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ module.exports = {
146146
createOrUpdateMeta( plugin, "owner", owner, fn );
147147
}),
148148

149+
// TODO: optimize with a cache (must be cleared when setting the new versions)
150+
getVersions: auto(function( plugin, fn ) {
151+
getMeta( plugin, "versions", function( error, versions ) {
152+
if ( error ) {
153+
return fn( error );
154+
}
155+
156+
if ( !versions ) {
157+
return fn( null, [] );
158+
}
159+
160+
return fn( null, JSON.parse( versions ) );
161+
});
162+
}),
163+
149164
end: function() {
150165
if ( db ) {
151166
db.end();

0 commit comments

Comments
 (0)