Skip to content

Commit

Permalink
add git scm-data-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ghedamat committed Mar 29, 2016
1 parent 599ca17 commit 6b3b9a1
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 24 deletions.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 0 additions & 10 deletions circle.yml

This file was deleted.

49 changes: 39 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand All @@ -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

This comment has been minimized.

Copy link
@MarkMT

MarkMT Oct 31, 2016

Seems to be a problem here, instantiating ScmDataGenerator with plugin: this, which is treated in the constructor as the path. See #43 (comment)

}).generate();
} else {
return Promise.resolve();
}
},

_errorMessage: function(error) {
this.log(error, { color: 'red' });
return Promise.reject(error);
Expand Down
26 changes: 26 additions & 0 deletions lib/scm-data-generators/git.js
Original file line number Diff line number Diff line change
@@ -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
});
});
});
}
});
3 changes: 3 additions & 0 deletions lib/scm-data-generators/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"git": require('./git'),
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
});
});

Expand All @@ -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;
},
Expand All @@ -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);
});
});
});
});

38 changes: 38 additions & 0 deletions tests/unit/lib/scm-data-generators/git-nodetest.js
Original file line number Diff line number Diff line change
@@ -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, '[email protected]');
assert.equal(data.name, 'Alisdair McDiarmid');
assert.isNotNull(data.timestamp);
assert.equal(data.branch, 'master');
});
});
});
});

0 comments on commit 6b3b9a1

Please sign in to comment.