Skip to content

Commit

Permalink
[4.x] Use authenticated user in Git events even when queued (#9225)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Jan 5, 2024
1 parent a001e7d commit 720d8a8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
13 changes: 13 additions & 0 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
namespace Statamic\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Statamic\Contracts\Auth\User as UserContract;
use Statamic\Facades\User;

abstract class Event
{
use Dispatchable;

public ?UserContract $authenticatedUser;

public static function dispatch()
{
$event = new static(...func_get_args());

$event->authenticatedUser = User::current();

return event($event);
}
}
14 changes: 2 additions & 12 deletions src/Git/CommitJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,18 @@ class CommitJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;

/**
* Commit message.
*
* @var string|null
*/
public $message;

/**
* Create a new job instance.
*
* @param string|null $message
*/
public function __construct($message = null)
public function __construct(public $message = null, public $committer = null)
{
$this->message = $message;
}

/**
* Execute the job.
*/
public function handle()
{
Git::commit($this->message);
Git::as($this->committer)->commit($this->message);
}
}
28 changes: 22 additions & 6 deletions src/Git/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Illuminate\Filesystem\Filesystem;
use Statamic\Console\Processes\Git as GitProcess;
use Statamic\Contracts\Auth\User as UserContract;
use Statamic\Facades\Antlers;
use Statamic\Facades\Path;
use Statamic\Facades\User;
use Statamic\Support\Str;

class Git
{
private ?UserContract $authenticatedUser;

/**
* Instantiate git tracked content manager.
*/
Expand Down Expand Up @@ -54,6 +57,18 @@ public function statuses()
return $statuses->isNotEmpty() ? $statuses : null;
}

/**
* Act as a specific user.
*/
public function as(?UserContract $user): static
{
$clone = clone $this;

$clone->authenticatedUser = $user;

return $clone;
}

/**
* Git add and commit all tracked content, using configured commands.
*/
Expand All @@ -74,7 +89,7 @@ public function dispatchCommit($message = null)
$message = null;
}

CommitJob::dispatch($message)
CommitJob::dispatch($message, $this->authenticatedUser())
->onConnection(config('statamic.git.queue_connection'))
->delay($delayInMinutes ?? null);
}
Expand All @@ -92,9 +107,7 @@ public function gitUserName()
return $default;
}

$currentUser = User::current();

return $currentUser ? $currentUser->name() : $default;
return $this->authenticatedUser()?->name() ?? $default;
}

/**
Expand All @@ -110,9 +123,12 @@ public function gitUserEmail()
return $default;
}

$currentUser = User::current();
return $this->authenticatedUser()?->email() ?? $default;
}

return $currentUser ? $currentUser->email() : $default;
private function authenticatedUser(): ?UserContract
{
return $this->authenticatedUser ?? User::current();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Git/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function commit($event)
return;
}

Git::dispatchCommit(
Git::as($event->authenticatedUser)->dispatchCommit(
$event instanceof ProvidesCommitMessage
? $event->commitMessage()
: null
Expand Down
7 changes: 6 additions & 1 deletion tests/Git/GitEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void
Config::set('statamic.git.enabled', true);

$this->actingAs(
User::make()
$user = User::make()
->id('chewbacca')
->email('[email protected]')
->data(['name' => 'Chewbacca'])
Expand All @@ -47,11 +47,13 @@ public function setUp(): void
Storage::fake('test');

Git::shouldReceive('statuses');
Git::shouldReceive('as')->with($user)->andReturnSelf();
}

/** @test */
public function it_doesnt_commit_when_git_is_disabled()
{
Git::shouldReceive('as')->never();
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->never();

Expand All @@ -66,6 +68,7 @@ public function it_doesnt_commit_when_git_is_disabled()
/** @test */
public function it_doesnt_commit_when_automatic_is_disabled()
{
Git::shouldReceive('as')->never();
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->never();

Expand All @@ -80,6 +83,7 @@ public function it_doesnt_commit_when_automatic_is_disabled()
/** @test */
public function it_doesnt_commit_ignored_events()
{
Git::shouldReceive('as')->never();
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once();

Expand All @@ -96,6 +100,7 @@ public function it_doesnt_commit_ignored_events()
/** @test */
public function it_doesnt_commit_when_event_subscriber_is_disabled()
{
Git::shouldReceive('as')->never();
Git::shouldReceive('dispatchCommit')->with('Collection saved')->never();
Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once();

Expand Down
10 changes: 10 additions & 0 deletions tests/Git/GitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ public function it_gets_git_user_info()
$this->assertEquals('Chewy', Git::gitUserName());
$this->assertEquals('[email protected]', Git::gitUserEmail());

$han = User::make()
->email('[email protected]')
->data(['name' => 'Han Solo'])
->makeSuper();

$this->assertEquals('Han Solo', Git::as($han)->gitUserName());
$this->assertEquals('[email protected]', Git::as($han)->gitUserEmail());
$this->assertEquals('Chewy', Git::gitUserName());
$this->assertEquals('[email protected]', Git::gitUserEmail());

Config::set('statamic.git.use_authenticated', false);

$this->assertEquals('Spock', Git::gitUserName());
Expand Down
2 changes: 2 additions & 0 deletions tests/Stache/Stores/AssetContainersStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Statamic\Facades\Path;
use Statamic\Stache\Stache;
use Statamic\Stache\Stores\AssetContainersStore;
use Statamic\Stache\Stores\UsersStore;
use Tests\TestCase;

class AssetContainersStoreTest extends TestCase
Expand Down Expand Up @@ -111,6 +112,7 @@ public function it_saves_to_disk()
// irrelevant for this test but gets called during saving
Facades\Stache::shouldReceive('shouldUpdateIndexes')->andReturnTrue();
Facades\Stache::shouldReceive('duplicates')->andReturn(optional());
Facades\Stache::shouldReceive('store')->with('users')->andReturn((new UsersStore((new Stache)->sites(['en']), app('files')))->directory($this->tempDir));

$container = Facades\AssetContainer::make('new')
->title('New Container');
Expand Down

0 comments on commit 720d8a8

Please sign in to comment.