8

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.

I've never worked with the github API and im having a bit of a hard time wrapping my head around it.

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.

Martin Hobert
  • 203
  • 1
  • 3
  • 10
  • This is the api you'll be using. Can you show what you have tried, and explain what didn't work? https://developer.github.com/v3/repos/commits/ – Travis Collins Apr 24 '15 at 10:46
  • Github has what it's called, a "RESTful API", for the kind of which many tutorials exist online - If you don't know how to poll such services it's best if you take a tutorial that teaches the fundamentals of making such requests – nicholaswmin Apr 24 '15 at 10:51
  • This is what ive tried: $url = 'https://api.github.com/repos/epenance/hoberthovers'; $obj = json_decode(file_get_contents($url), true); What im getting back is: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden – Martin Hobert Apr 24 '15 at 11:02
  • 1
    You will need to execute: `curl -X GET -H "Cache-Control: no-cache" https://api.github.com/repos///commits` – ʰᵈˑ Apr 24 '15 at 11:17
  • Aight, it works on my live server but not my localhost with what i wrote above, however some of the links in my db dives into their folders which is the ones i want to see the update on such as this: https://github.com/ikkeflikkeri/LeagueSharp/tree/master/EasyAhri So my previous line doesnt work. Any ideas? – Martin Hobert Apr 24 '15 at 11:21

3 Answers3

10

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

Marian
  • 14,759
  • 6
  • 32
  • 44
Clintm
  • 4,505
  • 3
  • 41
  • 54
  • maybe obvious, but your repo must be public for the github api to access it through a simple fetch request. – jimbotron May 13 '19 at 03:44
5

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.

GitHub also requires you to send a user-agent in the header: https://developer.github.com/v3/#user-agent-required

For example, your PHP code would look like;

$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. See this answer

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
  • Thanks, how would i navigate the json object in php since its not an array? – Martin Hobert Apr 24 '15 at 11:38
  • 1
    Remove `, true` from `json_decode` to make it an object. – ʰᵈˑ Apr 24 '15 at 11:39
  • I needed to add ````curl_setopt($objCurl, CURLOPT_RETURNTRANSFER, true);```` to stop curl from automatically outputting the response. ````$response=1```` prior to this addition. – Mahks Mar 28 '20 at 04:08
  • please update your code !!!! ```$aCommit = json_decode($sInfo, false); $sTimeLatest = $aCommit[0]->commit->author->date; $iTimeLatest = strtotime($sTimeLatest);``` i wasted 15 minutes before reading these comments here ! – born loser May 03 '20 at 10:32
4

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

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"));
Takumi Adachi
  • 88
  • 1
  • 9