Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
Trying more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
athieriot committed Jun 6, 2013
1 parent 21ed7ae commit 821c85d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 62 deletions.
7 changes: 5 additions & 2 deletions Gruntfile.coffee
Expand Up @@ -13,8 +13,11 @@ module.exports = (grunt) ->
NODE_ENV: 'test'

coffeelint:
app: [ '*.coffee', 'lib/**/*.coffee' ]

app: [ '*.coffee', 'lib/**/*.coffee' ],
options:
max_line_length:
value: 120

mochacli:
options:
reporter: 'spec',
Expand Down
2 changes: 1 addition & 1 deletion lib/configuration.coffee
Expand Up @@ -47,6 +47,6 @@ module.exports =
Hipchat: {
Rooms: hipchat.Rooms,
Channel: Nconf.get('hipchat').channel,
Frequency: Nconf.get('frequency')
Frequency: Nconf.get('hipchat').frequency
},
Version: JSON.parse(Fs.readFileSync('./package.json', 'utf8')).version
113 changes: 63 additions & 50 deletions lib/pulls.coffee
Expand Up @@ -60,37 +60,46 @@ getInfoPull = (org, reponame, number, callback) ->
repo: reponame,
number: number
}, (error, details) =>
Github.Api.statuses.get {
user: org,
repo: reponame,
sha: details.head.sha
}, (error, statuses) ->
callback error, {
title: details.title,
url: details.html_url,
infos: reponame,
comments: "(#{details.head.ref} -> #{details.base.ref}) - " +
Moment(details.created_at).fromNow() + " - " +
details.comments + " comments" +
needAttention(details.mergeable, details.state),
status: buildStatus(statuses),
avatar: details.user.gravatar_id,
order: details.created_at
}
if (error?)
callback(error)
else
Github.Api.statuses.get {
user: org,
repo: reponame,
sha: details.head.sha
}, (error, statuses) ->
if (error?)
callback(error)
else
callback null, {
title: details.title,
url: details.html_url,
infos: reponame,
comments: "(#{details.head.ref} -> #{details.base.ref}) - " +
Moment(details.created_at).fromNow() + " - " +
details.comments + " comments" +
needAttention(details.mergeable, details.state),
status: buildStatus(statuses),
avatar: details.user.gravatar_id,
order: details.created_at
}

displayError = (fallback, error) ->
Utils.fallback_print(fallback) { title: "An error occured: #{JSON.stringify(error)}", status: false }

githubUrlPattern = new RegExp "(http|https):\/\/github.com+([a-z0-9\-\.,@\?^=%&;:\/~\+#]*[a-z0-9\-@\?^=%&;\/~\+#])?",'i'

#TODO: Speeeeeeeeeeed
pulls = (fallback, keyword, filter) ->

githubUrlPattern = new RegExp "(http|https):\/\/github.com+
([a-z0-9\-\.,@\?^=%&;:\/~\+#]*[a-z0-9\-@\?^=%&;\/~\+#])?"
, 'i'

# First we verify if the argument is an URL
matching = keyword.match(githubUrlPattern) if _.isString(keyword)
if (matching and _.str.include(keyword, 'pull'))
pull = matching[2].split('\/')
getInfoPull pull[1], pull[2], pull[4], (error, result) ->
if (not error)
if (error?)
displayError(fallback, error)
else
Utils.fallback_print(fallback) {
title: result.title,
url: result.url,
Expand All @@ -99,39 +108,43 @@ pulls = (fallback, keyword, filter) ->
status: result.status,
avatar: result.avatar
}

else
Github.Api.repos.getFromOrg {
org: Github.Org,
per_page: 100
}, (error, repos) ->
Async.concat repos, (repo, callback) ->
if (isRepoInFilters(repo.name))
Github.Api.pullRequests.getAll {
user: Github.Org,
repo: repo.name
}, (error, prs) ->
if (error)
callback(error)
else
Async.reduce prs, [],
Async.apply (memo, pr, cb) ->
query = pr.title + repo.name + pr.user.login
if (shouldBeDisplayed(keyword, filter, query, pr.created_at))
getInfoPull Github.Org, repo.name, pr.number, (error, r) ->
memo.push r
cb null, memo
else
cb(error, memo)
, (err, list) ->
callback(err, list)
else
callback(null, [])
, (err, list) ->
if (err)
console.log "An error occured"
else
Utils.fallback_printList fallback,
list, _.partial(pickLastIfNeeded, keyword, filter)
if (error?)
displayError(fallback, error)
else
Async.concat repos, (repo, callback) ->
if (isRepoInFilters(repo.name))
Github.Api.pullRequests.getAll {
user: Github.Org,
repo: repo.name
}, (error, prs) ->
if (error?)
callback(error)
else
Async.reduce prs, [],
Async.apply (memo, pr, cb) ->
query = pr.title + repo.name + pr.user.login
if (shouldBeDisplayed(keyword, filter, query, pr.created_at))
getInfoPull Github.Org, repo.name, pr.number, (error, r) ->
memo.push r
cb error, memo
else
cb(error, memo)
, (err, list) ->
callback(err, list)
else
callback null, []
, (err, list) ->
if (err?)
displayError(fallback, err)
else
Utils.fallback_printList fallback,
list, _.partial(pickLastIfNeeded, keyword, filter)

module.exports = {
name: "Pull Requests"
Expand Down
6 changes: 4 additions & 2 deletions lib/server.coffee
Expand Up @@ -2,6 +2,7 @@ Configuration = require './configuration'
_ = require('underscore')._

Async = require 'async'
Moment = require 'moment'

Hipchat = Configuration.Hipchat
Commands = require './commands'
Expand Down Expand Up @@ -33,13 +34,13 @@ server = (frequency, testCallback) ->
freq = if _.isString(frequency) then frequency else Hipchat.Frequency

Hipchat.Rooms.history Hipchat.Channel, (error, lines) ->
if (error) then console.log(error)
if (error?) then console.log("An error occured while fetching history: #{JSON.stringify(error)}")
else if(lines)
Cache.store(lines.messages)

intervalId = setInterval () ->
Hipchat.Rooms.history Hipchat.Channel, (error, lines) ->
if (error) then console.log(error)
if (error?) then console.log("An error occured while fetching history: #{JSON.stringify(error)}")
else if (lines)
Async.each lines.messages, (line, cb) ->
if (not Cache.cached(line))
Expand All @@ -48,6 +49,7 @@ server = (frequency, testCallback) ->
if testCallback? and _.isFunction(testCallback)
testCallback(intervalId, command)
else
console.log("Command #{command.name} detected #{Moment().format()}")
_.partial(command.action, Utils.render).apply null, command.args
cb(null)
, (err) ->
Expand Down
22 changes: 16 additions & 6 deletions lib/utils.coffee
Expand Up @@ -2,6 +2,7 @@ Configuration = require './configuration'
_ = require('underscore')._

Hipchat = Configuration.Hipchat
Async = require 'async'
Styled = require 'styled'

status_icon = (status) ->
Expand All @@ -26,7 +27,8 @@ format_term = (title, url, infos, comments, status, avatar) ->
text += " - #{infos}" if infos?
text += " - #{comments}" if comments?
text


# TODO: Use templating
format_html = (title, url, infos, comments, status, avatar) ->
html = ""
html += "#{status_icon(status)} "
Expand All @@ -47,8 +49,9 @@ print = (o) ->
o['comments'],
o['status']
)
if (callback? and _.isFunction(callback)) then callback(null)

render = (o) ->
render = (o, callback) ->
Hipchat.Rooms.message Hipchat.Channel,
Configuration.Nickname,
format_html(
Expand All @@ -61,27 +64,34 @@ render = (o) ->
), {
message_format: "html",
color: status_color(o['status'])
}
}, (error) ->
if (callback? and _.isFunction(callback)) then callback(error)

fallback_print = (fallback) ->
if _.isFunction(fallback) then fallback else print
if fallback? and _.isFunction(fallback) then fallback else print

fallback_printList = (fallback, list, filter) ->
if (_.isEmpty(list))
fallback_print(fallback) { title: "No result for your request" }

if (_.every(list, (o) -> _.has(o, 'order')))
list = _.sortBy(list, 'order').reverse()

if (filter?)
list = filter list

_.each list, (item) ->
Async.eachSeries list, (item, callback) ->
fallback_print(fallback) {
title: item.title,
url: item.url,
infos: item.infos,
comments: item.comments,
status: item.status,
avatar: item.avatar
}
}, callback
, (error) ->
if (error?)
print { title: "An error occured while sending a message: #{JSON.stringify(error)}", status: false }

module.exports = {
format_term: format_term,
Expand Down
3 changes: 2 additions & 1 deletion test/commands/pulls.coffee
Expand Up @@ -115,7 +115,7 @@ describe 'Commands', () ->

it 'should list Pull Requests sorted by creation date', (done) ->
count = 0
Commands.pulls.action (object) ->
Commands.pulls.action (object, cb) ->
if (count is 0)
object.title.should.equal "new-feature"
object.url.should.equal "https://github.com/octocat/Hello-World/pulls/2"
Expand All @@ -124,6 +124,7 @@ describe 'Commands', () ->
object.status.should.equal true
count += 1
if (count is 2) then done()
cb()

it 'should be able to resolve an URL', (done) ->
Commands.pulls.action (object) ->
Expand Down
7 changes: 7 additions & 0 deletions test/utils.coffee
Expand Up @@ -51,3 +51,10 @@ describe 'Utils', () ->

Utils.render { title: "test message" }
nock.done()

describe '#fallback_printList()', () ->
it 'should handle an empty list', (done) ->
Utils.fallback_printList (object) ->
object.title.should.be.equal "No result for your request"
done()
, []

0 comments on commit 821c85d

Please sign in to comment.