Github API列出所有存储库和repo的内容

时间:2013-01-17 23:34:27

标签: php javascript github github-api

如果我要在外部网站上显示我的github存储库及其内容,我将如何进行此操作?是否有任何源代码可以提供给我,如果没有指出我正确的方向?我是编程的初学者,所以任何帮助都会受到赞赏。谢谢大家。 浏览一下他们的网站

我浏览了相关链接 - 但仍然不知道我将如何实现这一目标。

-Github List all Repo's

-Github List all Repo content

8 个答案:

答案 0 :(得分:28)

之前的所有答案都很棒。但是,如果您正在寻找一个如何获取公开可用回购列表的快速而肮脏的示例,请查看我的jsfiddle.

使用此ajax调用列出所有用户公共存储库:

$("#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);
        }
    });
});

要查看返回的数据类型,只需在点击按钮后检查控制台,或者您可以安装Google Chromes JSONView扩展程序,然后只需访问ajax请求正在进行的网址即https://api.github.com/users/google/repos

答案 1 :(得分:8)

这是一个很好的方式只是卷曲。您应该更改$ user和$ token变量以使此脚本适用于您的案例。代码使用有效令牌进行测试,因此我希望它能为您服务。正如您在代码的注释中所看到的,令牌可以从您的github帐户生成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 />';
    }
  }

?>

此外,既然我们喜欢github,我们应该将结果缓存到最后,每天大约一次获取它们。

答案 2 :(得分:5)

所有这些示例都是伪的,没有“身份验证”,您可以随意改进它们;

<?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
    ...

但是,我认为需要更复杂的结构;

<?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') );
?>

答案 3 :(得分:3)

当你说“显示一个仓库及其内容”时,你实际上说“在最新提交主分支后显示仓库的状态”,对吗?这实际上是思考问题的更好方式,并且将成为使用GitHub API的更好指南。

您需要查看API的Git data部分。这是你需要做的:

1)使用以下命令获取您的仓库的参考列表:

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

工作示例:

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

请注意,它列出了您的仓库中的参考资料,并为您提供了继续的链接。

2)使用响应1)中提供的链接获取您感兴趣的ref的提交对象,即“master”:

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

工作示例:

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

请注意,您将获得一个带有树链接的对象。

3)使用2)响应中提供的链接获取master ref中最后一次提交的树对象:

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

工作示例:

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

请注意,您将获取作为回购的根目录中的文件列表。这就是你想要的。如果您有子目录,您将获得获取这些子目录中文件的链接。

这应该足以让你入手:)。祝你好运!

答案 4 :(得分:1)

请尝试使用以下库,也可以在git hub上找到: https://github.com/ornicar/php-github-api

答案 5 :(得分:0)

您需要解析响应Githubs API发回的信息。在PHP中,您可以使用json_decode()来完成此操作,它将为您提供一个可以使用的数组。您可以使用curl之类的内容从PHP发出请求,然后获取结果并按上述方式解析它们。
另一种方法是使用PHP的REST客户端类,例如看看这个here

答案 6 :(得分:0)

如果你想要一些源代码进行分析,关于javascript,你可以尝试从GitHub Repositories开始(更具体地说是here),这是一个很好的开放式项目,用于Chrome扩展,它可以做类似于什么的你正在寻找。

答案 7 :(得分:0)

您可以使用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 

由于上述原因,您将获得一个包含所有存储库及其信息的长json。你可以从这里继续。

相关问题