Skip to content

Commit

Permalink
remove necessity of travis bot and add request of any repository status
Browse files Browse the repository at this point in the history
  • Loading branch information
eromano committed Apr 6, 2016
1 parent 9349b45 commit 666b1ba
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ logs
npm-debug.log*
pids
*.pid
/config.json
config.json
*.seed
lib-cov
coverage
Expand Down
15 changes: 5 additions & 10 deletions src/ciAlarmBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@ var assert = require('assert');

class CiAlarmBot {

constructor(token, idBotCi, githubToken, gpioPin) {
constructor(token, githubToken, gpioPin) {
assert(token, 'Slack Tocken is necessary');
assert(idBotCi, 'Id Bot CI, is necessary');
assert(githubToken, 'GithubToken is necessary');

this.gpioPin = !gpioPin ? 22 : gpioPin;

this.buildStatus = {message: 'Unknown', color: this.infoColor};
this.ciBotId = idBotCi;

this.raspberryInterface = new RaspberryInterface(this.gpioPin);
this.travisService = new TravisService(githubToken);

this.travisService.on('travis:login:ok', ()=> {
this.run(token, idBotCi);
this.run(token);
});
}

run(token, idBotCi) {
this.slackMessageInterface = new SlackMessageInterface(token, idBotCi, this.travisService);
run(token) {
this.slackMessageInterface = new SlackMessageInterface(token, this.travisService);
this.slackMessageInterface.run();

this.travisService.getUserRepository().then((res)=> {
console.log(res.repos[0].id);
this.travisService.getUserRepositoriesSlugList().then((res)=> {
this.slackMessageInterface.postSlackMessageToChannel(res.repos[0].id, '');
});

Expand Down
3 changes: 1 addition & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ nconf.add('config', {type: 'file', file: './config.json'});

try {
var tokenSlack = nconf.get('tokenslack');
var ciBotId = nconf.get('cibotid');
var githubToken = nconf.get('githubtoken');

new CiAlarmBot(tokenSlack, ciBotId, githubToken);
new CiAlarmBot(tokenSlack, githubToken);
} catch (error) {
console.log('Bot failed' + error);
}
Expand Down
65 changes: 34 additions & 31 deletions src/slackMessageInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ class slackMessageInterface {
return 'warning';
}

constructor(token, idBotCi, ciService) {
constructor(token, ciService) {
var settingsBot = {
token: token,
name: 'CI Bot Alarm'
};

this.bot = new Bot(settingsBot);
this.buildStatus = {message: 'Unknown', color: this.infoColor};
this.ciBotId = idBotCi;
this.ciService = ciService;
}

run() {
this.startChannelMessage();
this.listenerRequestStatusBuild();
this.listenerCiChangeStatusMessageBuild();
this.listenerRepositoryListMessage();
}

Expand All @@ -54,24 +51,21 @@ class slackMessageInterface {
}).bind(this));
}

listenerCiChangeStatusMessageBuild() {
this.bot.on('message', ((message) => {
if (this.isFromCiSlackBot(message)) {
if (this.isFailingMessage(message)) {
this.buildStatus = {message: 'Failed', color: this.failColor};
} else if (this.isSuccessMessage(message)) {
this.buildStatus = {message: 'Success', color: this.successColor};
}
}
}));
}

listenerRequestStatusBuild() {
this.bot.on('message', ((message) => {
if (!this.isFromCiAlarmBotMessage(message) && this.isChatMessage(message) &&
this.isMentioningCiAlarm(message) && this.isStatusRequest(message)) {

this.postSlackMessageToChannel('Hi <@' + message.user + '> the build Status is ' + this.buildStatus.message + '!', 'Ci status');
var repoName = this.getRepositoriesNameInMessage(message);

if (repoName) {
this.ciService.getLastBuildStatusByRepository(repoName).then((statusBuild)=> {
this.postSlackMessageToChannel('Hi <@' + message.user + '> the build Status is ' + statusBuild + '!', 'Ci status', this.colorByStatus(statusBuild));
},(error)=> {
this.postSlackMessageToChannel(error.toString(), 'Ci status', this.failColor);
});
}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
}
}
}));
}
Expand All @@ -81,7 +75,7 @@ class slackMessageInterface {
if (!this.isFromCiAlarmBotMessage(message) && this.isChatMessage(message) &&
this.isMentioningCiAlarm(message) && this.isListRepositoryRequest(message)) {

this.ciService.getUserRepository().then((repositories)=> {
this.ciService.getUserRepositoriesSlugList().then((repositories)=> {
this.postSlackMessageToChannel('Hi <@' + message.user + '> this is the repository list: \n • ' +
repositories.join('\n• ') + 'Repository list', this.infoColor);
});
Expand All @@ -94,15 +88,15 @@ class slackMessageInterface {
*
* @param {String} message
* @param {String} fallback
* @param {successColor|failColor|infoColor} color
* @param {successColor|failColor|infoColor} color of the vertical line before the message default infoColor yellow
*/
postSlackMessageToChannel(message, fallback, color) {
var params = {
icon_emoji: ':robot_face:',
attachments: [
{
'fallback': fallback,
'color': color || this.buildStatus.color,
'color': color || this.infoColor,
'author_name': 'Ci Alarm',
'author_link': 'https://github.com/eromano/ci-alarm',
'text': message
Expand All @@ -116,6 +110,15 @@ class slackMessageInterface {
return message.hasOwnProperty('username') && (message.username === this.bot.name);
}

getRepositoriesNameInMessage(message) {
var statusPos = message.text.toLowerCase().indexOf('status');
var afterStatus = message.text.toLowerCase().substr(statusPos + 6,message.length);
var allPhrasesSeparateBySpace = afterStatus.split(' ');
if (allPhrasesSeparateBySpace && allPhrasesSeparateBySpace.length > 1) {
return allPhrasesSeparateBySpace[1].trim();
}
}

isChatMessage(message) {
return message.type === 'message' && Boolean(message.text);
}
Expand All @@ -124,10 +127,6 @@ class slackMessageInterface {
return message.text && message.text.indexOf(this.bot.self.id) > -1;
}

isFromCiSlackBot(message) {
return this.ciBotId === message.bot_id;
}

isListRepositoryRequest(message) {
return message.text && message.text.toLowerCase().indexOf('repository list') > -1;
}
Expand All @@ -136,12 +135,16 @@ class slackMessageInterface {
return message.text && message.text.toLowerCase().indexOf('status') > -1;
}

isFailingMessage(message) {
return message.attachments[0].text.indexOf('failed') > -1;
}

isSuccessMessage(message) {
return message.attachments[0].text.indexOf('passed') > -1;
colorByStatus(status) {
var color;
if (status === 'passed') {
color = this.successColor;
} else if (status === 'failed') {
color = this.failColor;
} else {
color = this.infoColor;
}
return color;
}
}

Expand Down
46 changes: 42 additions & 4 deletions src/travisService.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class travisInterface {

this.travisAuth = new TravisAuth(this.travis, githubToken);
this.travisAuth.login().then(() => {
this.getAccountInfo().then(() => {this.emit('travis:login:ok');},() => {this.emit('travis:login:error');});
this.getAccountInfo().then(() => {
this.emit('travis:login:ok');
}, () => {
this.emit('travis:login:error');
});
});
}

Expand All @@ -49,15 +53,49 @@ class travisInterface {
}

/**
* Retrieve the user repository in a promise
* Retrieve the user repository List in a promise
*
* @return {Promise} A promise that returns the list of the all repositories
*/
getUserRepository() {
getUserRepositoriesList() {
return new Promise((resolve, reject) => {
this.travis.repos(this.username).get(function (err, res) {
if (err || !res) {
reject(new Error(('Get UserRepository Error ' + err)));
}
resolve(_.map(res.repos,'slug'));
resolve(res.repos);
});
});
}

/**
* Retrieve the user repositories slug list in a promise
*
* @return {Promise} A promise that returns the list of the all repositories slug
*/
getUserRepositoriesSlugList() {
return new Promise((resolve) => {
this.getUserRepositoriesList().then((repositoriesList)=> {
resolve(_.map(repositoriesList, 'slug'));
});
});
}

/**
* Retrieve the repository master branch status
*
* @param {String} repositoryName name of the repository which you are interested in
* @return {Promise} A promise that returns the status of the last build of the repository which you are interested in
*/
getLastBuildStatusByRepository(repositoryName) {
return new Promise((resolve, reject) => {
this.getUserRepositoriesList().then((repositoriesList)=> {
var slugRepository = _.find(repositoriesList, ['slug', repositoryName]);
if (slugRepository) {
resolve(slugRepository.last_build_state);
}else {
reject(new Error(('This repositories dosen\'t exixst')));
}
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/mockObjects/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class repository {
static createRepositoriesList() {
return [
this.createRepository({slug: 'fakeuser/fake-project1'}),
this.createRepository({slug: 'fakeuser/fake-project2'}),
this.createRepository({slug: 'fakeuser/fake-project2', last_build_state: 'failed'}),
this.createRepository({slug: 'fakeuser/fake-project3'})
];
}
Expand Down
2 changes: 1 addition & 1 deletion test/slackMessageInterface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Bot Initialization', function () {

this.loginStub = sinon.stub(Bot.prototype, 'login', function () {});

this.slackMessageInterface = new SlackMessageInterface('Fake-token-slack', 'B0W93JU9Y');
this.slackMessageInterface = new SlackMessageInterface('Fake-token-slack');
this.slackMessageInterface.run();
});

Expand Down
Loading

0 comments on commit 666b1ba

Please sign in to comment.