Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/display authors names from git repo #290

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion publishable/config/larecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,16 @@
'replacement' => '<code-block>',
]
]
]
],

/*
|--------------------------------------------------------------------------
| Other Options
|--------------------------------------------------------------------------
*/

'git' => [
'enabled' => true,
],

];
9 changes: 9 additions & 0 deletions resources/views/docs.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
@include('larecipe::partials.sidebar')

<div class="documentation is-{{ config('larecipe.ui.code_theme') }}" :class="{'expanded': ! sidebar}">
@if($authors && !$authors->isEmpty())
<div class="text-xs text-grey-darker mb-6">
@if(($authorsCount = count($authors)) > 1)
{{$authorsCount}} authors ({{$authors[0]['name']}} and others)
@else
By {{$authors[0]['name']}}
@endif
</div>
@endif
{!! $content !!}
@include('larecipe::plugins.forum')
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/Contracts/GitService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace BinaryTorch\LaRecipe\Contracts;

use Illuminate\Support\Collection;

interface GitService
{
public function isGitInstalled(): bool;

public function getFileShortlog(string $filePath): Collection;
}
23 changes: 22 additions & 1 deletion src/DocumentationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use BinaryTorch\LaRecipe\Models\Documentation;
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
use BinaryTorch\LaRecipe\Traits\HasDocumentationAttributes;
use BinaryTorch\LaRecipe\Contracts\GitService;

class DocumentationRepository
{
Expand All @@ -18,14 +19,17 @@ class DocumentationRepository
*/
private $documentation;

private $gitService;

/**
* DocumentationController constructor.
*
* @param Documentation $documentation
*/
public function __construct(Documentation $documentation)
public function __construct(Documentation $documentation, GitService $gitService)
{
$this->documentation = $documentation;
$this->gitService = $gitService;

$this->docsRoute = route('larecipe.index');
$this->defaultVersion = config('larecipe.versions.default');
Expand Down Expand Up @@ -53,6 +57,7 @@ public function get($version, $page = null, $data = [])

$this->prepareTitle()
->prepareCanonical()
->prepareAuthors()
->prepareSection($version, $page);

return $this;
Expand Down Expand Up @@ -120,6 +125,22 @@ protected function prepareCanonical()
return $this;
}

protected function prepareAuthors()
{
if (!$this->gitService->isGitInstalled() || !config('larecipe.git.enabled')) {
return $this;
}

$pagePath = base_path(config('larecipe.docs.path').'/'.$this->version.'/'.$this->sectionPage.'.md');

$this->authors = $this->gitService
->getFileShortLog($pagePath)
->sortByDesc('commits')
->values();

return $this;
}

/**
* Check if the given version is in the published versions.
*
Expand Down
1 change: 1 addition & 0 deletions src/Http/Controllers/DocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function show($version, $page = null)
'versions' => $documentation->publishedVersions,
'currentSection' => $documentation->currentSection,
'canonical' => $documentation->canonical,
'authors' => $documentation->authors,
], $documentation->statusCode);
}
}
3 changes: 3 additions & 0 deletions src/LaRecipeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use BinaryTorch\LaRecipe\Commands\ThemeCommand;
use BinaryTorch\LaRecipe\Commands\InstallCommand;
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
use BinaryTorch\LaRecipe\Contracts\GitService as GitServiceContract;
use BinaryTorch\LaRecipe\Services\ParseDownMarkdownParser;
use BinaryTorch\LaRecipe\Facades\LaRecipe as LaRecipeFacade;
use BinaryTorch\LaRecipe\Commands\GenerateDocumentationCommand;
use BinaryTorch\LaRecipe\Services\GitService;

class LaRecipeServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -57,6 +59,7 @@ public function register()
}

$this->app->bind(MarkdownParser::class, ParseDownMarkdownParser::class);
$this->app->bind(GitServiceContract::class, GitService::class);

$this->app->alias('LaRecipe', LaRecipeFacade::class);

Expand Down
30 changes: 30 additions & 0 deletions src/Services/GitService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace BinaryTorch\LaRecipe\Services;

use BinaryTorch\LaRecipe\Contracts\GitService as GitServiceContract;
use Symfony\Component\Process\Process;
use Illuminate\Support\Collection;

class GitService implements GitServiceContract
{
public function isGitInstalled(): bool
{
$process = new Process(['git', '--version']);
$process->run();

return $process->getExitCode() != 127;
}

public function getFileShortlog(string $filePath): Collection
{
$process = new Process(['git', 'shortlog', '-sn', 'HEAD', '--', $filePath]);
$process->run();

return collect(explode("\n", $process->getOutput()))->slice(0, -1)->map(function ($logLine) {
[$commits, $name] = explode("\t", trim($logLine));
return compact('commits', 'name');
});
;
}
}
9 changes: 9 additions & 0 deletions src/Traits/HasDocumentationAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ trait HasDocumentationAttributes
protected $statusCode = 200;
protected $publishedVersions;
protected $defaultVersionUrl;
protected $authors;

/**
* @return string
Expand Down Expand Up @@ -88,4 +89,12 @@ public function getPublishedVersionsAttribute()
{
return $this->publishedVersions;
}

/**
* @return array
*/
public function getAuthorsAttribute()
{
return $this->authors;
}
}
93 changes: 93 additions & 0 deletions tests/Feature/DisplayGitAuthorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace BinaryTorch\LaRecipe\Tests\Feature;

