Skip to content

Commit

Permalink
fetch github issues
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomarinho committed Dec 7, 2016
1 parent 83e4a29 commit e05a11f
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 55 deletions.
75 changes: 47 additions & 28 deletions app/Classes/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use Auth;
use GitScrum\Models\Branch;
use GitScrum\Models\Commit;
use GitScrum\Models\ConfigStatus;
use GitScrum\Models\User;
use GitScrum\Models\Issue;
use GitScrum\Models\Organization;
use GitScrum\Models\ProductBacklog;
use Carbon\Carbon;
use GitScrum\Libraries\Phpcs;

Expand Down Expand Up @@ -173,6 +176,50 @@ public function setBranches($owner, $product_backlog_id, $repo)
}
}

public function getIssues()
{
$repos = ProductBacklog::all();

foreach ($repos as $repo) {
$issues = $this->request('https://api.github.com/repos/'.$repo->organization->username.'/'.$repo->title.'/issues?state=all');

foreach ($issues as $issue) {
$user = User::where('username', $issue->user->login)->first();

$data = [
'github_id' => $issue->id,
'user_id' => isset($user_id) ? $user->id : Auth::user()->id,
'product_backlog_id' => $repo->id,
'effort' => 0,
'config_issue_effort_id' => 1,
'issue_type_id' => 1,
'number' => $issue->number,
'title' => $issue->title,
'description' => $issue->body,
'state' => $issue->state,
'html_url' => $issue->html_url,
'created_at' => $issue->created_at,
'updated_at' => $issue->updated_at,
];

if (!is_null($issue->closed_at)) {
$data['closed_at'] = Carbon::parse($issue->closed_at)->format('Y-m-d h:m:s');
$data['closed_user_id'] = $data['user_id'];
$data['config_status_id'] = ConfigStatus::where('type', 'issue')
->where('is_closed', 1)->first()->id;
}

if ( !isset(Issue::where('github_id', $issue->github_id)->first()) )
{
Issue::create($data)->users()->sync([$data['user_id']]);
}
//foreach ($issue->assignees as $assign) {
// User::where('github_id', $assign->id)->first()->issues()->sync([$issueId], false);
//}
}
}
}

