Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download link for latest release of GitHub project

I am trying to add to my website a download link to the latest github release of a project. For example the link https://github.com/mongodb/mongo/archive/r3.0.0-rc7.zip does link to the latest release (as of today), but I do not want to hard-code the version number on the web site.

I found several questions about this issue, answers using curl, ajax or php.

I tried the solution using ajax which uses the github release API:

<!DOCTYPE html>

<HTML> <BODY>

<script language="javascript" type="text/javascript">   
    $(document).ready(function () { 
        GetLatestReleaseInfo();   
    });   

    function GetLatestReleaseInfo() {
      $.getJSON("https://github.com/mongodb/mongo/releases").done(function (json) {
         var release = json[0];
         var asset = release.assets[0];
         var downloadURL = "https://github.com/mongodb/mongo/releases" + release.tag_name + "/" + asset.name;
         $(".mongodb-download").attr("href", downloadURL);   
      });    
    } 
</script>

<a href="GetLatestReleaseInfo();">Link</a> 
<a href="" onclick="location.href=this.href+downloadURL;return false;">Link2</a> 
<a href="" onclick="location.href=this.href+mongodb-download;return false;">Link3</a>

</BODY>
</HTML>

but I do not manage to call the javascript function correctly, as it seems in my tries above (Link, Link2 and Link3). I'm not very familiar with javascript or ajax, so I'd appreciate any help; maybe there is a simpler way without Ajax?

like image 956
user1981275 Avatar asked Jan 30 '15 13:01

user1981275


People also ask

How do I pull latest release from GitHub?

On GitHub.com, navigate to the main page of the repository. To the right of the list of files, click Releases. To copy a unique URL to your clipboard, find the release you want to link to, right click the title, and copy the URL. Alternatively, right click Latest Release and copy the URL to share it.


3 Answers

You're loading an html page instead of their REST API.The correct url to get the tags is https://api.github.com/repos/mongodb/mongo/tags

You may want to read more about github api over here - https://developer.github.com/v3/repos/

Your html could look like this :

<!DOCTYPE html>

<HTML> <BODY>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">  

$(document).ready(function () {
     GetLatestReleaseInfo();  
});  


function GetLatestReleaseInfo() {
   $.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (json) {
        var release = json[0];
        var downloadURL = release.zipball_url;
        $("#mongodb-download").attr("href", downloadURL);  
   });    
}  
</script>

<a id='mongodb-download' href="">Download latest mongo</a>

</BODY>
</HTML>
like image 57
Gal Avatar answered Sep 24 '22 02:09

Gal


For me, this worked well:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            $(document).ready (function () {
                $.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (data) {
                    $ ('#mongodb-download').attr ('href', data[0].zipball_url); 
                })
            });
        </script>
    </head>
    <body>
        <a id="mongodb-download">Download the latest version of MongoDB</a>
    </body>
</html>

If you are getting problems with undefined, just change the $ into jQuery and it should all work!

like image 45
Xesau Avatar answered Sep 24 '22 02:09

Xesau


<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            jQuery(document).ready (function () {
                jQuery.getJSON("https://api.github.com/repos/mongodb/mongo/tags").done(function (data) {
                    jQuery('#mongodb-download').attr ('href', data[0].zipball_url); 
                })
            });
        </script>
    </head>
    <body>
        <a id="mongodb-download">Download the latest version of MongoDB</a>
    </body>
</html>

This works for me, hope it helps!

like image 38
Kenneth D White Avatar answered Sep 24 '22 02:09

Kenneth D White