From e81680698002c5be74b8b19483e3f8aa9fe8bd2d Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Mon, 10 Jan 2022 19:35:47 +0200 Subject: [PATCH 01/16] Get documentation page authors from the git repo --- src/DocumentationRepository.php | 15 +++++++++++++++ src/Http/Controllers/DocumentationController.php | 1 + src/Traits/HasDocumentationAttributes.php | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/src/DocumentationRepository.php b/src/DocumentationRepository.php index 6ffa5b25..a3332a9e 100644 --- a/src/DocumentationRepository.php +++ b/src/DocumentationRepository.php @@ -6,6 +6,7 @@ use BinaryTorch\LaRecipe\Models\Documentation; use Illuminate\Database\Eloquent\Concerns\HasAttributes; use BinaryTorch\LaRecipe\Traits\HasDocumentationAttributes; +use Symfony\Component\Process\Process; class DocumentationRepository { @@ -53,6 +54,7 @@ public function get($version, $page = null, $data = []) $this->prepareTitle() ->prepareCanonical() + ->prepareAuthors() ->prepareSection($version, $page); return $this; @@ -120,6 +122,19 @@ protected function prepareCanonical() return $this; } + protected function prepareAuthors() + { + $pagePath = base_path(config('larecipe.docs.path').'/'.$this->version.'/'.$this->sectionPage.'.md'); + $process = new Process(['git', 'shortlog', '-sn', 'HEAD', '--', $pagePath]); + $process->run(); + $this->authors = collect(explode("\n", $process->getOutput()))->slice(0, -1)->map(function ($logLine) { + [$commits, $name] = explode("\t", trim($logLine)); + return compact('commits', 'name'); + }); + + return $this; + } + /** * Check if the given version is in the published versions. * diff --git a/src/Http/Controllers/DocumentationController.php b/src/Http/Controllers/DocumentationController.php index 6657cfce..a129716b 100644 --- a/src/Http/Controllers/DocumentationController.php +++ b/src/Http/Controllers/DocumentationController.php @@ -84,6 +84,7 @@ public function show($version, $page = null) 'versions' => $documentation->publishedVersions, 'currentSection' => $documentation->currentSection, 'canonical' => $documentation->canonical, + 'authors' => $documentation->authors, ], $documentation->statusCode); } } diff --git a/src/Traits/HasDocumentationAttributes.php b/src/Traits/HasDocumentationAttributes.php index 79ea8fdf..a52937eb 100644 --- a/src/Traits/HasDocumentationAttributes.php +++ b/src/Traits/HasDocumentationAttributes.php @@ -16,6 +16,7 @@ trait HasDocumentationAttributes protected $statusCode = 200; protected $publishedVersions; protected $defaultVersionUrl; + protected $authors; /** * @return string @@ -88,4 +89,12 @@ public function getPublishedVersionsAttribute() { return $this->publishedVersions; } + + /** + * @return array + */ + public function getAuthorsAttribute() + { + return $this->authors; + } } From f3bb1d75e37065e95e0f73750da18ee97fc12c65 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Mon, 10 Jan 2022 19:36:02 +0200 Subject: [PATCH 02/16] Display authors information in the doc page --- resources/views/docs.blade.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/resources/views/docs.blade.php b/resources/views/docs.blade.php index 57d435e1..e226592d 100644 --- a/resources/views/docs.blade.php +++ b/resources/views/docs.blade.php @@ -5,6 +5,15 @@ @include('larecipe::partials.sidebar')
+ @if($authors) +
+ @if(($authorsCount = count($authors)) > 1) + {{$authorsCount}} authors ($authors[0]['name'] and others) + @else + By {{$authors[0]['name']}} + @endif +
+ @endif {!! $content !!} @include('larecipe::plugins.forum')
From 6808d2a3cd60852e9b978fb194c9ddb45576b451 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Mon, 1 Aug 2022 06:21:36 +0300 Subject: [PATCH 03/16] Check if authors' collection empty --- resources/views/docs.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/docs.blade.php b/resources/views/docs.blade.php index e226592d..d2ae8adf 100644 --- a/resources/views/docs.blade.php +++ b/resources/views/docs.blade.php @@ -5,7 +5,7 @@ @include('larecipe::partials.sidebar')
- @if($authors) + @if($authors && !$authors->isEmpty())
@if(($authorsCount = count($authors)) > 1) {{$authorsCount}} authors ($authors[0]['name'] and others) From 3acd0523cc01bdc7ba7672ab04886c01fa7f31c2 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Mon, 1 Aug 2022 06:25:26 +0300 Subject: [PATCH 04/16] Separate git logic to another file and check for git installation --- src/DocumentationRepository.php | 15 +++++++++------ src/Services/GitService.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/Services/GitService.php diff --git a/src/DocumentationRepository.php b/src/DocumentationRepository.php index a3332a9e..6dc4e048 100644 --- a/src/DocumentationRepository.php +++ b/src/DocumentationRepository.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Concerns\HasAttributes; use BinaryTorch\LaRecipe\Traits\HasDocumentationAttributes; use Symfony\Component\Process\Process; +use BinaryTorch\LaRecipe\Services\GitService; class DocumentationRepository { @@ -124,13 +125,15 @@ protected function prepareCanonical() protected function prepareAuthors() { + $gitService = new GitService(); + + if (!$gitService->isGitInstalled()) { + return $this; + } + $pagePath = base_path(config('larecipe.docs.path').'/'.$this->version.'/'.$this->sectionPage.'.md'); - $process = new Process(['git', 'shortlog', '-sn', 'HEAD', '--', $pagePath]); - $process->run(); - $this->authors = collect(explode("\n", $process->getOutput()))->slice(0, -1)->map(function ($logLine) { - [$commits, $name] = explode("\t", trim($logLine)); - return compact('commits', 'name'); - }); + + $this->authors = $gitService->getFileShortLog($pagePath); return $this; } diff --git a/src/Services/GitService.php b/src/Services/GitService.php new file mode 100644 index 00000000..9a294210 --- /dev/null +++ b/src/Services/GitService.php @@ -0,0 +1,29 @@ +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'); + }); +; + } +} \ No newline at end of file From 77f20d83ef5f3b3892b0f19f399d0f9afa6588e8 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Mon, 1 Aug 2022 06:30:34 +0300 Subject: [PATCH 05/16] Allow disabling git authors feature --- publishable/config/larecipe.php | 13 ++++++++++++- src/DocumentationRepository.php | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/publishable/config/larecipe.php b/publishable/config/larecipe.php index 7cef328c..d6f5b4a1 100644 --- a/publishable/config/larecipe.php +++ b/publishable/config/larecipe.php @@ -204,5 +204,16 @@ 'replacement' => '', ] ] - ] + ], + + /* + |-------------------------------------------------------------------------- + | Other Options + |-------------------------------------------------------------------------- + */ + + 'git' => [ + 'enabled' => true, + ], + ]; diff --git a/src/DocumentationRepository.php b/src/DocumentationRepository.php index 6dc4e048..f7ea76c0 100644 --- a/src/DocumentationRepository.php +++ b/src/DocumentationRepository.php @@ -127,7 +127,7 @@ protected function prepareAuthors() { $gitService = new GitService(); - if (!$gitService->isGitInstalled()) { + if (!$gitService->isGitInstalled() || !config('larecipe.git.enabled')) { return $this; } From f1ce23bc4fcff2a601abddb75f58ffd02822881c Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 18:28:16 +0300 Subject: [PATCH 06/16] Initialize authors feature test class --- tests/Feature/DisplayGitAuthorsTest.php | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/Feature/DisplayGitAuthorsTest.php diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php new file mode 100644 index 00000000..14a3490c --- /dev/null +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -0,0 +1,26 @@ + Date: Sun, 7 Aug 2022 18:50:38 +0300 Subject: [PATCH 07/16] Use DI to inject GitService into the controller --- src/Contracts/GitService.php | 12 ++++++++++++ src/DocumentationRepository.php | 14 +++++++------- src/LaRecipeServiceProvider.php | 3 +++ src/Services/GitService.php | 3 ++- 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/Contracts/GitService.php diff --git a/src/Contracts/GitService.php b/src/Contracts/GitService.php new file mode 100644 index 00000000..69f824c3 --- /dev/null +++ b/src/Contracts/GitService.php @@ -0,0 +1,12 @@ +documentation = $documentation; + $this->gitService = $gitService; $this->docsRoute = route('larecipe.index'); $this->defaultVersion = config('larecipe.versions.default'); @@ -125,15 +127,13 @@ protected function prepareCanonical() protected function prepareAuthors() { - $gitService = new GitService(); - - if (!$gitService->isGitInstalled() || !config('larecipe.git.enabled')) { + if (!$this->gitService->isGitInstalled() || !config('larecipe.git.enabled')) { return $this; } $pagePath = base_path(config('larecipe.docs.path').'/'.$this->version.'/'.$this->sectionPage.'.md'); - $this->authors = $gitService->getFileShortLog($pagePath); + $this->authors = $this->gitService->getFileShortLog($pagePath); return $this; } diff --git a/src/LaRecipeServiceProvider.php b/src/LaRecipeServiceProvider.php index 2732295b..b105f2a4 100644 --- a/src/LaRecipeServiceProvider.php +++ b/src/LaRecipeServiceProvider.php @@ -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 { @@ -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); diff --git a/src/Services/GitService.php b/src/Services/GitService.php index 9a294210..3bddf024 100644 --- a/src/Services/GitService.php +++ b/src/Services/GitService.php @@ -2,10 +2,11 @@ namespace BinaryTorch\LaRecipe\Services; +use BinaryTorch\LaRecipe\Contracts\GitService as GitServiceContract; use Symfony\Component\Process\Process; use Illuminate\Support\Collection; -class GitService +class GitService implements GitServiceContract { public function isGitInstalled(): bool { From 084231d3c5880aa1614ee138952219dfd5b0e1cb Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:19:28 +0300 Subject: [PATCH 08/16] Create dummy git service for testing --- tests/Fixtures/DummyGitService.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/Fixtures/DummyGitService.php diff --git a/tests/Fixtures/DummyGitService.php b/tests/Fixtures/DummyGitService.php new file mode 100644 index 00000000..de06d8d0 --- /dev/null +++ b/tests/Fixtures/DummyGitService.php @@ -0,0 +1,27 @@ +isGitInstalled = $isGitInstalled; + $this->authorsArray = $authorsArray; + } + + public function isGitInstalled(): bool + { + return $this->isGitInstalled; + } + + public function getFileShortlog(string $filePath): Collection + { + return collect($this->authorsArray); + } +} \ No newline at end of file From 5230fce786dc04e25b819c233a1a953891241fb9 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:19:38 +0300 Subject: [PATCH 09/16] implement testing git config check --- tests/Feature/DisplayGitAuthorsTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php index 14a3490c..05fb32d2 100644 --- a/tests/Feature/DisplayGitAuthorsTest.php +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -3,13 +3,22 @@ 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 { /** @test */ public function if_git_config_is_not_enabled_page_will_be_displayed_without_errors() { - # code... + Config::set('larecipe.git.enabled', false); + + App::instance(GitServiceContract::class, new DummyGitService(true, [])); + + $this->get('/docs/1.0') + ->assertViewHas('authors', null); } /** @test */ From 4d0594fe9a9c9073c3a154698a715765de879480 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:23:38 +0300 Subject: [PATCH 10/16] Implement showing page test when git is disabled --- tests/Feature/DisplayGitAuthorsTest.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php index 05fb32d2..ee923aba 100644 --- a/tests/Feature/DisplayGitAuthorsTest.php +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -10,6 +10,13 @@ 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() { @@ -18,13 +25,18 @@ public function if_git_config_is_not_enabled_page_will_be_displayed_without_erro App::instance(GitServiceContract::class, new DummyGitService(true, [])); $this->get('/docs/1.0') + ->assertOk() ->assertViewHas('authors', null); } /** @test */ public function if_git_is_not_installed_page_will_be_displayed_without_errors() { - # code... + App::instance(GitServiceContract::class, new DummyGitService(false, [])); + + $this->get('/docs/1.0') + ->assertOk() + ->assertViewHas('authors', null); } /** @test */ From 668029412409b212b58e9cd0420c7492a5010f2b Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:34:25 +0300 Subject: [PATCH 11/16] Fix assertViewHas usage --- tests/Feature/DisplayGitAuthorsTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php index ee923aba..6d7b43e9 100644 --- a/tests/Feature/DisplayGitAuthorsTest.php +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -26,7 +26,9 @@ public function if_git_config_is_not_enabled_page_will_be_displayed_without_erro $this->get('/docs/1.0') ->assertOk() - ->assertViewHas('authors', null); + ->assertViewHas('authors', function($authors) { + return is_null($authors); + }); } /** @test */ @@ -36,7 +38,9 @@ public function if_git_is_not_installed_page_will_be_displayed_without_errors() $this->get('/docs/1.0') ->assertOk() - ->assertViewHas('authors', null); + ->assertViewHas('authors', function($authors) { + return is_null($authors); + }); } /** @test */ From e822009f792b697ad7055efbd01c7ebcbb7dbb7f Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:35:39 +0300 Subject: [PATCH 12/16] Implement getting page with empty authors --- tests/Feature/DisplayGitAuthorsTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php index 6d7b43e9..e9410941 100644 --- a/tests/Feature/DisplayGitAuthorsTest.php +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -46,6 +46,14 @@ public function if_git_is_not_installed_page_will_be_displayed_without_errors() /** @test */ public function if_git_is_installed_but_no_authors_are_found_page_will_be_displayed_without_errors() { - # code... + Config::set('larecipe.git.enabled', true); + + App::instance(GitServiceContract::class, new DummyGitService(true, [])); + + $response = $this->get('/docs/1.0'); + $response->assertOk(); + $response->assertViewHas('authors', function($authors) { + return $authors->isEmpty(); + }); } } From 1267849d4dd511a29cace940fb6c589dd98def09 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:40:31 +0300 Subject: [PATCH 13/16] Add test to check if one author name is dispalyed --- tests/Feature/DisplayGitAuthorsTest.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php index e9410941..a23d84f7 100644 --- a/tests/Feature/DisplayGitAuthorsTest.php +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -46,8 +46,6 @@ public function if_git_is_not_installed_page_will_be_displayed_without_errors() /** @test */ public function if_git_is_installed_but_no_authors_are_found_page_will_be_displayed_without_errors() { - Config::set('larecipe.git.enabled', true); - App::instance(GitServiceContract::class, new DummyGitService(true, [])); $response = $this->get('/docs/1.0'); @@ -56,4 +54,18 @@ public function if_git_is_installed_but_no_authors_are_found_page_will_be_displa return $authors->isEmpty(); }); } + + /** @test */ + public function check_if_one_author_displayed_on_view() { + App::instance(GitServiceContract::class, new DummyGitService(true, [ + [ + 'name' => 'The Tester', + 'commits' => 1, + ] + ])); + + $this->get('/docs/1.0') + ->assertOk() + ->assertSee('By The Tester'); + } } From 57da03d77aa7141b8769d66ee3f04a66fd1cdc50 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:44:30 +0300 Subject: [PATCH 14/16] Fix displayiing contributer name --- resources/views/docs.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/docs.blade.php b/resources/views/docs.blade.php index d2ae8adf..0a3fecae 100644 --- a/resources/views/docs.blade.php +++ b/resources/views/docs.blade.php @@ -8,7 +8,7 @@ @if($authors && !$authors->isEmpty())
@if(($authorsCount = count($authors)) > 1) - {{$authorsCount}} authors ($authors[0]['name'] and others) + {{$authorsCount}} authors ({{$authors[0]['name']}} and others) @else By {{$authors[0]['name']}} @endif From e8ee693e04313cdac0089caedde66f021b4402b4 Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:56:34 +0300 Subject: [PATCH 15/16] Sort authors by most commits --- src/DocumentationRepository.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DocumentationRepository.php b/src/DocumentationRepository.php index a7446f44..2045d713 100644 --- a/src/DocumentationRepository.php +++ b/src/DocumentationRepository.php @@ -133,7 +133,10 @@ protected function prepareAuthors() $pagePath = base_path(config('larecipe.docs.path').'/'.$this->version.'/'.$this->sectionPage.'.md'); - $this->authors = $this->gitService->getFileShortLog($pagePath); + $this->authors = $this->gitService + ->getFileShortLog($pagePath) + ->sortByDesc('commits') + ->values(); return $this; } From ae161758a9e69a392d666b6459f42d2a9808ea0d Mon Sep 17 00:00:00 2001 From: Ammar Al-Khawaldeh Date: Sun, 7 Aug 2022 19:56:54 +0300 Subject: [PATCH 16/16] Test previewing multiple authors --- tests/Feature/DisplayGitAuthorsTest.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/Feature/DisplayGitAuthorsTest.php b/tests/Feature/DisplayGitAuthorsTest.php index a23d84f7..cc5a5c29 100644 --- a/tests/Feature/DisplayGitAuthorsTest.php +++ b/tests/Feature/DisplayGitAuthorsTest.php @@ -56,7 +56,7 @@ public function if_git_is_installed_but_no_authors_are_found_page_will_be_displa } /** @test */ - public function check_if_one_author_displayed_on_view() { + public function check_if_one_author_is_displayed_on_the_view() { App::instance(GitServiceContract::class, new DummyGitService(true, [ [ 'name' => 'The Tester', @@ -68,4 +68,26 @@ public function check_if_one_author_displayed_on_view() { ->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)'); + } }