Skip to content

Commit d319733

Browse files
committed
Added sqlite to track owners of plugins.
1 parent 4acaa05 commit d319733

File tree

7 files changed

+178
-15
lines changed

7 files changed

+178
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
config.json
2+
plugins.db
23
node_modules

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"dependencies": {
1313
"mkdirp": "0.2.1",
1414
"mysql": "0.9.5",
15-
"semver": "1.0.12"
15+
"semver": "1.0.12",
16+
"sqlite3": "2.1.1"
1617
},
1718
"engines": {
1819
"node": "0.6.5"

src/config-sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"repoDir": "/tmp/plugin-repos",
3-
"pluginsDir": "/tmp/plugin-data",
3+
"pluginsDb": "plugins.db",
44
"dbHost": "localhost",
55
"dbPort": 3306,
66
"dbName": "mysql",

src/main.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
var exec = require( "child_process" ).exec,
22
fs = require( "fs" ),
33
mkdirp = require( "mkdirp" ),
4-
template = require( "./template" ),
54
semver = require( "semver" ),
6-
wordpress = require( "./wordpress" )
5+
template = require( "./template" ),
6+
wordpress = require( "./wordpress" ),
7+
pluginsDb = require( "./pluginsdb" ),
78
config = require( "./config" );
89

910

@@ -234,8 +235,6 @@ function validatePackageJson( package, version ) {
234235
errors.push( "Missing required dependency: jquery." );
235236
}
236237

237-
// TODO: validate repo (must match actual GitHub repo)
238-
239238
return errors;
240239
}
241240

@@ -284,11 +283,12 @@ function processPlugin( repo, fn ) {
284283
}
285284

286285
if ( !versions.length ) {
287-
return fn( createError( "No semver tags.", "NO_SEMVER_TAGS" ) );
286+
return fn( null );
288287
}
289288

290-
// TODO: add plugin to database
291289
var allErrors = [],
290+
// TODO: track our actions so we can process metadata in done()
291+
plugins = {},
292292
waiting = versions.length;
293293

294294
function progress() {
@@ -314,26 +314,43 @@ function processPlugin( repo, fn ) {
314314
return progress();
315315
}
316316

317+
var package = data.package;
318+
317319
if ( data.errors.length ) {
318320
allErrors.concat( data.errors );
319321
return progress();
320322
}
321323

322-
// TODO: verify user is owner of plugin
323-
324-
data.package._downloadUrl = repoDetails.downloadUrl( version );
325-
_generatePage( data.package, function( error, data ) {
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 ) {
326327
if ( error ) {
327328
// TODO: log failure for retry
328329
return progress();
329330
}
330331

331-
wordpress.addVersionedPlugin( data, function( error ) {
332+
// the plugin is owned by someone else
333+
if ( owner !== repoDetails.userName ) {
334+
return progress();
335+
}
336+
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 ) {
332342
if ( error ) {
333343
// TODO: log failure for retry
344+
return progress();
334345
}
335-
console.log( "Added " + data.pluginName + " " + data.version );
336-
progress();
346+
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+
});
337354
});
338355
});
339356
});

