Skip to content

Instantly share code, notes, and snippets.

@opn
Last active April 10, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opn/9371fbef641bbc7bbc5efce510224667 to your computer and use it in GitHub Desktop.
Save opn/9371fbef641bbc7bbc5efce510224667 to your computer and use it in GitHub Desktop.
This stack contains all the handlers that are specific to Himalayan Academy site structure.
--> MetaData
-
copyright: David Bovill
license: GPLv3
name: lib_GitHub
type: script library
version: 0.3
/*
A library for interfacing with GitHub
*/
--> GitHub | Gist
-
command gist_SaveHkey hKeys, @pShortGistFile, pGistHelp, pIsPublic
put line 1 of hKeys into hKey
if pGistHelp is empty then
put _FetchAndAskForHkeyHelp (hKey) into pGistHelp
end if
_tidyGistHelp pGistHelp
put hkey_CollectFromObjects (hKeys) into someScript
put gist_ShortFileFromHkey (hKeys) into shortGistFile
put gist_FindInListArray (shortGistFile) into gistID
if gistID is empty then
gist_Create someScript, pGistHelp, shortGistFile, pIsPublic
else
gist_Edit gistID, shortGistFile, someScript, pGistHelp, pIsPublic
end if
put the result into someJSON
put json_ToArray (someJSON) into gistReplyArray
return gistReplyArray
end gist_SaveHkey
command gist_SaveObjectScript @scriptObject, pGistHelp, pIsPublic
if exists (scriptObject) is false then return ("error, object" && scriptObject && "does not exist!")
put the mobile_Name of scriptObject into scriptObject
if pGistHelp is empty then
put _FetchAndAskForScriptHelp (scriptObject) into pGistHelp
end if
_tidyGistHelp pGistHelp
put the script of scriptObject into someScript
put gist_ShortFileFromObject (scriptObject) into shortGistFile
put gist_FindInListArray (shortGistFile) into gistID
if gistID is empty then
gist_Create someScript, pGistHelp, shortGistFile, pIsPublic
else
gist_Edit gistID, shortGistFile, someScript, pGistHelp, pIsPublic
end if
put the result into someJSON
put json_ToArray (someJSON) into someArray
return someArray
end gist_SaveObjectScript
private function _FetchAndAskForScriptHelp scriptObject
put the script_Help of scriptObject into pDescription
_tidyGistHelp pDescription
put the mobile_Name of scriptObject into scriptObject
put merge ("Give a description for the script of [[scriptObject]]...") into someQuestion
put opn_Ask (someQuestion, pDescription) into pDescription
if the shiftkey is "down" or the optionkey is "Down" then
answer "Do you want to store this new script description?" with "Cancel" or "OK"
if it is "OK" then
set the script_Help of scriptObject to pDescription
end if
end if
return pDescription
end _FetchAndAskForScriptHelp
private function _FetchAndAskForHkeyHelp hKey
put hkey_FetchHelp (hKey) into pDescription
_tidyGistHelp pDescription
put merge ("Give a description for handler '[[item 1 to 2 of hKey]]'...") into someQuestion
replace "'" with quote in someQuestion
put opn_Ask (someQuestion, pDescription) into pDescription
if the shiftkey is "down" or the optionkey is "Down" then
answer "Do you want to store this new handler description?" with "Cancel" or "OK"
if it is "OK" then
hkey_StoreHelp hKey, pDescription
end if
end if
return pDescription
end _FetchAndAskForHkeyHelp
private command _tidyGistHelp @gistHelp
put line 1 of gistHelp into gistHelp
put html_StripAllTags (gistHelp) into gistHelp
end _tidyGistHelp
function gist_ShortFileFromHkey hKeys
put line 1 of hKeys into hKey
put "handler" & comma & handler_ConvertType (item 2 of hKey) && word 1 of item 1 of hKey into shortGistFile
return shortGistFile
end gist_ShortFileFromHkey
function gist_ShortFileFromObject scriptObject
replace quote with "'" in scriptObject
put "script," & word 1 to -1 of scriptObject into shortGistFile
return shortGistFile
end gist_ShortFileFromObject
--> Gist | List
-
function gist_List
put gist_FetchListArray() into gistListArray
put gist_ParseListArray (gistListArray) into gistList
return gistList
end gist_List
function gist_FindInListArray shortGistFile, pGistListArray
if pGistListArray is not an array then
put gist_FetchListArray() into pGistListArray
end if
repeat for each key indexNum in pGistListArray
put pGistListArray [indexNum]["files"] into gistFileArray
put keys (gistFileArray) into gistFiles
if shortGistFile is among the lines of gistFiles then
put pGistListArray [indexNum]["id"] into gistID
return gistID
end if
end repeat
return empty
end gist_FindInListArray
function gist_ParseListArray gistListArray
repeat for each key indexNum in gistListArray
put gistListArray [indexNum]["description"] into gistDescription
put gistListArray [indexNum]["files"] into gistFileArray
put keys (gistFileArray) into gistFiles
put gistFiles & CR after gistList
end repeat
delete char -1 of gistList
return gistList
end gist_ParseListArray
function gist_FetchListArray
-- put "/users/:username/gists"
-- put url "GET /users/:username/gists"
put gist_FetchListJson () into someJSON
put json_ToArray (someJSON) into gistReplyArray
return gistReplyArray
end gist_FetchListArray
function gist_FetchListJson
-- put "/users/:username/gists"
-- put url "GET /users/:username/gists"
put _fetchFromGitHub ("/gists") into someJSON
return someJSON
end gist_FetchListJson
--> Gist | Content
-
function git_FetchContent gistID
put gist_FetchArray (gistID) into gistFileArray
put gist_ExtractContent (gistFileArray) into gistContent
return gistContent
end git_FetchContent
function gist_ExtractContent gistFileArray, pShortGistFile
gist_DeconstructFileArray gistFileArray, pShortGistFile, gistContent, gistDescription, htmlURL, gitPulllURL
return gistContent
end gist_ExtractContent
command gist_DeconstructFileArray gistFileArray, @pShortGistFile, @gistContent, @gistDescription, @htmlURL, @gitPulllURL
if pShortGistFile is empty then
put gistFileArray ["files"] into fileSectionArray
put line 1 of keys (fileSectionArray) into pShortGistFile
end if
put gistFileArray ["files"][pShortGistFile]["content"] into gistContent
put gistFileArray ["description"] into gistDescription
put gistFileArray ["html_url"] into htmlURL
put gistFileArray ["git_pull_url"] into gitPulllURL
put gistFileArray ["html_url"] into gistHistoryArray
return gistHistoryArray
end gist_DeconstructFileArray
function gist_FetchArray gistID
put gist_FetchJson (gistID) into someJSON
put json_ToArray (someJSON) into someArray
return someArray
end gist_FetchArray
command gist_Edit gistID, shortGistFile, pSomeText, pDescription, pIsPublic
-- PATCH /gists/:id
-- but you can use a POST
put "/gists/" & gistID into restPath
put gist_ConstructEditJSON (shortGistFile, pSomeText, pDescription, pIsPublic) into postJSON
_patchGitHub restPath, postJSON
put the result into someJSON
return someJSON
end gist_Edit
function gist_ConstructEditJSON shortGistFile, pSomeText, pDescription, pIsPublic
/*
This construct edit JSON to post:
{
"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
}
}
*/
put pIsPublic is not false into pIsPublic
if pSomeText is not empty then put pSomeText into postArray ["content"]
put json_FromArray (postArray) into innerJSON
if pDescription is not empty then put "{" & kwote("description") & ":" & kwote (pDescription) into postJSON
put comma & kwote ("public") & ":" & pIsPublic after postJSON
put comma & kwote ("files") & ":{" & kwote (shortGistFile) after postJSON
put ":" & innerJSON after postJSON
put "}}" after postJSON
return postJSON
end gist_ConstructEditJSON
command gist_Delete gistID
-- this is slow
-- DELETE /gists/:id
put "/gists/" & gistID into restPath
_deleteFromGitHub restPath
put the result into someJSON
return someJSON
end gist_Delete
function gist_FetchJson gistID
-- GET /gists/:id
put "/gists/" & gistID into restPath
put _fetchFromGitHub (restPath) into someJSON
return someJSON
end gist_FetchJson
command gist_Create someText, someDescription, shortGistFile, pIsPublic
-- POST /gists
-- {"description": "the description for this gist","public": true,"files": {"file1.txt": {"content": "String file contents"}}}
put pIsPublic is not false into pIsPublic
put someText into postArray ["content"]
put json_FromArray (postArray) into innerJSON
put "{" & kwote("description") & ":" & kwote(someDescription) into postJSON
put comma & kwote("public") & ":" & pIsPublic after postJSON
put comma & kwote("files") & ":{" & kwote(shortGistFile) after postJSON
put ":" & innerJSON after postJSON
put "}}" after postJSON
_postToGitHub "/gists", postJSON
put the result into jsonResult
return jsonResult
end gist_Create
--> GitHub | Issues
-
command github_CreateIssue repoOwner, someRepo, postJSON
-- POST /repos/:owner/:repo/issues
put _issueCommandPath (repoOwner, someRepo, "issues") into restPath
_postToGitHub restPath, postJSON
return the result
end github_CreateIssue
function github_FetchIssue repoOwner, someRepo, issueNum
-- GET /repos/:owner/:repo/issues/:number
put "issues/" & issueNum into issueString
put _issueCommandPath (repoOwner, someRepo, issueString) into restPath
put _fetchFromGitHub (restPath) into someJSON
return someJSON
end github_FetchIssue
function github_ListRepoIssues repoOwner, someRepo
-- help: https://developer.github.com/v3/issues/#list-issues
-- GET /repos/:owner/:repo/issues
put _issueCommandPath (repoOwner, someRepo, "issues") into restPath
put _fetchFromGitHub (restPath) into someJSON
return someJSON
end github_ListRepoIssues
function github_ListAllMyIssues
-- help: https://developer.github.com/v3/issues/#list-issues
-- List all issues across all the authenticated user's visible repositories including owned repositories, member repositories, and organization repositories:
put _fetchFromGitHub ("/issues") into someData
return someData
end github_ListAllMyIssues
function github_ListMyIssues
-- help: https://developer.github.com/v3/issues/#list-issues
-- List all issues across owned and member repositories for the authenticated user:
put _fetchFromGitHub ("/user/issues") into someJSON
return someJSON
end github_ListMyIssues
function github_ListOrgIssues someOrg
-- help: https://developer.github.com/v3/issues/#list-issues
-- put "co-operating-systems" into someOrg
put "/orgs/" & someOrg & "/issues" into orgBit
put _fetchFromGitHub (orgBit) into someJSON
return someJSON
end github_ListOrgIssues
--> GitHub | Repos
-
function github_ListRepos
put _fetchFromGitHub ("/orgs/opn/repos") into someData
return someData
end github_ListRepos
--> Deps
-
command text_EscapeReturns @someText
/*
\r = CR (Carriage Return) // Used as a new line character in Mac OS before X
\n = LF (Line Feed) // Used as a new line character in Unix/Mac OS X
\r\n = CR + LF // Used as a new line character in Windows
*/
-- replace CRLF with "\r\n" in someText
replace CR with "\r" in someText
replace LF with "\n" in someText
end text_EscapeReturns
function json_EncodeText someText
text_EscapeReturns someText
xml_Escape someText
put text_UTF8
end json_EncodeText
--> Private
-
private function _constructIssueJSON someTitle, pBody, pAssignee, pMilestone, pLabelArray
put someTitle into jsonArray ["title"]
if pBody is not empty then
put pBody into jsonArray ["body"]
end if
if pAssignee is not empty then put pAssignee into jsonArray ["assignee"]
if pMilestone is not empty then put pMilestone into jsonArray ["milestone"]
if pLabelArray is an array then
put pLabelArray into jsonArray ["labels"]
else if pLabelArray is not empty then
replace CR with comma in pLabelArray
split pLabelArray with comma
put pLabelArray into jsonArray ["labels"]
end if
put json_FromArray (jsonArray) into someJSON
return someJSON
end _constructIssueJSON
private function _constructIssueJSON someTitle, pBody, pAssignee, pMilestone, pLabelArray
put someTitle into jsonArray ["title"]
if pBody is not empty then put pBody into jsonArray ["body"]
if pAssignee is not empty then put pAssignee into jsonArray ["assignee"]
if pMilestone is not empty then put pMilestone into jsonArray ["milestone"]
if pLabelArray is an array then
put pLabelArray into jsonArray ["labels"]
else if pLabelArray is not empty then
replace CR with comma in pLabelArray
split pLabelArray with comma
put pLabelArray into jsonArray ["labels"]
end if
put json_FromArray (jsonArray) into someJSON
return someJSON
end _constructIssueJSON
private function _issueCommandPath repoOwner, someRepo, issueString
put "/repos/" & repoOwner & "/" & someRepo & "/" & issueString into restPath
return restPath
end _issueCommandPath
--> Private | REST
-
private function _constructApiUrl restPath
local GitHubApiStem = "https://api.github.com"
put GitHubApiStem into someUrl
if char 1 of restPath is not "/" then put "/" after someUrl
put restPath after someUrl
return someUrl
end _constructApiUrl
private command _patchGitHub restPath, postJSON
_setAuthHeaders
put _constructApiUrl (restPath) into someUrl
-- patch url someUrl
post postJSON to url someUrl
put it into postResult
return postResult
end _patchGitHub
private command _deleteFromGitHub restPath
-- this is slow
_setAuthHeaders
put _constructApiUrl (restPath) into someUrl
delete url someUrl
return the result
end _deleteFromGitHub
private function _fetchFromGitHub restPath
_setAuthHeaders
put _constructApiUrl (restPath) into someUrl
put url someUrl into someData
return someData
end _fetchFromGitHub
private command _postToGitHub restPath, postJSON
_setAuthHeaders
put _constructApiUrl (restPath) into someUrl
post postJSON to url someUrl
put it into postResult
return postResult
end _postToGitHub
private command _setAuthHeaders
-- put pref_FetchValue ("GitHubApiToken", "Default") into gitHubApiToken
put pref_GetValue ("GitHubApiToken") into gitHubApiToken
put "Accept: application/vnd.github.v3+json" into someHeader
put CR & "Authorization: token" && gitHubApiToken after someHeader
put CR & "User-Agent: OPN" after someHeader
set the httpheaders to someHeader
end _setAuthHeaders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment