Skip to content

Commit

Permalink
Join view, Org sync & login
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Jan 14, 2017
1 parent 0eb00d9 commit 48f1a48
Show file tree
Hide file tree
Showing 22 changed files with 1,638 additions and 340 deletions.
133 changes: 48 additions & 85 deletions app/Http/Controllers/GithubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Http\Controllers;

use App\Notification;
use App\Org;
use App\Repo;
use Auth;
use GrahamCampbell\GitHub\Facades\GitHub;
use GitHub;

class GithubController extends Controller
{
Expand All @@ -14,118 +14,81 @@ public function __construct()
$this->middleware('auth');
}

public function getNotifications()
public function syncOrgs()
{
Github::authenticate(Auth::user()->token, null, 'http_token');
$notifications = GitHub::api('notification')->all();
$this->storeNotifications($notifications);
$this->storeRepos($notifications);

return redirect('notifications');
$this->listOrgs();
$this->checkPerm();
return redirect('dashboard');
}

public function getNotification($id)
public function listOrgs()
{
Github::authenticate(Auth::user()->token, null, 'http_token');
$notification = GitHub::api('notification')->id($id);
$type = ($notification['subject'])['type'];
$url = ($notification['subject'])['url'];
$processed_url = explode('/', $url);
$user = $processed_url[4];
$repo = $processed_url[5];
$id = $processed_url[7];
if ($type == 'PullRequest') {
$pullRequest = $this->getPR($user, $repo, $id);

return redirect($pullRequest['html_url']);
} elseif ($type == 'Issue') {
$issue = $this->getIssue($user, $repo, $id);

return redirect($issue['html_url']);
} elseif ($type == 'Commit') {
$commit = $this->getCommit($user, $repo, $id);

return redirect($commit['html_url']);
} elseif ($type == 'Release') {
$release = $this->getRelease($user, $repo, $id);
$orgs = GitHub::api('user')->orgs();
$this->storeOrgs($orgs);
}

return redirect($release['html_url']);
} else {
return redirect('wip');
public function storeOrgs($orgs)
{
foreach ($orgs as $organization) {
if (!Org::where('id', '=', $organization['id'])->exists()) {
if (Org::find($organization['id']) == null) {
$org = new Org();
$org->id = $organization['id'];
$org->name = $organization['login'];
$org->url = $organization['url'];
$org->description = $organization['description'];
$org->avatar = $organization['avatar_url'];
$org->userid = Auth::id();
$org->username = Auth::user()->github_username;
$org->save();
}
}
}
}

public function getIssue($user, $repo, $id)
public function checkPerm()
{
Github::authenticate(Auth::user()->token, null, 'http_token');
$issue = Github::api('issue')->show($user, $repo, $id);

return $issue;
$orgs = Org::where('userid', '=', Auth::id())->get();
foreach ($orgs as $organization) {
if ($organization->role != 'admin') {
$membership = GitHub::api('organizations')->members()->member($organization->name, $organization->username);
$organization->role = $membership['role'];
$organization->save();
}
}
}

public function getPR($user, $repo, $id)
public function getReferers($id)
{
Github::authenticate(Auth::user()->token, null, 'http_token');
$pullRequest = Github::api('pull_request')->show($user, $repo, $id);
$repo = Repo::find($id);

return $pullRequest;
return Github::api('repo')->traffic()->referers($repo->owner, $repo->name);
}

public function getCommit($user, $repo, $id)
public function getPaths($id)
{
Github::authenticate(Auth::user()->token, null, 'http_token');
$commit = Github::api('repo')->commits()->show($user, $repo, $id);
$repo = Repo::find($id);

return $commit;
return Github::api('repo')->traffic()->paths($repo->owner, $repo->name);
}

public function getRelease($user, $repo, $id)
public function getViews($id)
{
Github::authenticate(Auth::user()->token, null, 'http_token');
$release = Github::api('repo')->releases()->show($user, $repo, $id);
$repo = Repo::find($id);

return $release;
return Github::api('repo')->traffic()->views($repo->owner, $repo->name);
}

public function storeNotifications($notifications)
public function getClones($id)
{
foreach ($notifications as $rawnotif) {
if (!Notification::where('id', '=', $rawnotif['id'])->exists()) {
$subject = $rawnotif['subject'];
$repo = $rawnotif['repository'];
$notification = new Notification();
$notification->id = $rawnotif['id'];
$notification->unread = $rawnotif['unread'];
$notification->reason = $rawnotif['reason'];
$notification->title = $subject['title'];
$notification->url = $subject['url'];
$notification->type = $subject['type'];
$notification->private = $repo['private'];
$notification->repoid = $repo['id'];
$notification->userid = Auth::user()->id;
$notification->save();
}
}
}
Github::authenticate(Auth::user()->token, null, 'http_token');
$repo = Repo::find($id);

public function storeRepos($notifications)
{
foreach ($notifications as $notification) {
$repo = $notification['repository'];
if (!Repo::where('id', '=', $repo['id'])->exists()) {
$user = $repo['owner'];
if (Repo::find($repo['id']) == null) {
$repository = new Repo();
$repository->id = $repo['id'];
$repository->name = $repo['name'];
$repository->full_name = $repo['full_name'];
$repository->owner = $user['login'];
$repository->html_url = $repo['html_url'];
$repository->description = $repo['description'];
$repository->userid = Auth::user()->id;
$repository->save();
}
}
}
return Github::api('repo')->traffic()->clones($repo->owner, $repo->name);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function loginUser(Request $request)
$user->email = $details->email;
$user->name = $details->full_name;
$user->token = $details->access_token;
$user->github_username = $details->nickname;
$user->save();
});
} catch (ApplicationRejectedException $e) {
Expand Down
10 changes: 10 additions & 0 deletions app/Org.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Org extends Model
{
public $timestamps = false;
}
7 changes: 5 additions & 2 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'token', 'github_username', 'id',
];

/**
Expand All @@ -24,6 +24,9 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'remember_token',
];
public function orgs(){
return $this->hasMany('App\Org', 'userid');
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"require": {
"php": ">=5.6.4",
"adamwathan/eloquent-oauth-l5": "^0.5.1",
"laravel/framework": "5.3.*"
"graham-campbell/github": "^5.1",
"laravel/framework": "5.3.*",
"m1guelpf/github-api": "dev-packagist",
"php-http/guzzle6-adapter": "^1.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit 48f1a48

Please sign in to comment.