Github API List all repositories and repo's content

后端 未结 8 758
陌清茗
陌清茗 2020-12-23 21:03

If I was to go about displaying just MY github repositories and their contents on an external website how would i go about doing this? Are there any source code\'s you can

相关标签:
8条回答
  • 2020-12-23 21:35

    all of the previous answers are great. however if you are looking for a quick and dirty example of how to get a list of publicly available repos then check out my jsfiddle.

    which uses this ajax call to list all of a users public repos:

    $("#btn_get_repos").click(function() {
        $.ajax({
            type: "GET",
            url: "https://api.github.com/users/google/repos",
            dataType: "json",
            success: function(result) {
                for(var i in result ) {
                    $("#repo_list").append(
                        "<li><a href='" + result[i].html_url + "' target='_blank'>" +
                        result[i].name + "</a></li>"
                    );
                    console.log("i: " + i);
                }
                console.log(result);
                $("#repo_count").append("Total Repos: " + result.length);
            }
        });
    });
    

    to see what kind of data is returned just check the console after clicking the button or you can install Google Chromes JSONView extension and then just visit the url that the ajax request is making i.e. https://api.github.com/users/google/repos

    0 讨论(0)
  • 2020-12-23 21:36

    you can use github api

    organization="write-here-the-organization"
    githubuser="your-github-user"
    token=`curl -i -u ${githubuser}  -d '{"scopes": ["repo"]}' https://api.github.com/authorizations | grep token | cut -d\" -f 4`
    curl -i -H "Authorization: token ${token}" https://api.github.com/orgs/${organization}/repos 
    

    as result of the above you will get a long json with all repositories, and their information. you can continue from here.

    0 讨论(0)
  • 2020-12-23 21:40

    Please try following library also available on git hub: https://github.com/ornicar/php-github-api

    0 讨论(0)
  • 2020-12-23 21:42

    When you say "display a repo and its contents" you actually say "display the state of the repo after the latest commit of the master branch", right? That's actually the better way of thinking about the problem and will be a better guide through using GitHub's API.

    You need to look at the Git data part of the API. Here's what you need to do:

    1) fetch the list of refs for your repo using:

    https://api.github.com/repos/:user/:repo/git/refs
    

    Working example:

    https://api.github.com/repos/izuzak/noam/git/refs
    

    Notice that it lists the references in your repo and gives you links to continue.

    2) fetch the commit object of the ref that interests you, namely "master", using the link provided in the response to 1):

    https://api.github.com/repos/:user/:repo/git/commits/:sha
    

    Working example:

    https://api.github.com/repos/izuzak/noam/git/commits/5cf12775b844664d5f7af6663706195680181374
    

    Notice that you get back an object with a link to a tree.

    3) fetch the tree object of the last commit in the master ref, using the link provided in the response to 2) :

    https://api.github.com/repos/:user/:repo/git/trees/:sha
    

    Working example:

    https://api.github.com/repos/izuzak/noam/git/trees/8a721bea8d2f281c87b39c74cbf5a70075d686b4
    

    Notice that you get back a list of files in the root directory that is your repo. This is what you want. If you have subdirectories, you will get links to fetch the files in those subdirectories.

    This should be enough to get you started :). Good luck!

    0 讨论(0)
  • 2020-12-23 21:42

    If you want some source code to analyze, about javascript you can try to start from GitHub Repositories (more specifically here), it's a nice open project for a Chrome extension which does something similiar to what you're looking for.

    0 讨论(0)
  • 2020-12-23 21:47

    All these examples are just as pseudo without "authentication" and you can improve them yourself as you like;

    <?php
    // a simple way to get a user's repo
    $res = file_get_contents("https://api.github.com/repos/qeremy/mii");
    $res = json_decode($res);
    print_r($res);
    ?>
    
    stdClass Object
    (
        [language] => JavaScript
        [merges_url] => https://api.github.com/repos/qeremy/mii/merges
        [contributors_url] => https://api.github.com/repos/qeremy/mii/contributors
        [assignees_url] => https://api.github.com/repos/qeremy/mii/assignees{/user}
        [url] => https://api.github.com/repos/qeremy/mii
        [description] => Multipurpose JavaScript Library
        [ssh_url] => git@github.com:qeremy/mii.git
        [comments_url] => https://api.github.com/repos/qeremy/mii/comments{/number}
        [statuses_url] => https://api.github.com/repos/qeremy/mii/statuses/{sha}
        [keys_url] => https://api.github.com/repos/qeremy/mii/keys{/key_id}
        ...
    
    <?php
    // getting a repo's README
    $res = file_get_contents("https://api.github.com/repos/qeremy/mii/readme");
    $res = json_decode($res);
    print_r($res);
    ?>
    
    stdClass Object
    (
        [_links] => stdClass Object
            (
                [self] => https://api.github.com/repos/qeremy/mii/contents/README.md
                [git] => https://api.github.com/repos/qeremy/mii/git/blobs/49f0c4d5e25ac44921ba4372aebd76d2da5128e2
                [html] => https://github.com/qeremy/mii/blob/master/README.md
            )
    
        [url] => https://api.github.com/repos/qeremy/mii/contents/README.md
        [type] => file
        [sha] => 49f0c4d5e25ac44921ba4372aebd76d2da5128e2
        [path] => README.md
        [size] => 8213
        [encoding] => base64
        [content] => QWN0dWFsbHksIEkga25vdyB0aGF0IHRoZXJlIGFyZSBidWNoIG9mIEphdmFT
    Y3JpcHQgbGlicmFyeSwgZXZlbiBtb3JlIHBvd2VyZnVsbC4gQnV0IHNvbWV0
        ...
    

    But, I think needs more complicated structure;

    <?php
    class GRepo
    {
        protected 
            // needs "user"
            $src_userRepos = "https://api.github.com/users/%s/repos",
            // needs "user,repo"
            $src_userRepoDetails = "https://api.github.com/repos/%s/%s",
            $responseCode, $responseText,
            $user;
    
        public function __construct($user) {
            $this->user = $user;
        }
    
        public function listRepos() {
            $this->_request(
                sprintf($this->src_userRepos, $this->user));
            if ($this->responseCode != 200) {
                throw new Exception('Server error!'); // e.g
            }
            return json_decode($this->responseText);
        }
    
        public function getRepoDetails($repo) {
            $this->_request(
                sprintf($this->src_userRepoDetails, $this->user, $repo));
            if ($this->responseCode != 200) {
                throw new Exception('Server error!'); // e.g
            }
            return json_decode($this->responseText);
        }
    
        // Could be extended, e.g with CURL..
        protected function _request($url) {
            $contents =@ file_get_contents($url);
            $this->responseCode = (false === $contents) ? 400 : 200;
            $this->responseText = $contents;
        }
    }
    
    // Test
    $gr = new GRepo('qeremy');
    print_r( $gr->listRepos() );
    print_r( $gr->getRepoDetails('mii') );
    ?>
    
    0 讨论(0)
提交回复
热议问题