use BinaryTorch\LaRecipe\Tests\TestCase;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use BinaryTorch\LaRecipe\Contracts\GitService as GitServiceContract;
use BinaryTorch\LaRecipe\Tests\Fixtures\DummyGitService;

class DisplayGitAuthorsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
Config::set('larecipe.docs.path', 'tests/views/docs');
Config::set('larecipe.docs.landing', 'foo');
}

/** @test */
public function if_git_config_is_not_enabled_page_will_be_displayed_without_errors()
{
Config::set('larecipe.git.enabled', false);

App::instance(GitServiceContract::class, new DummyGitService(true, []));

$this->get('/docs/1.0')
->assertOk()
->assertViewHas('authors', function($authors) {
return is_null($authors);
});
}

/** @test */
public function if_git_is_not_installed_page_will_be_displayed_without_errors()
{
App::instance(GitServiceContract::class, new DummyGitService(false, []));

$this->get('/docs/1.0')
->assertOk()
->assertViewHas('authors', function($authors) {
return is_null($authors);
});
}

/** @test */
public function if_git_is_installed_but_no_authors_are_found_page_will_be_displayed_without_errors()
{
App::instance(GitServiceContract::class, new DummyGitService(true, []));

$response = $this->get('/docs/1.0');
$response->assertOk();
$response->assertViewHas('authors', function($authors) {
return $authors->isEmpty();
});
}

/** @test */
public function check_if_one_author_is_displayed_on_the_view() {
App::instance(GitServiceContract::class, new DummyGitService(true, [
[
'name' => 'The Tester',
'commits' => 1,
]
]));

$this->get('/docs/1.0')
->assertOk()
->assertSee('By The Tester');
}

/** @test */
public function check_if_multiple_authors_are_displayed_on_the_view() {
App::instance(GitServiceContract::class, new DummyGitService(true, [
[
'name' => 'The Tester',
'commits' => 5,
],
[
'name' => 'Best Contributer',
'commits' => 10,
],
[
'name' => 'The Documenter',
'commits' => 7,
],
]));

$this->get('/docs/1.0')
->assertOk()
->assertSee('3 authors (Best Contributer and others)');
}
}
27 changes: 27 additions & 0 deletions tests/Fixtures/DummyGitService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace BinaryTorch\LaRecipe\Tests\Fixtures;

use BinaryTorch\LaRecipe\Contracts\GitService as GitServiceContract;
use Illuminate\Support\Collection;

class DummyGitService implements GitServiceContract
{
private $isGitInstalled;
private $authorsArray;

public function __construct(bool $isGitInstalled, array $authorsArray) {
$this->isGitInstalled = $isGitInstalled;
$this->authorsArray = $authorsArray;
}

public function isGitInstalled(): bool
{
return $this->isGitInstalled;
}

public function getFileShortlog(string $filePath): Collection
{
return collect($this->authorsArray);
}
}