简体   繁体   English

如何获取最新的提交日期 - Github API

[英]How to get latest commit date - Github API

I'm currently writing a small database of git reps and im wondering how i would go ahead and get the date of the latest commit if i have the rep listed in my database. 我正在编写一个git reps的小型数据库,我想知道如果我在我的数据库中列出了rep,我将如何继续并获取最新提交的日期。

I've never worked with the github API and im having a bit of a hard time wrapping my head around it. 我从来没有使用过github API,而且我很难绕过它。

If anyone could help me figure it out i'd much appreciate it. 如果有人能帮我搞清楚,我会非常感激。 PHP or JS prefereably as all the examples ive found has been in ruby. PHP或JS优先,因为我发现的所有例子都在ruby中。

Old question but I wanted to point out (at least with api v3) you could use the branches api to get the latest commit date for a particular branch. 老问题,但我想指出(至少使用api v3)你可以使用branches api来获取特定分支的最新提交日期。 I assume in your case you're interested in master. 我假设在你的情况下你对主人感兴趣。

Which would look like: 看起来像这样:

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

See https://developer.github.com/v3/repos/branches/ 请参阅https://developer.github.com/v3/repos/branches/

If you were to use PHP like your example , I'd use cURL instead of file_get_contents , as you'd need to configure allow-url-fopen . 如果您使用PHP作为示例 ,我将使用cURL而不是file_get_contents ,因为您需要配置allow-url-fopen

GitHub also requires you to send a user-agent in the header: https://developer.github.com/v3/#user-agent-required GitHub还要求您在标题中发送user-agenthttps//developer.github.com/v3/#user-agent-required

For example, your PHP code would look like; 例如,您的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) );

Note: You will be able to continue using file_get_contents and send the user-agent header. 注意:您将能够继续使用file_get_contents并发送user-agent标头。 See this answer 看到这个答案

I wanted to answer this exact question so I made a very small demo of how to get the date of the latest commit. 我想回答这个问题,所以我做了一个非常小的演示,了解如何获取最新提交的日期。

Demo 演示

Output: 输出:

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

Github repo Github回购

index.html 的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 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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM