 #   jingweno
 
 module.exports = (robot) ->
+
   github = require("githubot")(robot)
+
   robot.respond /show\s+(me\s+)?(.*)\s+pulls(\s+with\s+)?(.*)?/i, (msg)->
     repo = github.qualified_repo msg.match[2]
     filter_reg_exp = new RegExp(msg.match[4], "i") if msg.match[3]
     unless (url_api_base = process.env.HUBOT_GITHUB_API)?
-        url_api_base = "https://api.github.com"
+      url_api_base = "https://api.github.com"
 
     github.get "#{url_api_base}/repos/#{repo}/pulls", (pulls) ->
       if pulls.length == 0
-          msg.send "Achievement unlocked: open pull requests zero!"
+        msg.send "Achievement unlocked: open pull requests zero!"
       else
         filtered_result = []
         for pull in pulls

# Description:
#   Show open pull requests from a Github repository
#
# Dependencies:
#   "githubot": "0.2.0"


        for pull in filtered_result
          msg.send "\t#{pull.title} - #{pull.user.login}: #{pull.html_url}"

  robot.respond /show\s+(me\s+)?org pulls/i, (msg) ->
    unless (process.env.HUBOT_GITHUB_ORG)
      msg.send "No organization specified, please set HUBOT_GITHUB_ORG accordingly."
      return

    msg.send "I'll check for any open pull request within #{process.env.HUBOT_GITHUB_ORG}, just a sec..."

    unless (url_api_base = process.env.HUBOT_GITHUB_API)?
      url_api_base = "https://api.github.com"

    url = "#{url_api_base}/orgs/#{process.env.HUBOT_GITHUB_ORG}/issues?filter=all"
    github.get url, (issues) ->
      if issues.length == 0
        summary = "Achievement unlocked: open pull requests zero!"
      else
        filtered_result = []
        for issue in issues
          if issue.pull_request.html_url == null
            continue
          filtered_result.push(issue)

        if filtered_result.length == 0
          summary = "Achievement unlocked: open pull requests zero!"
        else if filtered_result.length == 1
          summary = "There's only one open pull request:"
        else
          summary = "I got #{filtered_result.length} open pull requests for you:"

        msg.send summary

        for issue in filtered_result
          msg.send " #{issue.repository.name}: #{issue.title} (#{issue.user.login}) -> #{issue.pull_request.html_url}"