private function request($url, $auth = true, $customRequest = null, $postFields = null)
{
$user = Auth::user();
Expand Down Expand Up @@ -219,34 +266,6 @@ public function repositories($org){
}
*/

public function setIssues($owner, $repo)
{
$issues = $this->request('https://api.github.com/repos/'.$owner.'/'.$repo.'/issues?state=all');
$repository = Repository::where('name', $repo)->first();
$IssueRepository = new IssueRepository();
foreach ($issues as $issue) {
$data = [
'github_id' => $issue->id,
'product_backlog_id' => $repository->id,
'number' => $issue->number,
'title' => $issue->title,
'body' => $issue->body,
'state' => $issue->state,
'html_url' => $issue->html_url,
'date' => $issue->created_at,
];
$issueId = $IssueRepository->add($data)->id;
foreach ($issue->assignees as $assign) {
User::where('github_id', $assign->id)->first()->issues()->sync([$issueId], false);
}
}
}

public function issues()
{
return $this->request('https://api.github.com/repos/Doinn/Dracarys/issues/317');
}

public function setCommits($owner, $repo, $branch, $since = null)
{
////repos/:owner/:repo/commits?sha=branchname
Expand Down
50 changes: 36 additions & 14 deletions app/Http/Controllers/IssueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,26 @@ public function update(IssueRequest $request, $slug)

public function statusUpdate(Request $request, $slug = null, int $status = 0)
{
if (!$request->ajax()) {
$issue = Issue::where('slug', $slug)
->firstOrFail();

$issue->config_status_id = $status;
$issue->closed_user_id = Auth::id();
$issue->closed_at = Carbon::now();
$issue->save();

return back()->with('success', _('Updated successfully'));
} else {
// TODO Refactory
if ($request->ajax()) {
$position = 0;
try {
foreach (json_decode($request->json) as $id) {
foreach (json_decode($request->json) as $id)
{
$issue = Issue::find($id);
$issue->config_status_id = $request->status_id;
$issue->closed_user_id = Auth::id();
$issue->closed_at = Carbon::now();

$status = ConfigStatus::find($request->status_id);

$issue->closed_user_id = null;
$issue->closed_at = null;

if (!is_null($status->is_closed) && is_null($issue->closed_at))
{
$issue->closed_user_id = Auth::id();
$issue->closed_at = Carbon::now();
}

$issue->position = $position++;
$issue->save();
}
Expand All @@ -228,6 +230,26 @@ public function statusUpdate(Request $request, $slug = null, int $status = 0)
'success' => false,
]);
}
} else {
$issue = Issue::where('slug', $slug)
->firstOrFail();

$issue->config_status_id = $status;

$issue->closed_user_id = null;
$issue->closed_at = null;

$status = ConfigStatus::find($status);

if (!is_null($status->is_closed) && is_null($issue->closed_at))
{
$issue->closed_user_id = Auth::id();
$issue->closed_at = Carbon::now();
}

$issue->save();

return back()->with('success', _('Updated successfully'));
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/WizardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ public function step2(Request $request)
->with('repositories', $repositories)
->with('columns', ['repository', 'organization']);
}

public function step3()
{
$result = app('GithubClass')->getIssues();
return redirect()->route('issues.index', ['slug'=>0]);
}
}
4 changes: 2 additions & 2 deletions app/Models/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public function dateForHumans($dateField = 'created_at')

public function setClosedUserIdAttribute($value)
{
$this->attributes['closed_user_id'] = $this->status->is_closed ? $value : null;
$this->attributes['closed_user_id'] = isset($this->status->is_closed) || $value ? $value : null;
}

public function setClosedAtAttribute($value)
{
$this->attributes['closed_at'] = $this->status->is_closed ? $value : null;
$this->attributes['closed_at'] = isset($this->status->is_closed) || $value ? $value : null;
}
}
24 changes: 18 additions & 6 deletions app/Observers/IssueObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ class IssueObserver
{
public function creating(Issue $issue)
{
try {
$product_backlog_id = UserStory::find($issue->user_story_id)->product_backlog_id;
} catch (\Exception $e) {
$product_backlog_id = $issue->sprint()->first()->product_backlog_id;
if (isset($issue->product_backlog_id)) {
$product_backlog_id = $issue->product_backlog_id;
} else {
try {
$product_backlog_id = UserStory::find($issue->user_story_id)->product_backlog_id;
} catch (\Exception $e) {
$product_backlog_id = $issue->sprint()->first()->product_backlog_id;
}
}

$issue->slug = Helper::slug($issue->title);
$issue->user_id = Auth::user()->id;
$issue->config_status_id = ConfigStatus::where('default', '=', 1)->first()->id;

if (!isset($issue->user_id)) {
$issue->user_id = Auth::user()->id;
}

if (!isset($issue->config_status_id)) {
$issue->config_status_id = ConfigStatus::where('default', '=', 1)->first()->id;
}

$issue->product_backlog_id = $product_backlog_id;

// TODO Create a branch in GitHub
//$model->branch->sync([['sprint_id' => true]]);
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@
"url": "https://github.com/jamisonvalenta/Laravel-4-Generators.git"
}
},
"version": "0.7.0"
"version": "0.8.0"
}
2 changes: 1 addition & 1 deletion composer.json-backup
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@
"url": "https://github.com/jamisonvalenta/Laravel-4-Generators.git"
}
},
"version": "0.6.0"
"version": "0.7.0"
}
2 changes: 1 addition & 1 deletion public/css/core.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/css/maps/core.css.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions resources/assets/core/less/core.less
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ h3{
min-height: 400px;
background: #e0e0e0;
padding:15px;
.label{
margin-right:6px;
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion resources/views/wizard/step2.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
@include('partials.boxes.repositories', ['list'=>$repositories, 'columns'=>$columns])

<div class="text-center">
<a href="{{route('product_backlogs.index')}}" class="btn btn-lg btn-success">{{_('Continue using')}} <strong>{{_('GitScrum')}}</strong></a>
<a href="{{route('wizard.step3')}}" class="btn btn-lg btn-success">{{_('Import my')}} <strong>{{_('GitHub Issues')}}</strong></a>

<span class="mll mrl"><strong>{{_('or')}}</strong></span>

<a href="{{route('product_backlogs.index')}}" class="btn btn-lg btn-default">{{_('Continue using')}} <strong>{{_('GitScrum')}}</strong></a>
</div>

</div>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@
Route::group(['prefix' => 'wizard'], function () {
Route::get('/step1', 'WizardController@step1')->name('wizard.step1');
Route::post('/step2', 'WizardController@step2')->name('wizard.step2');
Route::get('/step3', 'WizardController@step3')->name('wizard.step3');
});

0 comments on commit e05a11f

Please sign in to comment.