Created
June 30, 2017 23:44
-
-
Save brunoborges/23ee695672a46835d5ec237f4dea3596 to your computer and use it in GitHub Desktop.
Nashorn script to validate a GitHub profile: check whether name, email, company and a custom avatar have been set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/jjs | |
if (arguments.length < 1) { | |
print("Usage: validate-github-profile.js user ..."); | |
exit(); | |
} | |
var github_oauth = JSON.parse(readFully("./github-oauth.json")); | |
var user_pass = "${github_oauth.user}:${github_oauth.token}"; | |
var github_api_url = "https://${user_pass}@api.github.com"; | |
function stringify(json) { | |
return JSON.stringify(json, null, ' '); | |
} | |
function gh_curl(cmd, endpoint) { | |
return curl(cmd, "${github_api_url}${endpoint}"); | |
} | |
function curl(cmd, url) { | |
return $EXEC("curl -sb -H \"Accept: application/json\" -X ${cmd} '${url}'"); | |
} | |
var user = arguments[0]; | |
print("Going to validate GitHub user @${user}..."); | |
var endpoint = "/users/${user}"; | |
var result = gh_curl("GET", "${endpoint}"); | |
var json = JSON.parse(result); | |
var avatar_url = json.avatar_url; | |
var avatar_default = "https://identicons.github.com/${user}.png"; | |
// download avatars for comparison | |
$EXEC("wget -O avatar_custom ${avatar_url}"); | |
$EXEC("wget -O avatar_default ${avatar_default}"); | |
// compare avatars | |
var comparison_result = $EXEC("sh -c '[ \"$( compare -metric rmse avatar_custom avatar_default null: 2>&1 )\" = \"0 (0)\" ] && echo -n \"Default avatar\" || echo -n \"Custom avatar\"'"); | |
var user_properties = {"name":json.name,"Avatar":comparison_result,"email":json.email,"company":json.company}; | |
print(stringify(user_properties)); | |
// delete downloaded avatars | |
$EXEC("rm avatar_custom avatar_default"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment