From 6b3b9a19995dcbd8deb3029e3b5b852e0981a697 Mon Sep 17 00:00:00 2001 From: Mattia Gheda Date: Wed, 17 Feb 2016 23:15:58 -0500 Subject: [PATCH 1/2] add git scm-data-generator --- README.md | 45 ++++++++++++++++- circle.yml | 10 ---- index.js | 49 +++++++++++++++---- lib/scm-data-generators/git.js | 26 ++++++++++ lib/scm-data-generators/index.js | 3 ++ package.json | 3 +- tests/unit/index-nodetest.js | 8 ++- .../lib/scm-data-generators/git-nodetest.js | 38 ++++++++++++++ 8 files changed, 158 insertions(+), 24 deletions(-) delete mode 100644 circle.yml create mode 100644 lib/scm-data-generators/git.js create mode 100644 lib/scm-data-generators/index.js create mode 100644 tests/unit/lib/scm-data-generators/git-nodetest.js diff --git a/README.md b/README.md index 015dfe2..1c520f2 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,10 @@ For detailed information on how configuration of plugins works, please refer to ### Defaults ``` ENV["revision-data"] = { - type: 'file-hash' + type: 'file-hash', + scm: function(context) { + return require('./lib/scm-data-generators')['git']; + } } ``` ### type @@ -61,6 +64,16 @@ The type of [Data Generator](#data-generators) to be used. *Default:* `'file-hash'` *Alternatives:* `'git-tag-commit'`, `'git-commit'`, `'version-commit'` +### scm + +The type of the [SCM Data Generator](#scm-data-generator) to be used + +*Default:* GitScmDataGenerator + +You can set this to `null` if you don't want any Scm Data Generator to be used. + +You can also pass your own custom scm generator class. + ## Data Generators Data generators are the strategies used to generate information about the revision being deployed. A data generator must return an object which contains a property called `revisionKey` which uniquely identifies the current revision. A generator can add any other data that it deems relevant to the data object that it returns. @@ -155,6 +168,36 @@ The file containing your project's version number. Must be a JSON file with a to *Default:* `package.json` +## SCM Data Generators + +SCM Data generators are the strategies used to collect extra information about the revision being deployed. An scm data generator must return an object which contains properties that it deems relevant to the revision being deployed . + +### Git generator + +This generator uses the information available from the git repository of your ember-cli application. + +#### Data fields returned + +##### sha + +The SHA of the commit being deployed + +##### email + +Committer's email + +##### name + +Committer's name + +##### branch + +Git branch being deployed + +##### timestamp + +Commit's timestamp + ## Prerequisites The following properties are expected to be present on the deployment `context` object: diff --git a/circle.yml b/circle.yml deleted file mode 100644 index f76ae3f..0000000 --- a/circle.yml +++ /dev/null @@ -1,10 +0,0 @@ -machine: - node: - version: 0.12.0 - -dependencies: - pre: - - npm install -g bower - override: - - npm i - - bower i diff --git a/index.js b/index.js index 56d2ff3..8bebb04 100644 --- a/index.js +++ b/index.js @@ -14,25 +14,33 @@ module.exports = { defaultConfig: { type: 'file-hash', filePattern: 'index.html', + versionFile: 'package.json', distDir: function(context) { return context.distDir; }, + distFiles: function(context) { return context.distFiles; }, - versionFile: 'package.json', + + scm: function(context) { + return require('./lib/scm-data-generators')['git']; + } + }, - prepare: function(context) { + + prepare: function(/*context*/) { var self = this; - var type = this.readConfig('type'); - var DataGenerator = require('./lib/data-generators')[type]; - var dataGenerator = new DataGenerator({ - plugin: this - }); - this.log('creating revision data using `' + type + '`', { verbose: true }); - return dataGenerator.generate() - .then(function(data) { + var promises = { + data: this._getData(), + scm: this._getScmData() + }; + + return Promise.hash(promises) + .then(function(results) { + var data = results.data; + data.scm = results.scm; self.log('generated revision data for revision: `' + data.revisionKey + '`', { verbose: true }); return data; }) @@ -41,6 +49,27 @@ module.exports = { }) .catch(this._errorMessage.bind(this)); }, + + _getData: function() { + var type = this.readConfig('type'); + this.log('creating revision data using `' + type + '`', { verbose: true }); + var DataGenerator = require('./lib/data-generators')[type]; + return new DataGenerator({ + plugin: this + }).generate(); + }, + + _getScmData: function() { + var ScmDataGenerator = this.readConfig('scm'); + if (ScmDataGenerator) { + return new ScmDataGenerator({ + plugin: this + }).generate(); + } else { + return Promise.resolve(); + } + }, + _errorMessage: function(error) { this.log(error, { color: 'red' }); return Promise.reject(error); diff --git a/lib/scm-data-generators/git.js b/lib/scm-data-generators/git.js new file mode 100644 index 0000000..e5dc987 --- /dev/null +++ b/lib/scm-data-generators/git.js @@ -0,0 +1,26 @@ +var CoreObject = require('core-object'); +var Promise = require('ember-cli/lib/ext/promise'); +var gitRepoInfo = require('git-repo-info'); +var simpleGit = require('simple-git'); + +module.exports = CoreObject.extend({ + init: function(path) { + this.path = path; + }, + + generate: function() { + var _this = this; + return new Promise(function(resolve, reject) { + simpleGit(_this.path).log(function(err, log) { + var info = log.latest; + resolve({ + sha: info.hash.substring(1), + email: info.author_email.substring(0, info.author_email.length - 1), + name: info.author_name, + timestamp: new Date(info.date).toISOString(), + branch: gitRepoInfo().branch + }); + }); + }); + } +}); diff --git a/lib/scm-data-generators/index.js b/lib/scm-data-generators/index.js new file mode 100644 index 0000000..464dab0 --- /dev/null +++ b/lib/scm-data-generators/index.js @@ -0,0 +1,3 @@ +module.exports = { + "git": require('./git'), +}; diff --git a/package.json b/package.json index c236db2..0e88cc8 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,8 @@ "ember-cli-deploy-plugin": "^0.2.1", "git-repo-info": "^1.1.2", "minimatch": "^2.0.4", - "rsvp": "^3.0.18" + "rsvp": "^3.0.18", + "simple-git": "^1.26.2" }, "ember-addon": { "configPath": "tests/dummy/config" diff --git a/tests/unit/index-nodetest.js b/tests/unit/index-nodetest.js index a65f3c9..bd2a3a5 100644 --- a/tests/unit/index-nodetest.js +++ b/tests/unit/index-nodetest.js @@ -78,7 +78,7 @@ describe('the index', function() { return previous; }, []); - assert.equal(messages.length, 5); + assert.equal(messages.length, 6); }); it('adds default config to the config object', function() { @@ -99,6 +99,7 @@ describe('the index', function() { assert.isDefined(context.config['revision-data'].type); assert.isDefined(context.config['revision-data'].filePattern); + assert.isDefined(context.config['revision-data'].scm); }); }); @@ -116,6 +117,9 @@ describe('the index', function() { "revision-data": { type: 'file-hash', filePattern: 'index.html', + scm: function(context) { + return require('../../lib/scm-data-generators')['git']; + }, distDir: function(context) { return context.distDir; }, @@ -131,8 +135,8 @@ describe('the index', function() { .then(function(result) { assert.equal(result.revisionData.revisionKey, 'ae1569f72495012cd5e8588e0f2f5d49'); assert.isNotNull(result.revisionData.timestamp); + assert.isNotNull(result.revisionData.scm.email); }); }); }); }); - diff --git a/tests/unit/lib/scm-data-generators/git-nodetest.js b/tests/unit/lib/scm-data-generators/git-nodetest.js new file mode 100644 index 0000000..4bebb04 --- /dev/null +++ b/tests/unit/lib/scm-data-generators/git-nodetest.js @@ -0,0 +1,38 @@ +'use strict'; + +var assert = require('ember-cli/tests/helpers/assert'); +var gitRepoInfo = require('git-repo-info'); +var ScmDataGenerator = require('../../../../lib/scm-data-generators/git'); + +describe('the git scm data generator', function() { + var cwd; + + before(function() { + gitRepoInfo._changeGitDir('dotgit'); + }); + + beforeEach(function() { + cwd = process.cwd(); + }); + + afterEach(function() { + process.chdir(cwd); + }); + + describe('#generate', function() { + it('returns the correct data', function() { + process.chdir('tests/fixtures/repo'); + + var subject = new ScmDataGenerator('dotgit'); + + return assert.isFulfilled(subject.generate()) + .then(function(data) { + assert.equal(data.sha, '41d41f081b45ad50935c08b1203220737d9739b4'); + assert.equal(data.email, 'alisdair@mcdiarmid.org'); + assert.equal(data.name, 'Alisdair McDiarmid'); + assert.isNotNull(data.timestamp); + assert.equal(data.branch, 'master'); + }); + }); + }); +}); From ed356c8b5122e0741e0d8cfdb1620e11c3811c96 Mon Sep 17 00:00:00 2001 From: Mattia Gheda Date: Tue, 29 Mar 2016 16:01:27 -0700 Subject: [PATCH 2/2] add support for storing git tag --- lib/scm-data-generators/git.js | 3 ++- tests/unit/lib/scm-data-generators/git-nodetest.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/scm-data-generators/git.js b/lib/scm-data-generators/git.js index e5dc987..2c21220 100644 --- a/lib/scm-data-generators/git.js +++ b/lib/scm-data-generators/git.js @@ -18,7 +18,8 @@ module.exports = CoreObject.extend({ email: info.author_email.substring(0, info.author_email.length - 1), name: info.author_name, timestamp: new Date(info.date).toISOString(), - branch: gitRepoInfo().branch + branch: gitRepoInfo().branch, + tag: gitRepoInfo().tag }); }); }); diff --git a/tests/unit/lib/scm-data-generators/git-nodetest.js b/tests/unit/lib/scm-data-generators/git-nodetest.js index 4bebb04..5915a5a 100644 --- a/tests/unit/lib/scm-data-generators/git-nodetest.js +++ b/tests/unit/lib/scm-data-generators/git-nodetest.js @@ -32,6 +32,7 @@ describe('the git scm data generator', function() { assert.equal(data.name, 'Alisdair McDiarmid'); assert.isNotNull(data.timestamp); assert.equal(data.branch, 'master'); + assert.equal(data.tag, '2.3.4'); }); }); });