【问题标题】:How to get latest commit date - Github API如何获取最新的提交日期 - Github API
【发布时间】:2015-07-02 22:21:12
【问题描述】:

我目前正在编写一个小型 git reps 数据库,我想知道如果我的数据库中列出了代表,我将如何继续并获取最新提交的日期。

我从来没有使用过 github API,我很难理解它。

如果有人能帮我弄清楚,我将不胜感激。最好是 PHP 或 JS,因为我发现的所有示例都在 ruby​​ 中。

【问题讨论】:

  • 这是您将要使用的 api。你能展示你尝试过的东西,并解释什么没用吗? developer.github.com/v3/repos/commits
  • Github 有它所谓的“RESTful API”,在线上有很多教程 - 如果您不知道如何轮询此类服务,最好学习一个教程提出此类请求的基础
  • 这是我试过的:$url = 'api.github.com/repos/epenance/hoberthovers'; $obj = json_decode(file_get_contents($url), true);我回来的是:未能打开流:HTTP请求失败! HTTP/1.0 403 禁止
  • 你需要执行:curl -X GET -H "Cache-Control: no-cache" https://api.github.com/repos/<username>/<repo>/commits
  • 好吧,它适用于我的实时服务器,但不适用于我上面写的本地主机,但是我的数据库中的一些链接潜入了他们的文件夹,这是我想看到的更新就像这样:github.com/ikkeflikkeri/LeagueSharp/tree/master/EasyAhri 所以我的上一行不起作用。有什么想法吗?

标签: javascript php github-api


【解决方案1】:

我想回答这个确切的问题,所以我做了一个非常小的演示,说明如何获取最新提交的日期。

Demo

输出:

ta-dachi
master
2019-03-21T14:50:22Z <----- What you want
b80126c3ea900cd7c92729e652b2e8214ff014d8
https://github.com/ta-dachi/eatsleepcode.tech/tree/master

Github repo

index.html

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>React Local</title>
  <script
    type="application/javascript"
    src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"
  ></script>
  <script
    type="application/javascript"
    src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"
  ></script>
  <script
    type="application/javascript"
    src="https://unpkg.com/@babel/standalone/babel.min.js"
  ></script>
  <script
    type="application/javascript"
    src="https://unpkg.com/whatwg-fetch@3.0.0/dist/fetch.umd.js"
  ></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/jsx" src="index.jsx"></script>
</body>

index.jsx

/**
 * See https://developer.github.com/v3/repos/branches/#get-branch
 *
 * Example Github api request:
 * https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master
 */
class LatestCommitComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      author: "",
      branch: "",
      date: "",
      sha: "",
      link: ""
    };
  }

  componentDidMount() {
    // Replace this with your own repo
    // https://api.github.com/repos/:owner/:repo/branches/master
    fetch(
      "https://api.github.com/repos/ta-dachi/eatsleepcode.tech/branches/master"
    )
      .then(response => {
        response.json().then(json => {
          console.log(json);
          this.setState({
            author: json.commit.author.login,
            branch: json.name,
            date: json.commit.commit.author.date,
            sha: json.commit.sha,
            link: json._links.html
          });
        });
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    return (
      <div>
        <div>{this.state.author}</div>
        <div>{this.state.branch}</div>
        <div>{this.state.date}</div>
        <div>{this.state.sha}</div>
        <div>{this.state.link}</div>
      </div>
    );
  }
}

ReactDOM.render(<LatestCommitComponent />, document.getElementById("root"));

【讨论】:

    【解决方案2】:

    老问题,但我想指出(至少使用 api v3)您可以使用分支 api 来获取特定分支的最新提交日期。我假设在您的情况下您对大师感兴趣。

    看起来像:

    https://api.github.com/repos/:owner/:repo/branches/master
    

    https://developer.github.com/v3/repos/branches/

    【讨论】:

    • 可能很明显,但是你的 repo 必须是公开的,github api 才能通过简单的 fetch 请求访问它。
    【解决方案3】:

    如果您要使用像your example 这样的PHP,我会使用cURL 而不是file_get_contents,因为您需要配置allow-url-fopen

    GitHub 还要求您在标头中发送user-agenthttps://developer.github.com/v3/#user-agent-required

    例如,您的 PHP 代码如下所示;

    $objCurl = curl_init();
    
    //The repo we want to get
    curl_setopt($objCurl, CURLOPT_URL, "https://api.github.com/repos/google/blueprint/commits");
    
    //To comply with https://developer.github.com/v3/#user-agent-required
    curl_setopt($objCurl, CURLOPT_USERAGENT, "StackOverflow-29845346"); 
    
    //Skip verification (kinda insecure)
    curl_setopt($objCurl, CURLOPT_SSL_VERIFYPEER, false);
    
    //Get the response
    $response = curl_exec($objCurl);
    print_r( json_decode($response, true) );
    

    注意:您将能够继续使用file_get_contents 并发送user-agent 标头。见this answer

    【讨论】:

    • 谢谢,我将如何在 php 中导航 json 对象,因为它不是数组?
    • json_decode 中删除, true 使其成为一个对象。
    • 我需要添加curl_setopt($objCurl, CURLOPT_RETURNTRANSFER, true); 来阻止 curl 自动输出响应。 $response=1 在此添加之前。
    • 请更新您的代码!!!! $aCommit = json_decode($sInfo, false); $sTimeLatest = $aCommit[0]-&gt;commit-&gt;author-&gt;date; $iTimeLatest = strtotime($sTimeLatest); 在这里阅读这些 cmets 之前我浪费了 15 分钟!
    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    相关资源
    最近更新 更多