32

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 provide me with, if not point me in the right direction? I'm quite a beginner to programming so any help is appreciated. Thank you everyone. Taking a glance at their website

I glanced over relevant links- but still have no clue how I would accomplish this.

-Github List all Repo's

-Github List all Repo content

4

8 回答 8

30

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

于 2013-02-02T00:08:19.070 回答
11

Here is a nice way just with the curl. You should change the $user and the $token variableso to make this script work for your case. The code is tested with a valid token so I hope it will work for you. As you could see in the comments of the code the token could be generated from your github account from here https://github.com/settings/applications

<?php
  // for example your user
  $user = 'flesheater';

  // A token that you could generate from your own github 
  // go here https://github.com/settings/applications and create a token
  // then replace the next string
  $token = 'ced38b0e522a5c5e8ab10';

  // We generate the url for curl
  $curl_url = 'https://api.github.com/users/' . $user . '/repos';

  // We generate the header part for the token
  $curl_token_auth = 'Authorization: token ' . $token;

  // We make the actuall curl initialization
  $ch = curl_init($curl_url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  // We set the right headers: any user agent type, and then the custom token header part that we generated
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Awesome-Octocat-App', $curl_token_auth));

  // We execute the curl
  $output = curl_exec($ch);

  // And we make sure we close the curl       
  curl_close($ch);

  // Then we decode the output and we could do whatever we want with it
  $output = json_decode($output);

  if (!empty($output)) {
    // now you could just foreach the repos and show them
    foreach ($output as $repo) {
      print '<a href="' . $repo->html_url . '">' . $repo->name . '</a><br />';
    }
  }

?>

Also since we like github, we should cache the results in the end and fetch them once per day or so.

于 2014-04-30T14:59:49.617 回答
5

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') );
?>
于 2013-01-30T12:55:29.413 回答
3

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!

于 2013-01-18T16:47:17.347 回答
1

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

于 2013-01-30T13:03:06.103 回答
0

You need to parse the respone Githubs API sends you back. In PHP you can do this by using json_decode() which will give you an array to work with. You can use something like curl to issue the requests from PHP and then get the results and parse them as described above.
Another way to do this are REST Client classes for PHP, have a look at this one here for example.

于 2013-01-17T23:44:32.867 回答
0

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.

于 2013-01-18T02:57:32.667 回答
0

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.

于 2013-10-03T12:13:51.397 回答