-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.github.min.js
More file actions
1 lines (1 loc) · 3.66 KB
/
jquery.github.min.js
File metadata and controls
1 lines (1 loc) · 3.66 KB
1
(function($){"use strict";function debug(msg){console.log("github:",msg)}function iso8601(date){var year=date.getFullYear(),month=""+date.getMonth()+1,day=""+date.getDate();return year+"-"+(1==month.length?"0"+month:month)+"-"+ +(1==day.length?"0"+day:day)}function ago(date){function pl(qt,unit){var m=Math.ceil(diff/qt),u=unit;return m>1&&(u+="s"),m+" "+u+" ago"}var diff=Date.now()-date.getTime();return 6e4>diff?"less than a minute ago":(diff=Math.ceil(diff/1e3),3600>diff?pl(60,"minute"):86400>diff?pl(3600,"hour"):2678400>diff?pl(86400,"day"):31536e3>diff?pl(2678400,"month"):pl(31536e3,"year"))}function getRepos(cb){var repos=[],params=settings.client_id&&settings.client_secret?{client_id:settings.client_id,client_secret:settings.client_secret}:{};$.ajax({url:api_base+"/users/"+settings.user+"/repos",data:params,type:"GET",dataType:"json",success:function(data){if(!(data instanceof Array))return cb(repos),void 0;for(var i=0;data.length>i;i++){var repo=data[i];repos.push({name:repo.name,url:repo.html_url})}cb(repos)}})}function getCommits(repos,cb){function lower(){0==--bar&&complete()}function complete(){commits.sort(function(a,b){return b.date.getTime()-a.date.getTime()}),cb(commits.slice(0,settings.display))}var commits=[],bar=repos.length,since=iso8601(new Date(Date.now()-262974e4)),params={};params.since=since,settings.client_id&&settings.client_secret&&(params.client_id=settings.client_id,params.client_secret=settings.client_secret),$.each(repos,function(i,repo){$.ajax({url:api_base+"/repos/"+settings.user+"/"+repo.name+"/commits",type:"GET",data:params,dataType:"json",success:function(data){if(!(data instanceof Array))return lower(),void 0;for(var i=0;data.length>i;i++){var commit=data[i];commits.push({repo:repo,user:{name:commit.author.login,avatar:commit.author.avatar_url,url:"https://github.com/"+commit.author.login},msg:commit.commit.message,sha:commit.sha,url:"https://github.com/"+settings.user+"/"+repo.name+"/commit/"+commit.sha,tree:"https://github.com/"+settings.user+"/"+repo.name+"/tree/"+commit.sha,date:new Date(Date.parse(commit.commit.author.date))})}lower()}})})}var api_base="https://api.github.com",settings={display:5,user:null,client_id:null,client_secret:null};$.fn.github=function(options){var $this=this;for(var key in settings)options.hasOwnProperty(key)&&options[key]!==void 0&&(settings[key]=options[key]);return settings.user?(debug(settings),getRepos(function(repos){debug(repos),getCommits(repos,function(commits){debug(commits);for(var html='<table class="table table-bordered table-striped table-condensed"><tr><td colspan="3" style="text-align:center"><small><strong><a href="https://github.com/'+settings.user+'">'+settings.user+"</a> • GitHub</strong></small>"+"</td>"+"</tr>",i=0;commits.length>i;i++){var commit=commits[i];html+='<tr><td style="width:36px"><img src="'+commit.user.avatar+'" class="img-rounded" '+'style="height:36px;width:36px" />'+"</td>"+'<td style="border-left:0">'+'<a style="color:#000" href="'+commit.url+'" target="_blank"><strong>'+commit.msg+"</strong></a><br />"+'<div class="visible-desktop"><small><a href="'+commit.user.url+'" target="_blank">'+commit.user.name+'</a> <span class="muted">'+ago(commit.date)+" to </span>"+'<a href="'+commit.repo.url+'">'+commit.repo.name+"</a></small>"+"</div>"+"</td>"+'<td style="border-left:0;text-align:right">'+'<a href="'+commit.url+'" target="_blank"><span class="badge" style="font-weight:regular">'+commit.sha.substring(0,10)+"</span></a><br />"+'<small><a class="muted" target="_blank" href="'+commit.tree+'"><strong>Browse code »</strong>'+"</a></small>"+"</td>"+"</tr>"}html+="</table>",$this.html(html)})}),void 0):(console.error("github: please provide username"),void 0)}})(jQuery);