/
githubot.coffee
58 lines (52 loc) · 1.79 KB
/
githubot.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
http = require "scoped-http-client"
querystring = require "querystring"
class Github
constructor: (@logger) ->
qualified_repo: (repo) ->
unless repo?
unless (repo = process.env.HUBOT_GITHUB_REPO)?
@logger.error "Default Github repo not specified"
return null
repo = repo.toLowerCase()
return repo unless repo.indexOf("/") is -1
unless (user = process.env.HUBOT_GITHUB_USER)?
@logger.error "Default Github user not specified"
return repo
"#{user}/#{repo}"
request: (verb, url, data, cb) ->
unless cb?
[cb, data] = [data, null]
url_api_base = process.env.HUBOT_GITHUB_API || "https://api.github.com"
if url[0..3] isnt "http"
url = "/#{url}" unless url[0] is "/"
url = "#{url_api_base}#{url}"
req = http.create(url).header("Accept", "application/vnd.github.beta+json")
req = req.header("Authorization", "token #{oauth_token}") if (oauth_token = process.env.HUBOT_GITHUB_TOKEN)?
req[verb.toLowerCase()](JSON.stringify data) (err, res, body) =>
data = null
if err?
@logger.error err
else unless (200 <= res.statusCode < 300)
@logger.error "#{res.statusCode} #{JSON.parse(body).message}"
else
data = JSON.parse body
cb data
get: (url, data, cb) ->
unless cb?
[cb, data] = [data, null]
if data?
url += "?" + querystring.stringify data
@request "GET", url, cb
post: (url, data, cb) ->
@request "POST", url, data, cb
branches: (repo, cb) ->
@get("https://api.github.com/repos/#{@qualified_repo repo}/branches", cb)
module.exports = github = (robot) ->
new Github robot.logger
github[method] = func for method,func of Github.prototype
github.logger = {
error: (msg) ->
util = require "util"
util.error "ERROR: #{msg}"
debug: ->
}