/* Get the latest commits from a github repo and print them in a div
 *
 * Params:
 * - reponame: the user/reponame part of the github URL 
 * - divid: the ID of the HTML div to be modified.
 * - num: the number of commits to be printed
 *
 */    
function latestCommits(reponame, divid, num = 20)
{
//    $(div).css( "border", "3px solid red" );

    var url = "https://api.github.com/repos/" + reponame + "/commits";
    var div = "#" + divid;
    if($(div).length == 0) {
	throw("Can't find HTML element with '" + divid + "' ID");
    }
    
    $.get( url, function( data ) {
	var text = "<table>";
	for (i = 0; i < num; i++) {
	    text += "<tr>";
	    var url = data[i].html_url;
	    var date = new Date(data[i].commit.author.date);
	    text+= td(date.toLocaleDateString());
	    text += td(a(url, data[i].commit.message));
	    text += "</tr>";
	}
	text += "</table>";
	$(div).html(text);
    }).fail(function(qXHR, textStatus, error){
	throw("Cannot get repository '" + url + "' : " + error);
    });
}

function td(text){
    return "<td>" + text + "</td>";
}

function a(url, text){
    return '<a href="'+ url + '">' + text + '</a>';
}
