user1981275
user1981275

Reputation: 13372

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?

Upvotes: 5

Views: 2949

Answers (3)

Gal Sisso
Gal Sisso

Reputation: 1959

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>

Upvotes: 10

Kenneth D White
Kenneth D White

Reputation: 49

<!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!

Upvotes: 1

Xesau
Xesau

Reputation: 161

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!

Upvotes: 2

Related Questions