private function updateAndReturnLatestCommits() { if ($lastSavedCommit = Commit::orderBy('when', 'desc')->first()) { $url = "https://api.github.com/repos/nodejs/node/commits?per_page=25&since=" . gmDate("Y-m-d\\TH:i:s\\Z", strtotime($lastSavedCommit->created_at)); } else { $url = "https://api.github.com/repos/nodejs/node/commits?per_page=25"; } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_USERAGENT, 'Picmonic PHP Test'); $data = curl_exec($curl); $data = json_decode($data); curl_close($curl); foreach ($data as $commitData) { $commit = new Commit(); $commit->sha = $commitData->sha; $commit->name = $commitData->commit->author->name; $commit->message = $commitData->commit->message; $commit->when = $commitData->commit->committer->date; $commit->url = $commitData->commit->url; $commit->save(); } return Commit::orderBy('when', 'desc')->take(25)->get(); }
public function index() { // grab desired commits array from github. Let's try utilizing "knplabs/github-api": "~1.4" first in composer.json $client = new \Github\Client(); $commits = $client->api('repo')->commits()->all('nodejs', 'node', array('sha' => 'master')); $numCommitsToParse = 25; $numCurrentCommitBeingParsed = 0; // save 25 most recent commits to db foreach ($commits as $commit) { if ($numCurrentCommitBeingParsed < $numCommitsToParse) { // grab new Commit instance $cm = new Commit(); // check to see if commit hash entry already exists $cmchk = Commit::where('hash', '=', $commit['sha'])->first(); // if it does not exist, save->db if ($cmchk == null) { $cm->author = $commit['commit']['author']['name']; $cm->hash = $commit['sha']; $cm->msg = $commit['commit']['message']; $cm->date = $commit['commit']['author']['date']; // save to db $cm->save(); $numCurrentCommitBeingParsed++; } } } /*** * * Yoink 25 most recent commits FROM DB (as specified in coding challenge * readme) * * Normally this functionality would probably live in a method somewhere else -- * ahhh the joys of having time to refactor :-) * */ #$savedCommits = Commit::orderBy('date', 'desc')->take(25)->get(); // moved 'orderBy' logic in above comment to Commit model scope $savedCommits = Commit::dateDescending()->take(25)->get(); /*** * * Now that we have the 25 most recent commits, we need to check to see if * the commit SHA (commits.hash) ends in a number, and if so, color that row * to light blue (#E6F1F6) * * !!!! Scratch that !!!! Let's move this functionality to the index.php angular view * where it should live. * */ // foreach ($savedCommits as $savedCommit) { // // if commits.hash ends with num then color row light blue... // } // json up the goodies and send 'em to angular //return Response::json(Commit::get()); /*** * Darn! Looks like Illuminate\Http\Response::json() is belly up. * Let's find something else to json encode with. Come on php, you can do it! */ // return JsonResponse::create($commits); // return JsonResponse::create($tmoo); return JsonResponse::create($savedCommits); }