diff --git a/lib/circleci.coffee b/lib/circleci.coffee index 3595c90..e6504b1 100644 --- a/lib/circleci.coffee +++ b/lib/circleci.coffee @@ -21,11 +21,7 @@ class CircleCI _endpoint = "https://circleci.com/api/v1.1/project/#{@vcsType}/#{@owner}/#{@project}/tree/#{@branch}" unless _endpoint? return _endpoint - notify: (destination) -> - @destination = destination - return @ - - execute: (robot) -> + execute: (robot, callback) -> params = JSON.stringify(build_parameters: { CIRCLE_JOB: @job, JOB_USER: robot.name }) robot.http("#{@endpoint()}?circle-token=#{@token}") @@ -34,15 +30,15 @@ class CircleCI .post(params) (error, response, body) -> if error - robot.send { room: @destination }, "Encountered an error :( `#{error}`" + callback("Encountered an error :( `#{error}`") return if response.statusCode < 200 or response.statusCode >= 300 - robot.send { room: @destination }, "Request fail :( `#{response.statusCode}: #{response.statusMessage}`" + callback("Request fail :( `#{response.statusCode}: #{response.statusMessage}`") return result = JSON.parse(body) - robot.send { room: @destination }, "Created a new build! #{result.build_url}" + callback("Created a new build! #{result.build_url}") _assert = -> throw new Error('`owner` is required argument') unless @owner? diff --git a/scripts/ci.coffee b/scripts/ci.coffee index bdef1fa..e0c5598 100644 --- a/scripts/ci.coffee +++ b/scripts/ci.coffee @@ -13,6 +13,6 @@ module.exports = (robot) -> owner: 'yulii' job: msg.match[1] project: msg.match[2] - ).notify("##{msg.envelope.room}").execute(robot) + ).execute(robot, (message) -> msg.send(message)) catch error msg.send "#{error.name}: #{error.message}" diff --git a/scripts/webhook.coffee b/scripts/webhook.coffee index 816efac..90d251e 100644 --- a/scripts/webhook.coffee +++ b/scripts/webhook.coffee @@ -25,10 +25,10 @@ module.exports = (robot) -> ['hubot-scripts', 'yulii.github.io'].forEach (name) -> try new CircleCI( - owner: 'yulii' - project: name - job: 'update' - ).notify('#devops').execute(robot) + owner: 'yulii', project: name, job: 'update' + ).execute(robot, (message) -> + robot.send { room: '#devops' }, message + ) catch error robot.send { room: '#devops' }, "#{error.name}: #{error.message}" diff --git a/test/lib/alpha_vantage.coffee b/test/lib/alpha_vantage.coffee index 3cfaa3e..abc8864 100644 --- a/test/lib/alpha_vantage.coffee +++ b/test/lib/alpha_vantage.coffee @@ -39,15 +39,15 @@ describe 'AlphaVantage', -> describe 'when response is successful', -> it 'calls callback with a message object ', () -> - http_stub((f) -> f('error', 'response', helper.fixture.time_series_daily)) + http_stub((f) -> f(null, 'response', helper.fixture.time_series_daily)) new AlphaVantage(function: 'FUNCTION', symbol: 'SYMBOL').execute(robot, (message) -> expect(message).to.be.an.instanceof(AlphaVantageSlackMessage) ) describe 'when response is failure', -> - it 'calls callback with a message object ', () -> - http_stub((f) -> f('error', 'response', helper.fixture.error_message)) + it 'calls callback with a message object', () -> + http_stub((f) -> f(null, 'response', helper.fixture.error_message)) new AlphaVantage(function: 'FUNCTION', symbol: 'SYMBOL').execute(robot, (message) -> expect(message).to.be.an.instanceof(AlphaVantageErrorMessage) diff --git a/test/lib/circleci.coffee b/test/lib/circleci.coffee index 264c3d0..b622edd 100644 --- a/test/lib/circleci.coffee +++ b/test/lib/circleci.coffee @@ -1,6 +1,8 @@ helper = require('../test_helper') expect = require('chai').expect +sinon = require('sinon') +Robot = require('hubot/src/robot') CircleCI = helper.require('circleci') describe 'CircleCI', -> @@ -60,8 +62,36 @@ describe 'CircleCI', -> ci = new CircleCI(owner: 'yulii', job: 'test', project: 'mocha', token: 'circle-token') expect(ci.endpoint()).to.equal('https://circleci.com/api/v1.1/project/github/yulii/mocha/tree/master') - describe '#notify', -> - it 'sets `destination` property', () -> - ci = new CircleCI(owner: 'yulii', job: 'test', project: 'mocha', token: 'circle-token').notify('address') - expect(ci).to.be.an.instanceof(CircleCI) - expect(ci).to.have.property('destination', 'address') + describe '#execute', -> + robot = undefined + http_stub = undefined + + beforeEach -> + process.env.CIRCLE_TOKEN_EXECUTE = 'circle-project-token' + robot = new Robot(null, 'mock-adapter', false, 'hubot') + http_stub = (callback) -> + sinon.stub(robot, 'http').returns( + header: sinon.stub().returnsThis() + post: sinon.stub().callsFake((_) -> return callback) + ) + + afterEach -> + delete process.env.CIRCLE_TOKEN_EXECUTE + robot.http.restore() + + describe 'when response is successful', -> + it 'calls callback with a string ', () -> + http_stub((f) -> f(null, 'response', '{ "build_url": "https://build_url" }')) + + new CircleCI(owner: 'yulii', job: 'test', project: 'execute').execute(robot, (message) -> + expect(message).to.equal('Created a new build! https://build_url') + ) + + describe 'when response is failure', -> + it 'calls callback with a string', () -> + response = { statusCode: 404, statusMessage: 'Not Found' } + http_stub((f) -> f(null, response, 'body')) + + new CircleCI(owner: 'yulii', job: 'test', project: 'execute').execute(robot, (message) -> + expect(message).to.equal("Request fail :( `#{response.statusCode}: #{response.statusMessage}`") + )