diff --git a/package-lock.json b/package-lock.json index e10d69b..4f1053e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -128,8 +128,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "bluebird": { "version": "3.5.1", @@ -140,7 +139,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -250,8 +248,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "cookiejar": { "version": "2.1.2", @@ -556,8 +553,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "function-bind": { "version": "1.1.1", @@ -575,7 +571,6 @@ "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", - "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -585,6 +580,15 @@ "path-is-absolute": "^1.0.0" } }, + "glob-all": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/glob-all/-/glob-all-3.1.0.tgz", + "integrity": "sha1-iRPd+17hrHgSZWJBsD1SF8ZLAqs=", + "requires": { + "glob": "^7.0.5", + "yargs": "~1.2.6" + } + }, "globals": { "version": "11.7.0", "resolved": "https://registry.npmjs.org/globals/-/globals-11.7.0.tgz", @@ -666,7 +670,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -854,7 +857,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -913,7 +915,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -950,8 +951,7 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-is-inside": { "version": "1.0.2", @@ -1225,6 +1225,11 @@ "readable-stream": "^2.3.5" } }, + "superagent-throttle": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/superagent-throttle/-/superagent-throttle-1.0.1.tgz", + "integrity": "sha512-m5Ngf0S5QoA84zgwVqVnVA34u9uvo8uM+QEF9B7eNI5FDaSoSoUwQsx7V1GmLXgYLkolhIiucFDVJXF9z49hgQ==" + }, "supports-color": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", @@ -1318,8 +1323,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "write": { "version": "0.2.1", @@ -1329,6 +1333,21 @@ "requires": { "mkdirp": "^0.5.1" } + }, + "yargs": { + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-1.2.6.tgz", + "integrity": "sha1-nHtKgv1dWVsr8Xq23MQxNUMv40s=", + "requires": { + "minimist": "^0.1.0" + }, + "dependencies": { + "minimist": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.1.0.tgz", + "integrity": "sha1-md9lelJXTCHJBXSX33QnkLK0wN4=" + } + } } } } diff --git a/package.json b/package.json index 3e39cb5..9c70338 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,11 @@ }, "dependencies": { "bluebird": "^3.5.1", + "glob-all": "^3.1.0", "lodash": "^4.17.10", "semver": "^5.5.0", "superagent": "^3.8.3", + "superagent-throttle": "^1.0.1", "uuid": "^3.3.2" }, "peerDependencies": { diff --git a/src/index.js b/src/index.js index 4a78eb1..52ac0df 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,10 @@ const _ = require("lodash") , SemVer = require("semver") , uuid = require("uuid/v4") , request = require("superagent") - , GitRev = require("./git-rev"); + , GitRev = require("./git-rev") + , glob = require("glob-all") + , path = require("path") + , Throttle = require("superagent-throttle"); /** * Serverless Plugin forward Lambda exceptions to Sentry (https://sentry.io) @@ -25,6 +28,14 @@ class Sentry { .then(this.setRelease) .then(this.instrumentFunctions), + "before:package:createDeploymentArtifacts": () => BbPromise.bind(this) + .then(this.createSentryRelease) + .then(this.deploySentryRelease), + + "after:package:createDeploymentArtifacts": () => BbPromise.bind(this) + .then(this.deploySentrySourceMaps), + + "before:deploy:deploy": () => BbPromise.bind(this) .then(this.validate), @@ -156,9 +167,23 @@ class Sentry { _resolveGitRefs(gitRev) { return BbPromise.join( - gitRev.origin().then(str => str.match(/[:/]([^/]+\/[^/]+?)(?:\.git)?$/i)[1]).catch(_.noop), + gitRev.origin(), gitRev.long() ) + .spread((origin, commit) => { + let repository = null; + try { + repository = origin.match(/[:/]([^/]+\/[^/]+?)(?:\.git)?$/i)[1]; + if(_.includes(origin, "gitlab")) { + // gitlab uses spaces around the slashes in the repository name + repository = repository.replace(/\//g, " / "); + } + } + catch (err) { + // ignore + } + return BbPromise.join(repository, commit); + }) .spread((repository, commit) => { _.forEach(_.get(this.sentry, "release.refs", []), ref => { if (ref && ref.repository === "git") { @@ -288,6 +313,95 @@ class Sentry { }); } + deploySentrySourceMaps() { + if (!this.sentry.authToken || !this.sentry.release || !this.sentry.uploadSourcemaps) { + // Nothing to do + return BbPromise.resolve(); + } + + const organization = this.sentry.organization; + const release = this.sentry.release; + const buildDirectory = path.join(this._serverless.config.servicePath, "../"); + this._serverless.cli.log( + `Sentry: Uploading source maps for "${release.version}" from directory "${buildDirectory}"...` + ); + + const throttle = new Throttle({ + active: true, // set false to pause queue + rate: 20, // how many requests can be sent every `ratePer` + ratePer: 1000, // number of ms in which `rate` requests may be sent + concurrent: 5 // how many requests can be sent concurrently + }).on("sent", (request) => { + this._serverless.cli.log( + `Sentry: Uploading file ${request.filename} to sentry...` + ); + }); + + const upload = filepath => BbPromise.fromCallback(cb => { + + let filename = filepath.split(buildDirectory)[1]; + if(filename.startsWith("node_modules")) { + filename = `/var/task/${filename}`; + } + else { + filename = `/var/${filename}`; + } + + return request.post( + `https://sentry.io/api/0/organizations/${organization}/releases/${encodeURIComponent( + release.version + )}/files/` + ) + .set("Authorization", `Bearer ${this.sentry.authToken}`) + .attach( + "file", + filepath, + filename + ) + .use((req) => { + req.filepath = filepath; + req.filename = filename; + return req; + }) + .use(throttle.plugin()) + .end( (error, result) => { + if(result && result.status === 409) { + return cb(null, result); + } + + if(error) { + return cb(error); + } + + return cb(null, result); + }); + }); + + const types = ["js", "js.map", "ts"]; + const globs = types + .map( t => `${buildDirectory}/**/*.${t}`) + .concat(`!${buildDirectory}/node_modules`) + .concat(`!${buildDirectory}/**/*.d.ts`); + + this._serverless.cli.log( + `Sentry: Uploading source maps for globs ${globs}...` + ); + const files = glob.sync(globs); + const uploads = files.map( f => () => upload(f)); + return BbPromise.map(uploads, u => u()).catch(err => { + if (err && err.response && err.responsetext) { + this._serverless.cli.log( + `Sentry: Received error response from Sentry:\n${err.response.text}` + ); + } + return BbPromise.reject( + new this._serverless.classes.Error( + "Sentry: Error uploading source map - " + err.stack + ) + ); + }); + } + getRandomVersion() { return _.replace(uuid(), /-/g, ""); }