Skip to content

Commit

Permalink
Refactoring method
Browse files Browse the repository at this point in the history
  • Loading branch information
yulii committed Aug 24, 2017
1 parent 6e7c7b0 commit 7bf6fb8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
9 changes: 2 additions & 7 deletions lib/circleci.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ class CircleCI
_assert.call @

tokenEnvName: ->
return unless @project?
_tokenEnvName = "CIRCLE_TOKEN_#{@project.replace(/[-.]/gi, '_').toUpperCase()}" unless _tokenEnvName?
return _tokenEnvName

tokenEnvValue: ->
return unless @tokenEnvName?()
_tokenEnvValue = process.env[@tokenEnvName()] unless _tokenEnvValue?
return _tokenEnvValue

Expand Down Expand Up @@ -46,11 +44,8 @@ class CircleCI
robot.send { room: @destination }, "Request fail :( `#{response.statusCode}: #{response.statusMessage}`"
return

try
result = JSON.parse(body)
robot.send { room: @destination }, "Created a new build! #{result.build_url}"
catch error
console.error(error.message)
result = JSON.parse(body)
robot.send { room: @destination }, "Created a new build! #{result.build_url}"

_assert = ->
throw new Error('`owner` is required argument') unless @owner?
Expand Down
31 changes: 23 additions & 8 deletions test/lib/circleci.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ expect = require('chai').expect

CircleCI = require(source)
describe 'CircleCI', ->
_env = null

beforeEach ->
_env = JSON.parse(JSON.stringify(process.env))

afterEach ->
process.env = _env

describe '#new', ->
it 'return an instance with default values', () ->
Expand All @@ -22,6 +15,7 @@ describe 'CircleCI', ->
expect(ci).to.have.property('branch', 'master')
expect(ci).to.have.property('job', 'test')
expect(ci).to.have.property('token', 'circle-token-mocha')
delete process.env.CIRCLE_TOKEN_MOCHA

it 'return an instance', () ->
ci = new CircleCI(vcsType: 'vcs', owner: 'yulii', project: 'mocha', branch: 'test/lib', job: 'test', token: 'circle')
Expand All @@ -40,16 +34,37 @@ describe 'CircleCI', ->
expect(-> new CircleCI(owner: 'yulii', project: 'mocha', job: 'test' )).to.throw(Error, /Access token not found!/)

describe '#tokenEnvName', ->
beforeEach ->
process.env.CIRCLE_TOKEN_PROJECT_REPOSITORY_NAME = 'circle-project-token'

afterEach ->
delete process.env.CIRCLE_TOKEN_PROJECT_REPOSITORY_NAME

it 'return environment variable name', () ->
ci = new CircleCI(
owner: 'yulii'
job: 'test'
project: 'project.repository-name'
token: 'circle-token'
)

expect(ci.tokenEnvName()).to.equal('CIRCLE_TOKEN_PROJECT_REPOSITORY_NAME')

describe '#tokenEnvValue', ->
beforeEach ->
process.env.CIRCLE_TOKEN_PROJECT_REPOSITORY_NAME = 'circle-project-token'

afterEach ->
delete process.env.CIRCLE_TOKEN_PROJECT_REPOSITORY_NAME

it 'return environment variable', () ->
ci = new CircleCI(
owner: 'yulii'
job: 'test'
project: 'project.repository-name'
)

expect(ci.tokenEnvValue()).to.equal('circle-project-token')

describe '#endpoint', ->
it 'return endpoint url in CircleCI API', () ->
ci = new CircleCI(
Expand Down

0 comments on commit 7bf6fb8

Please sign in to comment.