Skip to content

Instantly share code, notes, and snippets.

@HyroVitalyProtago
Last active October 27, 2020 05:35
Show Gist options
  • Save HyroVitalyProtago/6296553 to your computer and use it in GitHub Desktop.
Save HyroVitalyProtago/6296553 to your computer and use it in GitHub Desktop.
Github API
-- GithubAPI
-- @Author : Hyro Vitaly Protago
-- @Version : 1.0.0
--[[
INFOS :
- Cannot delete an anonymous gist
]]--
GithubAPI = {
location = "https://api.github.com/",
token = nil,
OAuth = {
authorizations = {}
},
gist = {
list = {},
comment = {}
},
github = {}
}
----------------------------------------------------------------------------
------------------------------ Github API ----------------------------------
----------------------------------------------------------------------------
--- Authentication ---
--[[ Scopes --
Scopes let you specify exactly what type of access you need. Scopes limit access for OAuth tokens.
They do not grant any additional permission beyond that which the user already has.
For the web flow, requested scopes will be displayed to the user on the authorize form.
Check headers to see what OAuth scopes you have, and what the API action accepts.
~~~
$ curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/users/technoweenie -I
HTTP/1.1 200 OK
X-OAuth-Scopes: repo, user
X-Accepted-OAuth-Scopes: user
X-OAuth-Scopes lists the scopes your token has authorized. X-Accepted-OAuth-Scopes lists the scopes that the action checks for.
~~~
- (no scope)
public read-only access (includes public user profile info, public repo info, and gists).
- user
Read/write access to profile info only. Note: this scope includes user:email and user:follow.
- user:email
Read access to a user’s email addresses.
- user:follow
Access to follow or unfollow other users.
- public_repo
Read/write access to public repos and organizations.
- repo
Read/write access to public and private repos and organizations.
- repo:status
Read/write access to public and private repository commit statuses.
This scope is only necessary to grant other users or services access to private repository commit statuses without granting access to the code.
The repo and public_repo scopes already include access to commit status for private and public repositories respectively.
- delete_repo
Delete access to adminable repositories.
- notifications
Read access to a user’s notifications. repo is accepted too.
- gist
Write access to gists.
NOTE: Your application can request the scopes in the initial redirection. You can specify multiple scopes by separating them by a comma.
~~~
https://github.com/login/oauth/authorize?
client_id=...&
scope=user,public_repo
~~~
]]--
-- Redirect users to request GitHub access --
function GithubAPI.OAuth.getToken(client_id, scope, callback)
GithubAPI.http_request("https://github.com/login/oauth/authorize?client_id="..client_id.."&scope="..scope, function(data, status, headers)
GithubAPI.OAuth._getToken(client_id, client_secret, data, callback)
end, nil, true)
end
-- GitHub redirects back to your site --
function GithubAPI.OAuth._getToken(client_id, client_secret, code, callback)
GithubAPI.http_request("https://github.com/login/oauth/access_token", callback, {
method = "POST",
data = {
client_id = client_id,
client_secret = client_secret,
code = code
}
}, true)
end
-- List your authorizations --
function GithubAPI.OAuth.authorizations.list(callback)
GithubAPI.http_request("authorizations", callback)
end
--[[ Response --
Status: 200 OK
Link: <https://api.github.com/resource?page=2>; rel="next",
<https://api.github.com/resource?page=5>; rel="last"
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
[
{
"id": 1,
"url": "https://api.github.com/authorizations/1",
"scopes": [
"public_repo"
],
"token": "abc123",
"app": {
"url": "http://my-github-app.com",
"name": "my github app",
"client_id": "abcde12345fghij67890"
},
"note": "optional note",
"note_url": "http://optional/note/url",
"updated_at": "2011-09-06T20:39:23Z",
"created_at": "2011-09-06T17:26:27Z"
}
]
]]--
-- Get a single authorization --
function GithubAPI.OAuth.authorizations.get(id, callback)
GithubAPI.http_request("authorizations/"..id, callback)
end
--[[ Response --
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"id": 1,
"url": "https://api.github.com/authorizations/1",
"scopes": [
"public_repo"
],
"token": "abc123",
"app": {
"url": "http://my-github-app.com",
"name": "my github app",
"client_id": "abcde12345fghij67890"
},
"note": "optional note",
"note_url": "http://optional/note/url",
"updated_at": "2011-09-06T20:39:23Z",
"created_at": "2011-09-06T17:26:27Z"
}
]]--
-- Create a new authorization --
function GithubAPI.OAuth.authorizations.create(callback, scopes, note, note_url, client_id, client_secret)
GithubAPI.http_request("authorizations/", callback, {
method = "POST",
data = {
scopes = scopes,
note = note,
note_url = note_url,
client_id = client_id,
client_secret = client_secret
}
})
end
--[[ Input --
scopes
Optional array - A list of scopes that this authorization is in.
note
Optional string - A note to remind you what the OAuth token is for.
note_url
Optional string - A URL to remind you what app the OAuth token is for.
client_id
Optional String - The 20 character OAuth app client key for which to create the token.
client_secret
Optional String - The 40 character OAuth app client secret for which to create the token.
~~~
{
"scopes": [
"public_repo"
],
"note": "admin script"
}
~~~
]]--
--[[ Response --
Status: 201 Created
Location: https://api.github.com/authorizations/1
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"id": 1,
"url": "https://api.github.com/authorizations/1",
"scopes": [
"public_repo"
],
"token": "abc123",
"app": {
"url": "http://my-github-app.com",
"name": "my github app",
"client_id": "abcde12345fghij67890"
},
"note": "optional note",
"note_url": "http://optional/note/url",
"updated_at": "2011-09-06T20:39:23Z",
"created_at": "2011-09-06T17:26:27Z"
}
]]--
-- TODO
-- Update
-- Check
-- Delete
--- GISTS ---
-- List gists --
function GithubAPI.gist.list.user(user, callback)
GithubAPI.http_request("users/"..user.."/gists", callback)
end
function GithubAPI.gist.list.all(callback) -- return all public gists if called anonymously
GithubAPI.http_request("gists", callback)
end
function GithubAPI.gist.list.allPublic(callback)
GithubAPI.http_request("gists/public", callback)
end
function GithubAPI.gist.list.starred(callback)
GithubAPI.http_request("gists/starred", callback)
end
--[[ Response --
Status: 200 OK
Link: <https://api.github.com/resource?page=2>; rel="next",
<https://api.github.com/resource?page=5>; rel="last"
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
[
{
"url": "https://api.github.com/gists/88a3112be74ba6ad701e",
"id": "1",
"description": "description of gist",
"public": true,
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"files": {
"ring.erl": {
"size": 932,
"filename": "ring.erl",
"raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
}
},
"comments": 0,
"comments_url": "https://api.github.com/gists/8438e99468ee9a4ab10e/comments/",
"html_url": "https://gist.github.com/1",
"git_pull_url": "git://gist.github.com/1.git",
"git_push_url": "git@gist.github.com:1.git",
"created_at": "2010-04-14T02:15:15Z"
}
]
]]--
-- Get a single gist --
function GithubAPI.gist.get(id, callback)
GithubAPI.http_request("gists/"..id, callback)
end
--[[ Response --
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"url": "https://api.github.com/gists/88a3112be74ba6ad701e",
"id": "1",
"description": "description of gist",
"public": true,
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"files": {
"ring.erl": {
"size": 932,
"filename": "ring.erl",
"raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
}
},
"comments": 0,
"comments_url": "https://api.github.com/gists/8438e99468ee9a4ab10e/comments/",
"html_url": "https://gist.github.com/1",
"git_pull_url": "git://gist.github.com/1.git",
"git_push_url": "git@gist.github.com:1.git",
"created_at": "2010-04-14T02:15:15Z",
"forks": [
{
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"url": "https://api.github.com/gists/add0d71b065f55c46f60",
"created_at": "2011-04-14T16:00:49Z"
}
],
"history": [
{
"url": "https://api.github.com/gists/80bdb0d081c447600e18",
"version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"change_status": {
"deletions": 0,
"additions": 180,
"total": 180
},
"committed_at": "2010-04-14T02:15:15Z"
}
]
}
]]--
-- Create a gist --
function GithubAPI.gist.create(public, files, callback ,description)
GithubAPI.http_request("gists/"..id, callback, {
method = "POST",
data = {
public = public,
files = files,
description = description
}
})
end
--[[ Input --
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "String file contents"
}
}
}
]]--
--[[ Response --
Status: 201 Created
Location: https://api.github.com/gists/1
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"url": "https://api.github.com/gists/88a3112be74ba6ad701e",
"id": "1",
"description": "description of gist",
"public": true,
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"files": {
"ring.erl": {
"size": 932,
"filename": "ring.erl",
"raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
}
},
"comments": 0,
"comments_url": "https://api.github.com/gists/8438e99468ee9a4ab10e/comments/",
"html_url": "https://gist.github.com/1",
"git_pull_url": "git://gist.github.com/1.git",
"git_push_url": "git@gist.github.com:1.git",
"created_at": "2010-04-14T02:15:15Z",
"forks": [
{
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"url": "https://api.github.com/gists/add0d71b065f55c46f60",
"created_at": "2011-04-14T16:00:49Z"
}
],
"history": [
{
"url": "https://api.github.com/gists/80bdb0d081c447600e18",
"version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"change_status": {
"deletions": 0,
"additions": 180,
"total": 180
},
"committed_at": "2010-04-14T02:15:15Z"
}
]
}
]]--
-- Edit a gist --
function GithubAPI.gist.edit(id, files, callback, description)
GithubAPI.http_request("gists/"..id, callback, {
method = "PATCH",
data = {
id = id,
files = files,
description = description
}
})
end
--[[ Input --
{
"description": "the description for this gist",
"files": {
"file1.txt": {
"content": "updated file contents"
},
"old_name.txt": {
"filename": "new_name.txt",
"content": "modified contents"
},
"new_file.txt": {
"content": "a new file"
},
"delete_this_file.txt": null
}
}
]]--
--[[ Response --
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"url": "https://api.github.com/gists/88a3112be74ba6ad701e",
"id": "1",
"description": "description of gist",
"public": true,
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"files": {
"ring.erl": {
"size": 932,
"filename": "ring.erl",
"raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
}
},
"comments": 0,
"comments_url": "https://api.github.com/gists/8438e99468ee9a4ab10e/comments/",
"html_url": "https://gist.github.com/1",
"git_pull_url": "git://gist.github.com/1.git",
"git_push_url": "git@gist.github.com:1.git",
"created_at": "2010-04-14T02:15:15Z",
"forks": [
{
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"url": "https://api.github.com/gists/add0d71b065f55c46f60",
"created_at": "2011-04-14T16:00:49Z"
}
],
"history": [
{
"url": "https://api.github.com/gists/80bdb0d081c447600e18",
"version": "57a7f021a713b1c5a6a199b54cc514735d2d462f",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"change_status": {
"deletions": 0,
"additions": 180,
"total": 180
},
"committed_at": "2010-04-14T02:15:15Z"
}
]
}
]]--
-- Star a gist --
function GithubAPI.gist.star(id, callback)
GithubAPI.http_request("gists/"..id.."/star", callback, {method="PUT"})
end
--[[ Response --
Status: 204 No Content
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
]]--
-- Unstar a gist --
function GithubAPI.gist.unstar(id, callback)
GithubAPI.http_request("gists/"..id.."/star", callback, {method="DELETE"})
end
--[[ Response --
Status: 204 No Content
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
]]--
-- Check if a gist is starred --
function GithubAPI.gist.checkStar(id, callback)
GithubAPI.http_request("gists/"..id.."/star", callback)
end
--[[
-- Response if gist is starred --
Status: 204 No Content
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
-- Response if gist is not starred --
Status: 404 Not Found
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
]]--
-- Fork a gist --
function GithubAPI.gist.fork(id, callback)
GithubAPI.http_request("gists/"..id.."/forks", callback, {method="POST"})
end
--[[ Response --
Status: 201 Created
Location: https://api.github.com/gists/2
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"url": "https://api.github.com/gists/88a3112be74ba6ad701e",
"id": "1",
"description": "description of gist",
"public": true,
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"files": {
"ring.erl": {
"size": 932,
"filename": "ring.erl",
"raw_url": "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
}
},
"comments": 0,
"comments_url": "https://api.github.com/gists/8438e99468ee9a4ab10e/comments/",
"html_url": "https://gist.github.com/1",
"git_pull_url": "git://gist.github.com/1.git",
"git_push_url": "git@gist.github.com:1.git",
"created_at": "2010-04-14T02:15:15Z"
}
]]--
-- Delete a gist --
function GithubAPI.gist.delete(id, callback)
GithubAPI.http_request("gists/"..id, callback, {method="DELETE"})
end
--[[ Response --
Status: 204 No Content
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
]]--
--- GISTS COMMENTS ---
-- List comments on a gist --
function GithubAPI.gist.comment.list(gist_id, callback)
GithubAPI.http_request("gists/"..gist_id.."/comments", callback)
end
--[[ Response --
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
[
{
"id": 1,
"url": "https://api.github.com/gists/ae709e9cf889e485e65f/comments/1",
"body": "Just commenting for the sake of commenting",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"created_at": "2011-04-18T23:23:56Z"
}
]
]]--
-- Get a single comment --
function GithubAPI.gist.comment.get(gist_id, id, callback)
GithubAPI.http_request("gists/"..gist_id.."/comments/"..id, callback)
end
--[[ Response --
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"id": 1,
"url": "https://api.github.com/gists/ae709e9cf889e485e65f/comments/1",
"body": "Just commenting for the sake of commenting",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"created_at": "2011-04-18T23:23:56Z"
}
]]--
-- Create a comment --
function GithubAPI.gist.comment.create(gist_id, body, callback)
GithubAPI.http_request("gists/"..gist_id.."/comments", callback, {
method = "POST",
data = {
body = body
}
})
end
--[[ Input --
{
"body": "Just commenting for the sake of commenting"
}
]]--
--[[ Response --
Status: 201 Created
Location: https://api.github.com/gists/comments/1
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"id": 1,
"url": "https://api.github.com/gists/ae709e9cf889e485e65f/comments/1",
"body": "Just commenting for the sake of commenting",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"created_at": "2011-04-18T23:23:56Z"
}
]]--
-- Edit a comment --
function GithubAPI.gist.comment.edit(gist_id, id, body, callback)
GithubAPI.http_request("gists/"..gist_id.."/comments/"..id, callback, {
method = "PATCH",
data = {
body = body
}
})
end
--[[ Input --
{
"body": "Just commenting for the sake of commenting"
}
]]--
--[[ Response --
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
{
"id": 1,
"url": "https://api.github.com/gists/ae709e9cf889e485e65f/comments/1",
"body": "Just commenting for the sake of commenting",
"user": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "somehexcode",
"url": "https://api.github.com/users/octocat"
},
"created_at": "2011-04-18T23:23:56Z"
}
]]--
-- Delete a comment --
function GithubAPI.gist.comment.delete(gist_id, id, callback)
GithubAPI.http_request("gists/"..gist_id.."/comments/"..id, callback, {method = "DELETE"})
end
--[[ Response --
Status: 204 No Content
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
]]--
----------------------------------------------------------------------------
-------------------------------- TOOLS -------------------------------------
----------------------------------------------------------------------------
function GithubAPI.http_request(url, callback, opts, fullUrl)
opts = opts or {}
if GithubAPI.token then opts.headers = {Authorization = "Bearer " .. GithubAPI.token} end
if opts.data then opts.data = json.encode(opts.data) end
local _url
if (fullUrl) then _url = url else _url = GithubAPI.location .. url end
http.request(_url, function(data, status, headers)
if (status == 500) then error("Github: Internal Server Error ...") end
data = json.decode(data)
callback(data, status, headers)
end, alert, opts)
end
function GithubAPI.explode(div,str) -- credit: http://richard.warburton.it
if (div=='') then return false end
local pos,arr = 0,{}
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1))
pos = sp + 1
end
table.insert(arr,string.sub(str,pos))
return arr
end
-- GITHUB TIMESTAMP (YYYY-MM-DDTHH:MM:SSZ) to os.time
function GithubAPI.gtimestamp(githubTime)
githubTime = githubTime:sub(1, #githubTime-1) -- remove Z
githubTime = GithubAPI.explode("T", githubTime)
githubTime[1] = GithubAPI.explode("-", githubTime[1])
githubTime[2] = GithubAPI.explode(":", githubTime[2])
return os.time({
year = tonumber(githubTime[1][1]),
month = tonumber(githubTime[1][2]),
day = tonumber(githubTime[1][3]),
hour = tonumber(githubTime[2][1]),
min = tonumber(githubTime[2][2]),
sec = tonumber(githubTime[2][3])
})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment