Skip to content

Commit

Permalink
🔗 Add Link travis build in the description closes #10
Browse files Browse the repository at this point in the history
Title message should be the name of the command answered #15
  • Loading branch information
eromano committed Apr 9, 2016
1 parent d4712c5 commit d2337de
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## How to build a Raspberry Light alarm
19 changes: 10 additions & 9 deletions src/slackMessageInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}));
Expand All @@ -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');
});
}
}));
Expand All @@ -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');
}
}));
}
Expand All @@ -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
}
Expand All @@ -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}
];
}

Expand Down
14 changes: 10 additions & 4 deletions src/travisService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion test/mockObjects/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 17 additions & 5 deletions test/slackMessageInterfaceBotStatusNotify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {});
Expand Down Expand Up @@ -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":' +
'"<https://travis-ci.org/fakeuser/fake-project1/builds/120506232|Build #37>","short":true}');
expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project1/builds/120506232');
done();
}, 50);
});
Expand All @@ -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":' +
'"<https://travis-ci.org/fakeuser/fake-project2/builds/120506232|Build #37>","short":true}');
expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project2/builds/120506232');
done();
}, 50);

Expand All @@ -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":' +
'"<https://travis-ci.org/fakeuser/fake-project3/builds/120506232|Build #37>","short":true}');
expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project3/builds/120506232');
done();
}, 50);
});
Expand All @@ -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":' +
'"<https://travis-ci.org/fakeuser/fake-project3/builds/120506232|Build #37>","short":true}');
expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project3/builds/120506232');
done();
}, 50);

Expand All @@ -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":' +
'"<https://travis-ci.org/fakeuser/fake-project2/builds/120506232|Build #37>","short":true}');
expect(this.title_link).to.be.equal('https://travis-ci.org/fakeuser/fake-project2/builds/120506232');
done();
}, 50);

Expand Down

0 comments on commit d2337de

Please sign in to comment.