Skip to main content
added 117 characters in body
Source Link
Samuel O
  • 2.3k
  • 1
  • 17
  • 27

Quick sample link for repo MDOWN of user ONDREK
https://api.github.com/repos/ondrek/mdown/git/refs/heads/

How to make a request to Github API

How to make a request to Github API

Quick sample link for repo MDOWN of user ONDREK
https://api.github.com/repos/ondrek/mdown/git/refs/heads/

How to make a request to Github API

Source Link
Samuel O
  • 2.3k
  • 1
  • 17
  • 27

You can check my article about this:
http://ondrek.me/articles/how-to-parse-github-API-in-nodejs/

This is NodeJs way
(for client js change require("https") to client ajax JSON - code is very simular)

How to make a request to Github API

var options = {
    user : 'ondrek',
    repo : 'favicon-blog',
    branch : 'master'
};

function getHttpRequestJson(customPath, callback){
    require('https').request({
        hostname: 'api.github.com',
        path: customPath,
        method: 'GET'
    }, function(res){
        var data = '';
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on('end', function () {
            callback(JSON.parse(data));
        });
    }).end();
}

How to get user details json from Github API

(inject previous code)

var url = '/repos/'+options.user+'/'+options.repo+'/git/refs/heads/'+options.branch;
getHttpRequestJson(url, function(userJson){
    var lastCommitUrl = userJson.object.url;
    getLastCommit(lastCommitUrl);
});

How to get last commit json from Github API

(inject previous code)

function getLastCommit(url){
    getHttpRequestJson(url+auth, function(lastCommitJson){
        var treeUrl = lastCommitJson.tree.url;
        getTree(treeUrl);
    });
}

How to get tree of last commit from Github API

(inject previous code)

function getTree(url){
    getHttpRequestJson(url+auth, function(treeJson){
        var treeArr = treeJson.tree;
        getOnlyPages(treeArr);
    });
}

How to get specific folder of last commit from Github API

(inject previous code)

function getOnlyPages(treeArr){
    treeArr.forEach(function(ele){
        if (ele.path==='blog') { getArticles(ele.url); }
    });
}

function getArticles(url){
    getHttpRequestJson(url+auth, function(treeJson){
        var treeArr = treeJson;
        parseMarkdownArticles(treeArr.tree);
    });
}