|
| 1 | +var semver = require( "semver" ), |
| 2 | + Step = require( "step" ), |
| 3 | + UserError = require( "./user-error" ), |
| 4 | + pluginsDb = require( "./pluginsdb" ), |
| 5 | + service = require( "./service" ), |
| 6 | + retry = require( "./retry" ); |
| 7 | + |
| 8 | +function processHook( data, fn ) { |
| 9 | + var repo = service.getRepoByHook( data ); |
| 10 | + |
| 11 | + if ( !repo ) { |
| 12 | + // TODO: log and bail (no retry) |
| 13 | + return fn( new Error( "Could not parse hook." ) ); |
| 14 | + } |
| 15 | + |
| 16 | + Step( |
| 17 | + function() { |
| 18 | + processVersions( repo, this ); |
| 19 | + }, |
| 20 | + |
| 21 | + function( error ) { |
| 22 | + if ( error ) { |
| 23 | + return fn( error ); |
| 24 | + } |
| 25 | + |
| 26 | + processMeta( repo, this ); |
| 27 | + }, |
| 28 | + |
| 29 | + function( error ) { |
| 30 | + fn( error ); |
| 31 | + } |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function processVersions( repo, fn ) { |
| 36 | + Step( |
| 37 | + // get all tags for the repo |
| 38 | + function() { |
| 39 | + pluginsDb.getTags( repo.getId(), this.parallel() ); |
| 40 | + repo.getVersionTags( this.parallel() ); |
| 41 | + }, |
| 42 | + |
| 43 | + // filter to new versions |
| 44 | + function( error, processedTags, tags ) { |
| 45 | + if ( error ) { |
| 46 | + retry.log( "processVersions", repo.getId() ); |
| 47 | + return fn( error ); |
| 48 | + } |
| 49 | + |
| 50 | + return tags.filter(function( tag ) { |
| 51 | + return !(tag in processedTags); |
| 52 | + }); |
| 53 | + }, |
| 54 | + |
| 55 | + // get releases |
| 56 | + function( error, tags ) { |
| 57 | + if ( error ) { |
| 58 | + retry.log( "processVersions", repo.getId() ); |
| 59 | + return fn( error ); |
| 60 | + } |
| 61 | + |
| 62 | + if ( !tags.length ) { |
| 63 | + return fn( null ); |
| 64 | + } |
| 65 | + |
| 66 | + this.parallel()( null, tags ); |
| 67 | + var group = this.group(); |
| 68 | + tags.forEach(function( tag ) { |
| 69 | + repo.getRelease( tag, group() ); |
| 70 | + }); |
| 71 | + }, |
| 72 | + |
| 73 | + // filter to valid releases |
| 74 | + function( error, tags, releases ) { |
| 75 | + if ( error ) { |
| 76 | + retry.log( "processVersions", repo.getId() ); |
| 77 | + return fn( error ); |
| 78 | + } |
| 79 | + |
| 80 | + var releasesCb = this.parallel(), |
| 81 | + invalidGroup = this.group(); |
| 82 | + releasesCb( null, releases.filter(function( release, i ) { |
| 83 | + if ( release ) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + // TODO: gracefully handle duplicates in case of retry |
| 88 | + // track invalid tags so we don't process them on each update |
| 89 | + pluginsDb.addTag( repo.getId(), tags[ i ], invalidGroup() ); |
| 90 | + return false; |
| 91 | + })); |
| 92 | + }, |
| 93 | + |
| 94 | + // process the releases |
| 95 | + function( error, releases ) { |
| 96 | + if ( error ) { |
| 97 | + retry.log( "processVersions", repo.getId() ); |
| 98 | + return fn( error ); |
| 99 | + } |
| 100 | + |
| 101 | + if ( !releases.length ) { |
| 102 | + return fn( null ); |
| 103 | + } |
| 104 | + |
| 105 | + var group = this.group(); |
| 106 | + releases.forEach(function( release ) { |
| 107 | + processRelease( repo, release, group() ); |
| 108 | + }); |
| 109 | + }, |
| 110 | + |
| 111 | + function( error ) { |
| 112 | + fn( error ); |
| 113 | + } |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +function processRelease( repo, release, fn ) { |
| 118 | + Step( |
| 119 | + // find out who owns this plugin |
| 120 | + // if there is no owner, then set the user as the owner |
| 121 | + function() { |
| 122 | + pluginsDb.getOrSetOwner( release.package.name, repo.userName, this ); |
| 123 | + }, |
| 124 | + |
| 125 | + // verify the user is the owner |
| 126 | + function( error, owner ) { |
| 127 | + if ( error ) { |
| 128 | + retry.log( "processRelease", repo.getId(), release.tag ); |
| 129 | + return fn( error ); |
| 130 | + } |
| 131 | + |
| 132 | + // the plugin is owned by someone else |
| 133 | + if ( owner !== repo.userName ) { |
| 134 | + // TODO: report error to user |
| 135 | + return fn( new UserError( "Plugin " + release.package.name + " is owned by " + owner + "." ) ); |
| 136 | + } |
| 137 | + |
| 138 | + return owner; |
| 139 | + }, |
| 140 | + |
| 141 | + // track the new release |
| 142 | + function( error, owner ) { |
| 143 | + pluginsDb.addRelease( repo.getId(), release, this ); |
| 144 | + }, |
| 145 | + |
| 146 | + // finished processing release |
| 147 | + function( error ) { |
| 148 | + if ( error ) { |
| 149 | + retry.log( "processRelease", repo.getId(), release.tag ); |
| 150 | + return fn( error ); |
| 151 | + } |
| 152 | + |
| 153 | + console.log( "Added " + release.package.name + " " + release.package.version + " to plugins DB" ); |
| 154 | + fn( null, release ); |
| 155 | + } |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +function processMeta( repo, fn ) { |
| 160 | + Step( |
| 161 | + function() { |
| 162 | + repo.getPackageJson( null, this ); |
| 163 | + }, |
| 164 | + |
| 165 | + function( error, package ) { |
| 166 | + if ( error ) { |
| 167 | + retry.log( "processMeta", repo.getId() ); |
| 168 | + return fn( error ); |
| 169 | + } |
| 170 | + |
| 171 | + if ( !package || !package.name ) { |
| 172 | + return fn( null ); |
| 173 | + } |
| 174 | + |
| 175 | + pluginsDb.updatePlugin( package.name, repo.userName, { |
| 176 | + watchers: repo.watchers, |
| 177 | + forks: repo.forks |
| 178 | + }, this ); |
| 179 | + }, |
| 180 | + |
| 181 | + function( error ) { |
| 182 | + if ( error ) { |
| 183 | + retry.log( "processMeta", repo.getId() ); |
| 184 | + return fn( error ); |
| 185 | + } |
| 186 | + |
| 187 | + fn( null ); |
| 188 | + } |
| 189 | + ); |
| 190 | +} |
| 191 | + |
| 192 | +module.exports = { |
| 193 | + processHook: processHook, |
| 194 | + processVersions: processVersions, |
| 195 | + processRelease: processRelease, |
| 196 | + processMeta: processMeta |
| 197 | +}; |
0 commit comments