简体   繁体   中英

How do I get the SHA of the latest commit on a GitHub repository via jQuery?

I've been at this for several hours now and I have yet to find any useful information anywhere. I'm trying to use jQuery to get the latest commit of any GitHub repository from a single user to show up on my homepage. Currently, this is what I have:

$.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>');
});

That outputs this link: https://github.com/TheInfection/Blue-Monday-HD/commit/undefined . This sort of works but it only shows the latest commit of the first repo listed in the JSON file and it doesn't get the SHA of said commit.

Now, what I want to know is:

  • How do I get the SHA of commits?
  • How do I sort the commits in chronological order so that the latest is at the top?
  • How do I get the commit comment text?

This has been bothering me for a while so any help is much appreciated!

Would this help?

$.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);
    });
});

In the initial request you get the repos, then take the repo you want and get it's commits. Of course, if you know your repo name already, you can call:

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

The list is already sorted in the reverse-chronological order and the message and the author are also there: data[0].commit.message and data[0].commit.committer.name .

Play with it on jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM