如何通过jQuery获取GitHub存储库上最新提交的SHA?

时间:2013-05-18 14:59:38

标签: jquery json api github commit

我已经在这几个小时了,我还没有在任何地方找到任何有用的信息。我正在尝试使用jQuery从单个用户获取任何GitHub存储库的最新提交,以显示在我的主页上。目前,这就是我所拥有的:

$.getJSON("https://api.github.com/users/theinfection/repos", function(data) {
        $(".lastcommit").html('<a href="https://github.com/TheInfection/' + data[0].name + '/commit/' + data[0].sha + '">text</a>');
});

输出此链接:https://github.com/TheInfection/Blue-Monday-HD/commit/undefined。这种工作但它只显示JSON文件中列出的第一个repo的最新提交,并且它没有得到所述提交的SHA。

现在,我想知道的是:

  • 如何获取提交的SHA?
  • 如何按时间顺序对提交进行排序,以使最新版本位于顶部?
  • 如何获取提交评论文本?

这一直困扰着我一段时间,所以非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

这会有帮助吗?

$.getJSON("https://api.github.com/users/theinfection/repos", function(data) {
    $.getJSON(data[0].commits_url.slice(0, -6), function(data) {
        $('.lastcommit').text(data[0].sha);
    });
});

在初始请求中,您获得了回购,然后获取您想要的回购并获得它的提交。当然,如果您已经知道您的回购品名称,可以致电:

var repo = 'TheInfection/Blue-Monday-HD';
$.getJSON("https://api.github.com/repos/" + repo + "/commits", function(data) {
    $('.lastcommit').text(data[0].sha);
});

列表已按逆时间顺序排序,消息和作者也在那里:data[0].commit.messagedata[0].commit.committer.name

jsfiddle

上播放
相关问题