src/pluginsdb.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var sqlite = require( "sqlite3" ),
2+
config = require( "./config" );
3+
4+
var db;
5+
6+
function connect( fn ) {
7+
db = new sqlite.Database( config.pluginsDb, fn );
8+
}
9+
10+
function auto( fn ) {
11+
return function() {
12+
var that = this,
13+
args = arguments;
14+
15+
if ( db ) {
16+
return fn.apply( that, args );
17+
}
18+
19+
connect(function( error ) {
20+
if ( error ) {
21+
return fn( error );
22+
}
23+
24+
fn.apply( that, args );
25+
});
26+
};
27+
}
28+
29+
var pluginsDb = module.exports = {
30+
getOwner: auto(function( plugin, fn ) {
31+
db.get( "SELECT owner FROM owners WHERE plugin = ?",
32+
[ plugin ], function( error, row ) {
33+
if ( error ) {
34+
return fn( error );
35+
}
36+
37+
if ( !row ) {
38+
return fn( null, null );
39+
}
40+
41+
return fn( null, row.owner );
42+
});
43+
}),
44+
45+
setOwner: auto(function( plugin, owner, fn ) {
46+
db.run( "INSERT INTO owners( plugin, owner ) VALUES( ?, ? )",
47+
[ plugin, owner ], function( error ) {
48+
if ( error ) {
49+
return fn( error );
50+
}
51+
52+
fn( null );
53+
});
54+
}),
55+
56+
getOrSetOwner: auto(function( plugin, owner, fn ) {
57+
pluginsDb.setOwner( plugin, owner, function( error ) {
58+
// successfully set owner (new plugin)
59+
if ( !error ) {
60+
return fn( null, owner );
61+
}
62+
63+
// there is already an owner
64+
if ( error.code === "SQLITE_CONSTRAINT" ) {
65+
return pluginsDb.getOwner( plugin, fn );
66+
}
67+
68+
fn( error );
69+
});
70+
})
71+
};

src/setup.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var fs = require( "fs" ),
2+
sqlite = require( "sqlite3" ),
3+
config = require( "./config" ),
4+
success = true;
5+
6+
try {
7+
fs.unlinkSync( config.pluginsDb );
8+
} catch( error ) {
9+
if ( error.code !== "ENOENT" ) {
10+
return logError( "Could not check status of, or delete, plugin.db " );
11+
}
12+
}
13+
14+
var db = new sqlite.Database( config.pluginsDb, function( error ) {
15+
if ( error ) {
16+
return logError( error, "Could not open database." );
17+
}
18+
19+
setup();
20+
});
21+
22+
function setup() {
23+
createOwnersTable(function( error ) {
24+
if ( error ) {
25+
return logError( error, "Could not create owners table." );
26+
}
27+
28+
console.log( "Created owners table." );
29+
});
30+
31+
createActionsTable(function( error ) {
32+
if ( error ) {
33+
return logError( error, "Could not create actions table." );
34+
}
35+
36+
console.log( "Created actions table." );
37+
});
38+
}
39+
40+
function logError( error, msg ) {
41+
console.log( "ERROR!", msg );
42+
console.log( error );
43+
success = false;
44+
}
45+
46+
function createOwnersTable( fn ) {
47+
db.run( "CREATE TABLE owners (" +
48+
"plugin TEXT PRIMARY KEY, " +
49+
"owner TEXT " +
50+
");", fn );
51+
}
52+
53+
function createActionsTable( fn ) {
54+
db.run( "CREATE TABLE actions (" +
55+
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
56+
"action TEXT, " +
57+
"data TEXT " +
58+
");", fn );
59+
}
60+
61+
process.on( "exit", function() {
62+
if ( success ) {
63+
console.log( "SUCCESS!", "Setup complete." );
64+
} else {
65+
console.log( "ERROR!", "Setup failed." );
66+
}
67+
});

src/wordpress.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ function getMeta( plugin, key, fn ) {
110110
return fn( error );
111111
}
112112

113+
if ( !rows.length ) {
114+
return fn( null, null );
115+
}
116+
113117
fn( null, rows[ 0 ].meta_value );
114118
});
115119
});
@@ -132,10 +136,12 @@ module.exports = {
132136
data.pluginTitle, data.content, fn );
133137
}),
134138

139+
// TODO: check if this is needed in the end
135140
getOwner: auto(function( plugin, fn ) {
136141
getMeta( plugin, "owner", fn );
137142
}),
138143

144+
// TODO: check if this is needed in the end
139145
setOwner: auto(function( plugin, owner, fn ) {
140146
createOrUpdateMeta( plugin, "owner", owner, fn );
141147
}),

0 commit comments

Comments
 (0)