Skip to content

Allow custom commit hash length when constructing revisionKey #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Constructs a revision key based on the most recent git tag and the currently che

##### revisionKey

The unique identifier of this build based on the git tag, followed by a the separator symbol (`+` by default), followed by the first 8 characters of the current commit hash.
The unique identifier of this build based on the git tag, followed by the separator symbol (`+` by default), followed by the first 8 characters of the current commit hash.

For example, if your most recent git tag is `v2.0.3`, and the current commit is `0993043d49f9e0[...]`, this generator will return a revision of `v2.0.3+0993043d`.

Expand All @@ -130,6 +130,10 @@ The timestamp of the current deploy

#### Configuration Options

##### commitHashLength

The length of the commit hash that is used when constructing the `revisionKey`.

##### separator

The text used to separate the tag name from the commit sha. By default, `+` is used.
Expand All @@ -150,6 +154,12 @@ For example, if the current commit is `0993043d49f9e0[...]`, this generator will

The timestamp of the current deploy

#### Configuration Options

##### commitHashLength

The length of the commit hash that is used as the `revisionKey`.

### Version Commit generator

Similar to the Git Tag Commit generator but uses the `package.json` version string to construct the revision key instead of the git tag.
Expand All @@ -170,6 +180,10 @@ The timestamp of the current deploy

#### Configuration Options

##### commitHashLength

The length of the commit hash that is used when constructing the `revisionKey`.

##### separator

The text used to separate the tag name from the commit sha. By default, `+` is used.
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ module.exports = {
separator: '+',
filePattern: 'index.html',
versionFile: 'package.json',

commitHashLength: function() {
if (this.type === 'git-commit') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added to ensure that we are not breaking the current behavior even though it is inconsistent.

return 7;
} else {
return 8;
}
},

distDir: function(context) {
return context.distDir;
},
Expand All @@ -26,7 +35,6 @@ module.exports = {
scm: function(/* context */) {
return require('./lib/scm-data-generators')['git'];
}

},

prepare: function(/*context*/) {
Expand Down
8 changes: 7 additions & 1 deletion lib/data-generators/git-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ var gitRepoInfo = require('git-repo-info');
var RSVP = require('rsvp');

module.exports = CoreObject.extend({
init: function(options) {
this._super();
this._plugin = options.plugin;
},

generate: function() {
var commitHashLength = this._plugin.readConfig('commitHashLength');
var info = gitRepoInfo();

if (info === null || info.root === null) {
return RSVP.reject('Could not find git repository');
}

var sha = info.sha.slice(0, 7);
var sha = info.sha.slice(0, commitHashLength);

if (!sha) {
return RSVP.reject('Could not build revision with commit hash `' + sha + '`');
Expand Down
3 changes: 2 additions & 1 deletion lib/data-generators/git-tag-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = CoreObject.extend({
},

generate: function() {
var commitHashLength = this._plugin.readConfig('commitHashLength');
var separator = this._plugin.readConfig('separator');
var info = gitRepoInfo();

Expand All @@ -17,7 +18,7 @@ module.exports = CoreObject.extend({
}

var tag = info.tag;
var sha = info.sha.slice(0, 8);
var sha = info.sha.slice(0, commitHashLength);

if (!info.tag || !sha) {
return RSVP.reject('Could not build revision with tag `' + tag + '` and commit hash `' + sha + '`');
Expand Down
3 changes: 2 additions & 1 deletion lib/data-generators/version-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = CoreObject.extend({
},

generate: function() {
var commitHashLength = this._plugin.readConfig('commitHashLength');
var separator = this._plugin.readConfig('separator');
var versionFile = this._plugin.readConfig('versionFile');

Expand All @@ -22,7 +23,7 @@ module.exports = CoreObject.extend({
return RSVP.reject('Could not find git repository');
}

var sha = (info.sha || '').slice(0, 8);
var sha = (info.sha || '').slice(0, commitHashLength);
var plugin = this._plugin;

return readFile(versionFile)
Expand Down
57 changes: 56 additions & 1 deletion tests/unit/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('the index', function() {
plugin.configure(context);
assert.ok(true); // it didn't throw
});

it('warns about missing optional config', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-data'
Expand All @@ -78,7 +79,7 @@ describe('the index', function() {
return previous;
}, []);

assert.equal(messages.length, 7);
assert.equal(messages.length, 8);
});

it('adds default config to the config object', function() {
Expand All @@ -101,6 +102,60 @@ describe('the index', function() {
assert.isDefined(context.config['revision-data'].filePattern);
assert.isDefined(context.config['revision-data'].scm);
});

it('defaults commitHashLength to 7 when type is git-commit', function() {
var plugin = subject.createDeployPlugin({ name: 'revision-data' });

var context = {
ui: mockUi,
config: {
'revision-data': {
type: 'git-commit'
}
}
};

plugin.beforeHook(context);
plugin.configure(context);

assert.equal(context.config['revision-data'].commitHashLength(), 7);
});

it('defaults commitHashLength to 8 when type is git-tag-commit', function() {
var plugin = subject.createDeployPlugin({ name: 'revision-data' });

var context = {
ui: mockUi,
config: {
'revision-data': {
type: 'git-tag-commit'
}
}
};

plugin.beforeHook(context);
plugin.configure(context);

assert.equal(context.config['revision-data'].commitHashLength(), 8);
});

it('defaults commitHashLength to 8 when type is version-commit', function() {
var plugin = subject.createDeployPlugin({ name: 'revision-data' });

var context = {
ui: mockUi,
config: {
'revision-data': {
type: 'version-commit'
}
}
};

plugin.beforeHook(context);
plugin.configure(context);

assert.equal(context.config['revision-data'].commitHashLength(), 8);
});
});

describe('prepare hook', function() {
Expand Down
30 changes: 26 additions & 4 deletions tests/unit/lib/data-generators/git-commit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ describe('the git-commit data generator', function() {
var DataGenerator;
var cwd;

var stubbedPlugin = function(options = {}) {
let defaultConfig = { commitHashLength: 7 };

return {
stubbedConfig: Object.assign(defaultConfig, options),
readConfig: function(key) { return this.stubbedConfig[key]; }
};
};

before(function() {
DataGenerator = require('../../../../lib/data-generators/git-commit');
gitRepoInfo._changeGitDir('dotgit');
Expand All @@ -21,21 +30,34 @@ describe('the git-commit data generator', function() {
});

describe('#generate', function() {
it('revision key contains first 7 characters of git commit hash', function() {
it('sets revision key to first 7 characters of git commit hash', function() {
process.chdir('tests/fixtures/repo');

var subject = new DataGenerator();
var subject = new DataGenerator({ plugin: stubbedPlugin() });

return assert.isFulfilled(subject.generate())
.then(function(data) {
assert.equal(data.revisionKey, '41d41f0');
});
});

it('sets revision key to git commit hash of custom length', function() {
process.chdir('tests/fixtures/repo');

var subject = new DataGenerator({
plugin: stubbedPlugin({ commitHashLength: 40 })
});

return assert.isFulfilled(subject.generate())
.then(function(data) {
assert.equal(data.revisionKey, '41d41f081b45ad50935c08b1203220737d9739b4');
});
});

it('returns a timestamp', function() {
process.chdir('tests/fixtures/repo');

var subject = new DataGenerator();
var subject = new DataGenerator({ plugin: stubbedPlugin() });

return assert.isFulfilled(subject.generate())
.then(function(data) {
Expand All @@ -46,7 +68,7 @@ describe('the git-commit data generator', function() {
it('rejects if no repository found', function() {
process.chdir('tests/fixtures/not-a-repo');

var subject = new DataGenerator();
var subject = new DataGenerator({ plugin: stubbedPlugin() });

return assert.isRejected(subject.generate())
.then(function(error) {
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/lib/data-generators/git-tag-commit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('the git-tag-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
separator: '+'
},
readConfig: function(key) { return this.stubConfig[key]; },
Expand Down Expand Up @@ -39,11 +40,31 @@ describe('the git-tag-commit data generator', function() {
});
});

it('concatenates the git tag and the git commit hash of custom length', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
commitHashLength: 40,
separator: '+'
},
readConfig: function(key) { return this.stubConfig[key]; },
};

var subject = new DataGenerator({ plugin: plugin });

return assert.isFulfilled(subject.generate())
.then(function(data) {
assert.equal(data.revisionKey, '2.3.4+41d41f081b45ad50935c08b1203220737d9739b4');
});
});

it('concatenates the git tag and the git commit hash with a custom separator', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
commitHashLength: 8,
separator: '--'
},
readConfig: function(key) { return this.stubConfig[key]; },
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/lib/data-generators/version-commit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('the version-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'package.json',
separator: '+'
},
Expand All @@ -58,6 +59,7 @@ describe('the version-commit data generator', function() {
var expectedMessage = /missing git commit sha/i;
var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'package.json',
separator: '+'
},
Expand All @@ -80,6 +82,7 @@ describe('the version-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'package.json',
separator: '+'
},
Expand All @@ -96,11 +99,34 @@ describe('the version-commit data generator', function() {
});
});

it('concatenates the package version and the git commit hash of custom length', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
commitHashLength: 40,
versionFile: 'package.json',
separator: '+'
},
readConfig: function(key) { return this.stubConfig[key]; }
};

var subject = new DataGenerator({
plugin: plugin
});

return assert.isFulfilled(subject.generate())
.then(function(data) {
assert.equal(data.revisionKey, '3.2.1+41d41f081b45ad50935c08b1203220737d9739b4');
});
});

it('concatenates the package version and the git commit hash with a custom separator', function() {
process.chdir('tests/fixtures/repo');

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'package.json',
separator: '--'
},
Expand All @@ -122,6 +148,7 @@ describe('the version-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'version.json',
separator: '+'
},
Expand All @@ -143,6 +170,7 @@ describe('the version-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'package.json',
separator: '+'
},
Expand All @@ -164,6 +192,7 @@ describe('the version-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'package.json',
separator: '+'
},
Expand All @@ -185,6 +214,7 @@ describe('the version-commit data generator', function() {

var plugin = {
stubConfig: {
commitHashLength: 8,
versionFile: 'tests/fixtures/missing-version.json',
separator: '+'
},
Expand Down