This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgithub.lua
More file actions
312 lines (279 loc) · 9.72 KB
/
Copy pathgithub.lua
File metadata and controls
312 lines (279 loc) · 9.72 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
-- @module github
-- Copyright (C) 2014 John E. Vincent (lusis)
-- TODO
-- * clean up pagination logic. it's ugly as fuck and weaksauce
local cjson = require 'cjson'
local parsers = require 'github.parsers'
local _M = {}
local m = {}
_M.VERSION = "0.0.1"
local defaults = {
api_url = "https://api.github.com",
oauth_url = "https://github.com/login/oauth",
authorize_base_url = "https://github.com/login/oauth/authorize",
access_base_url = "https://github.com/login/oauth/access_token"
}
local function merge_tables(t1, t2)
for k,v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
function m.new(params)
local self = {}
local args = params or {}
self.access_token = nil
if not args.httpclient_driver then
self.hc = require('httpclient').new()
else
self.hc = require('httpclient').new(args.httpclient_driver)
end
if args.access_token then
self.access_token = args.access_token
end
self.user_agent = args.user_agent or "lua-github ".._M.VERSION
setmetatable(self, {__index = _M})
return self
end
function _M:set_access_token(token)
self.access_token = token
end
function _M:get_access_token()
return self.access_token
end
function _M:unauthed_request(path, args, buffer)
if not path then
return nil, "no path specified"
end
local args = args or {}
local page_buffer = buffer or {}
local page_size = args.page_size or 30
local url = defaults.api_url..path.."?per_page="..page_size
local res = self.hc:get(url,{headers = {accept = "application/json", ["content-type"] = "application/json", ["user-agent"] = self.user_agent}})
if args.raw then
return res, nil
else
if res.err then
return nil, res.err
else
if res.headers["link"] then
-- we have pagination
local links = parsers.parse_link_header(res.headers["link"])
if links["next"] then
page_buffer = merge_tables(page_buffer, cjson.decode(res.body))
local u = self.hc:urlparse(links["next"])
local next_page = u.path.."?page="..u.query.page
return self:authed_request(next_page, args or {}, page_buffer)
else
-- on last page
page_buffer = merge_tables(page_buffer, cjson.decode(res.body))
return page_buffer, nil
end
else
return cjson.decode(res.body), nil
end
end
end
end
function _M:authed_request(path, args, buffer)
if not path then
return nil, "no path specified"
end
local token = self:get_access_token()
if not token then
return nil, "no access token"
end
local args = args or {}
local page_buffer = buffer or {}
local args = args or {}
local page_size = args.page_size or 30
local url = defaults.api_url..path.."?per_page="..page_size.."&access_token="..token
local res = self.hc:get(url,{headers = {accept = "application/json", ["content-type"] = "application/json", ["user-agent"] = self.user_agent}})
if args.raw then
return res, nil
else
if res.err then
return nil, res.err
else
if res.headers["link"] then
-- we have pagination
local links = parsers.parse_link_header(res.headers["link"])
if links["next"] then
page_buffer = merge_tables(page_buffer, cjson.decode(res.body))
local u = self.hc:urlparse(links["next"])
local next_page = u.path.."?page="..u.query.page
return self:authed_request(next_page, args or {}, page_buffer)
else
-- on last page
page_buffer = merge_tables(page_buffer, cjson.decode(res.body))
return page_buffer, nil
end
else
return cjson.decode(res.body), nil
end
end
end
end
-- returns a url that can be used for oauth redirection
function _M:get_authorize_url(client_id, scope)
if not client_id or not scope then
return nil, "missing a require param: client_id or scope"
else
return defaults.authorize_base_url.."?client_id="..client_id.."&scope="..scope, nil
end
end
-- makes a request to github for an access token
-- based on an oauth callback
function _M:request_token(callback_code, args)
if not callback_code then
return nil, "this requires a callback code from github"
end
if not args.client_id or not args.client_secret or not args.redirect_uri then
return nil, "missing one of the required params: client_id, client_secret or callback url"
end
local params = {
access_token_url = defaults.access_base_url,
client_id = args.client_id,
client_secret = args.client_secret,
code = callback_code,
redirect_uri = args.redirect_uri
}
local opts = {
headers = { accept = "application/json" },
params = params,
}
local res = self.hc:post(defaults.access_base_url, "", opts)
if res.err then
return nil, res.err
else
local b = cjson.decode(res.body)
return b.access_token, nil
end
end
-- returns details for the requested user
function _M:get_user(username)
if not username then return nil, "must specificy username" end
return self:unauthed_request("/users/"..username)
end
-- returns details for the authenticated user
function _M:get_authenticated_user()
return self:authed_request("/user")
end
-- returns the followers for the requested user
function _M:get_user_followers(username)
if not username then return nil, "must specify username" end
return self:unauthed_request("/users/"..username.."/followers")
end
-- returns the followers for the authenticated user
function _M:get_authenticated_user_followers()
return self:authed_request("/user/followers")
end
-- returns the teams for the authenticated user
function _M:get_authenticated_user_teams()
return self:authed_request("/user/teams")
end
-- returns the users followed by the requested user
function _M:get_user_following(username)
if not username then return nil, "must specify username" end
return self:unauthed_request("/users/"..username.."/following")
end
-- returns the users followed by the authenticated user
function _M:get_authenticated_user_following()
return self:authed_request("/user/following")
end
-- returns the verified public keys for the requested user
function _M:get_user_pubkeys(username)
if not username then return nil, "must specify username" end
return self:unauthed_request("/users/"..username.."/keys")
end
-- returns the keys for the authenticated user
function _M:get_authenticated_user_pubkeys(username)
return self:authed_request("/user/keys")
end
-- gets a single key for the authenticated user
function _M:get_authenticated_user_key(key_id)
if not key_id then return nil, "must specify key id" end
return self:authed_request("/user/keys/"..key_id)
end
function _M:get_org(org_name)
if not org_name then return nil, "must specify org name" end
res, err = self:unauthed_request("/orgs/"..org_name, {raw = true})
if res.code ~= 200 then
return nil, res.err
else
return cjson.decode(res.body), nil
end
end
function _M:get_authed_org(org_name)
if not org_name then return nil, "must specify org name" end
res, err = self:authed_request("/orgs/"..org_name, {raw = true})
if res.code ~= 200 then
return nil, res.err
else
return cjson.decode(res.body), nil
end
end
-- gets an organization's public members
function _M:get_org_members(org_name)
if not org_name then return nil, "must specify org name" end
return self:unauthed_request("/orgs/"..org_name.."/members")
end
-- returns org members as visible to the authenticated user
function _M:get_authed_org_members(org_name)
if not org_name then return nil, "must specify org name" end
return self:authed_request("/orgs/"..org_name.."/members")
end
-- check if a user is publicly a member of an organization
function _M:get_user_org_membership(org_name, username)
if not org_name then return nil, "must specify org name" end
if not username then return nil, "must specify username" end
local res,_ = self:unauthed_request("/orgs/"..org_name.."/public_members/"..username, {raw = true})
if res.code == 404 then return false, nil end
if res.code == 204 then return true, nil end
return nil, res.err
end
-- check if a user is publicly or privately a member of an organization
function _M:get_authed_user_org_membership(org_name, username)
if not org_name then return nil, "must specify org name" end
if not username then return nil, "must specify username" end
local res,_ = self:authed_request("/orgs/"..org_name.."/members/"..string.lower(username), {raw = true})
if res.code == 404 then return false, nil end
if res.code == 204 then return true, nil end
return nil, res.err
end
-- returns teams for given org (authed request only)
-- From the github api page:
-- All actions against teams require at a minimum an authenticated user who is a member of the Owners team in the :org being managed.
-- Additionally, OAuth users require the “read:org” scope.
function _M:get_org_teams(org_name)
if not org_name then return nil, "must specify org name" end
local res, err = self:authed_request("/orgs/"..org_name.."/teams", {raw = true})
if res.code == 403 then
return nil, res.err
else
return cjson.decode(res.body), nil
end
end
-- helper: is user in org team (uses current user's token)
function _M:current_user_in_org_team(team_name, org_name)
if not team_name then return nil, "must specify team name" end
if not org_name then return nil, "must specify org name" end
local match = false
local user_teams, err = self:get_authenticated_user_teams()
if not user_teams then
return false, err
else
for _, team in pairs(user_teams) do
if string.lower(team.organization.login) == string.lower(org_name) then
if string.lower(team.slug) == string.lower(team_name) then
match = true
break
else
match = false
end
end
end
end
return match, nil
end
return m