Skip to content

Instantly share code, notes, and snippets.

@Alliages
Created December 3, 2015 21:44
Show Gist options
  • Save Alliages/a7cbe5b9c7a630408dcc to your computer and use it in GitHub Desktop.
Save Alliages/a7cbe5b9c7a630408dcc to your computer and use it in GitHub Desktop.
Github_check python script for Grasshopper : a component that request github server to get the last commit's comment for a specific file
#
# Github_check : a component that request github server to get the last commit's comment for a specific file
#
# This file is part Elioth toolbox
#
# Copyright (c) 2015, Guillaume Meunier <alliages@gmail.com>
# Github_check is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3 of the License,
# or (at your option) any later version.
#
# Github_check is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Github_check; If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
#------------------------------
# INFORMATION
# API rate limit can be exceeded, but here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.
# https://developer.github.com/v3/#rate-limiting
#------------------------------
"""
This component request github server to get the last commit's comment for a specific file
-
Provided by Guillaume Meunier
Args:
github [STR] : name of the github repository to request USERNAME/REPO_NAME
script [STR]: name of the script you re looking for
check [BOOL]: do the check?
authorization [BOOL] : does it need authentification?
token [STR]: the token for authorization
Returns:
version: the commit's comment
"""
ghenv.Component.Name = "Check_Github"
ghenv.Component.NickName = 'checkgithub'
ghenv.Component.Message = 'VER 0.1\nNOV_2015'
ghenv.Component.Category = "Elioth"
ghenv.Component.SubCategory = "Basic"
try: ghenv.Component.AdditionalHelpFromDocStrings = "1"
except: pass
import System
import json
import Grasshopper.Kernel as gh
def github_connect(address, authToken):
r = System.Net.WebRequest.Create(address)
if authorization:
r.Headers.Add("Authorization", authToken)
r.AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
r.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"
try:
resp = r.GetResponse()
except Exception, e:
msg_e = "\nError in github get fonction\nwrong username?\nwrong repo?\ntry with authorization?\nAPI rate limit exceeded?\ncheck : "+address
print `e` + msg_e
ghenv.Component.AddRuntimeMessage(gh.GH_RuntimeMessageLevel.Warning, msg_e)
return
respS = resp.GetResponseStream()
sr = System.IO.StreamReader(respS, System.Text.Encoding.GetEncoding(1252))
jsonR = sr.ReadToEnd()
sr.Close()
resp.Close()
return jsonR
def main():
if authorization:
if token == None:
return "Need a token!",False
authToken = "token "+token
else:
authToken=""
if script == None:
return "You need a script name", False
jsonR_commits = github_connect(address, authToken)
if jsonR_commits == None:
return "",False
data_commits = json.loads(jsonR_commits.ToString())
for i in data_commits:
if not (i['url'].ToString() == None):
jsonR_file = github_connect(i['url'].ToString(), authToken)
data_file = json.loads(jsonR_file.ToString())
nom = data_file["files"][0]["filename"]
if script == nom:
message = data_file["commit"]["message"]
version = message
return message, True
if check :
if github == None:
msg_e = "You need a repository like USERNAME/REPO_NAME"
print msg_e
ghenv.Component.AddRuntimeMessage(gh.GH_RuntimeMessageLevel.Warning, msg_e)
else:
if github[-1] == "/":
github = github[:-1]
if github[0] == "/":
github = github[1:]
address = "https://api.github.com/repos/" + github + "/commits"
msg, success = main()
if not success:
ghenv.Component.AddRuntimeMessage(gh.GH_RuntimeMessageLevel.Warning, msg)
print msg
else:
version = msg
print "Done!"
else:
print " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment