diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..2d4e4b1 --- /dev/null +++ b/doc/README.md @@ -0,0 +1 @@ +## How to build a Raspberry Light alarm diff --git a/src/slackMessageInterface.js b/src/slackMessageInterface.js index 54d0c20..f343ac4 100644 --- a/src/slackMessageInterface.js +++ b/src/slackMessageInterface.js @@ -63,12 +63,13 @@ class slackMessageInterface { var fields = this._createFieldsAdditionInformationMessage(statusBuild); var lastBuildState = statusBuild.last_build_state ? statusBuild.last_build_state : 'unknown'; - this.postSlackMessageToChannel('Hi <@' + message.user + '> the build Status was ' + lastBuildState + ' ' + moment(statusBuild.last_build_finished_at).fromNow(), 'Ci status', this.colorByStatus(lastBuildState), fields); // jscs:ignore maximumLineLength + this.postSlackMessageToChannel('Hi <@' + message.user + '> the build Status was ' + lastBuildState + ' ' + moment(statusBuild.last_build_finished_at).fromNow(), 'Ci status', this.colorByStatus(lastBuildState), fields, 'Build status', statusBuild.linkBuild); // jscs:ignore maximumLineLength }, (error)=> { - this.postSlackMessageToChannel(error.toString(), 'Ci status', this.failColor); + this.postSlackMessageToChannel(error.toString(), 'Ci status', this.failColor, null, 'Build status'); }); } else { - this.postSlackMessageToChannel('Maybe you want use the command : "status username/example-project" but you forgot to add the repository slug', 'Ci status', this.infoColor);// jscs:ignore maximumLineLength + this.postSlackMessageToChannel('Maybe you want use the command : "status username/example-project" but' + + ' you forgot to add the repository slug', 'Ci status', this.infoColor, null, 'Build status'); } } })); @@ -83,7 +84,7 @@ class slackMessageInterface { this.ciService.getUserRepositoriesSlugList().then((repositories)=> { this.postSlackMessageToChannel('Hi <@' + message.user + '> this is the repository list: \n • ' + - repositories.join('\n• ') + 'Repository list', this.infoColor); + repositories.join('\n• ') + 'Repository list', this.infoColor, null, 'Repositories list'); }); } })); @@ -95,7 +96,7 @@ class slackMessageInterface { listenerCommandListMessage() { this.bot.on('message', ((message) => { if (this.isValidCiMentionMessage(message) && this.isCommandListRequest(message)) { - this.postSlackMessageToChannel('Command list: \n • repository list \n • status username/example-project'); + this.postSlackMessageToChannel('Command list: \n • repository list \n • status username/example-project', this.infoColor, null, 'Repository list'); } })); } @@ -108,15 +109,15 @@ class slackMessageInterface { * @param {successColor|failColor|infoColor} color of the vertical line before the message default infoColor yellow * @param {Array} fields is an Array of messages { 'title': 'Project', 'value': 'Awesome Project','short': true}, */ - postSlackMessageToChannel(message, fallback, color, fields) { + postSlackMessageToChannel(message, fallback, color, fields, title, titleLink) { var params = { icon_emoji: ':robot_face:', attachments: [ { 'fallback': fallback, 'color': color || this.infoColor, - 'author_name': 'Ci Alarm', - 'author_link': 'https://github.com/eromano/ci-alarm', + 'title': title ? title : 'Ci Alarm', + 'title_link': titleLink, 'text': message, 'fields': fields } @@ -143,7 +144,7 @@ class slackMessageInterface { _createFieldsAdditionInformationMessage(statusBuild) { return [ {'title': 'Elapsed time', 'value': (statusBuild.last_build_duration + ' sec'), 'short': true}, - {'title': 'Build Number', 'value': ('#' + statusBuild.last_build_number), 'short': true} + {'title': 'Build Number', 'value': ('<' + statusBuild.linkBuild + '|Build #' + statusBuild.last_build_number + '>'), 'short': true} ]; } diff --git a/src/travisService.js b/src/travisService.js index 26b3c1e..175f23e 100644 --- a/src/travisService.js +++ b/src/travisService.js @@ -27,6 +27,7 @@ class travisInterface { } }); + this.travisBaseUrl = 'https://travis-ci.org'; this.travisAuth = new TravisAuth(this.travis, githubToken); this.travisAuth.login().then(() => { this.getAccountInfo().then(() => { @@ -92,20 +93,25 @@ class travisInterface { this.getUserRepositoriesList().then((repositoriesList)=> { - var slugRepository = _.find(repositoriesList, (repository)=> { + var repository = _.find(repositoriesList, (repository)=> { if (repository.slug.indexOf(repositoryName) > -1) { - return repository.slug; + return true; } }); - if (slugRepository) { - resolve(slugRepository); + if (repository) { + this._expandBaseRepositoryTravisObject(repository); + resolve(repository); } else { reject(new Error(('This repositories dosen\'t exixst'))); } }); }); } + + _expandBaseRepositoryTravisObject(repository) { + repository.linkBuild = this.travisBaseUrl + '/' + repository.slug + '/builds/' + repository.last_build_id; + } } util.inherits(travisInterface, EventEmitter); diff --git a/test/mockObjects/repository.js b/test/mockObjects/repository.js index 49d4fc3..0bea528 100644 --- a/test/mockObjects/repository.js +++ b/test/mockObjects/repository.js @@ -17,7 +17,8 @@ class repository { 'last_build_started_at': new Date(), 'last_build_finished_at': new Date(), 'active': true, - 'github_language': 'JavaScript' + 'github_language': 'JavaScript', + 'linkBuild': '' }; return _.extend(defaultAttributes, attributes); diff --git a/test/slackMessageInterfaceBotStatusNotify.spec.js b/test/slackMessageInterfaceBotStatusNotify.spec.js index 647a0de..92bb0f5 100644 --- a/test/slackMessageInterfaceBotStatusNotify.spec.js +++ b/test/slackMessageInterfaceBotStatusNotify.spec.js @@ -19,6 +19,8 @@ describe('Bot CI build communication', function () { this.textCheck = message.attachments[0].text; this.colorMessage = message.attachments[0].color; this.fields = message.attachments[0].fields; + this.title = message.attachments[0].title; + this.title_link = message.attachments[0].title_link; }).bind(this)); this.loginStub = sinon.stub(Bot.prototype, 'login', function () {}); @@ -91,7 +93,9 @@ describe('Bot CI build communication', function () { expect(this.textCheck).to.be.equal('Hi <@C3P0> the build Status was passed a few seconds ago'); expect(this.colorMessage).to.be.equal(this.slackMessageInterface.successColor); expect(JSON.stringify(this.fields[0])).to.be.equal('{"title":"Elapsed time","value":"52 sec","short":true}'); - expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":"#37","short":true}'); + expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":' + + '"","short":true}'); + expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project1/builds/120506232'); done(); }, 50); }); @@ -113,7 +117,9 @@ describe('Bot CI build communication', function () { expect(this.textCheck).to.be.equal('Hi <@C3P0> the build Status was failed a few seconds ago'); expect(this.colorMessage).to.be.equal(this.slackMessageInterface.failColor); expect(JSON.stringify(this.fields[0])).to.be.equal('{"title":"Elapsed time","value":"52 sec","short":true}'); - expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":"#37","short":true}'); + expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":' + + '"","short":true}'); + expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project2/builds/120506232'); done(); }, 50); @@ -136,7 +142,9 @@ describe('Bot CI build communication', function () { expect(this.textCheck).to.be.equal('Hi <@C3P0> the build Status was unknown a few seconds ago'); expect(this.colorMessage).to.be.equal(this.slackMessageInterface.infoColor); expect(JSON.stringify(this.fields[0])).to.be.equal('{"title":"Elapsed time","value":"52 sec","short":true}'); - expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":"#37","short":true}'); + expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":' + + '"","short":true}'); + expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project3/builds/120506232'); done(); }, 50); }); @@ -158,7 +166,9 @@ describe('Bot CI build communication', function () { expect(this.textCheck).to.be.equal('Hi <@C3P0> the build Status was unknown a few seconds ago'); expect(this.colorMessage).to.be.equal(this.slackMessageInterface.infoColor); expect(JSON.stringify(this.fields[0])).to.be.equal('{"title":"Elapsed time","value":"52 sec","short":true}'); - expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":"#37","short":true}'); + expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":' + + '"","short":true}'); + expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project3/builds/120506232'); done(); }, 50); @@ -181,7 +191,9 @@ describe('Bot CI build communication', function () { expect(this.textCheck).to.be.equal('Hi <@C3P0> the build Status was failed a few seconds ago'); expect(this.colorMessage).to.be.equal(this.slackMessageInterface.failColor); expect(JSON.stringify(this.fields[0])).to.be.equal('{"title":"Elapsed time","value":"52 sec","short":true}'); - expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":"#37","short":true}'); + expect(JSON.stringify(this.fields[1])).to.be.equal('{"title":"Build Number","value":' + + '"","short":true}'); + expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project2/builds/120506232'); done(); }, 50);