Skip to content

Commit

Permalink
use "as" instead of passing around committer - also fixes current use…
Browse files Browse the repository at this point in the history
…r not applied when calling commit without event
  • Loading branch information
jasonvarga committed Jan 5, 2024
1 parent b9db5b6 commit caf98e1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Git/CommitJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function __construct(public $message = null, public $committer = null)
*/
public function handle()
{
Git::commit($this->message, $this->committer);
Git::as($this->committer)->commit($this->message);
}
}
49 changes: 31 additions & 18 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,63 +57,73 @@ public function statuses()
return $statuses->isNotEmpty() ? $statuses : null;
}

public function as($user)
{
$this->authenticatedUser = $user;

return $this;
}

/**
* Git add and commit all tracked content, using configured commands.
*/
public function commit($message = null, $committer = null)
public function commit($message = null)
{
$this->groupTrackedContentPathsByRepo()->each(function ($paths, $gitRoot) use ($message, $committer) {
$this->runConfiguredCommands($gitRoot, $paths, $message ?? __('Content saved'), $committer);
$this->groupTrackedContentPathsByRepo()->each(function ($paths, $gitRoot) use ($message) {
$this->runConfiguredCommands($gitRoot, $paths, $message ?? __('Content saved'));
});
}

/**
* Dispatch commit job to queue.
*/
public function dispatchCommit($message = null, $committer = null)
public function dispatchCommit($message = null)
{
if ($delay = config('statamic.git.dispatch_delay')) {
$delayInMinutes = now()->addMinutes($delay);
$message = null;
}

CommitJob::dispatch($message, $committer)
CommitJob::dispatch($message, $this->authenticatedUser())
->onConnection(config('statamic.git.queue_connection'))
->delay($delayInMinutes ?? null);
}

/**
* Get git user name.
*
* @param \Statamic\Contracts\Auth\User|null $committer
* @return string
*/
public function gitUserName($committer = null)
public function gitUserName()
{
$default = config('statamic.git.user.name');

if (! config('statamic.git.use_authenticated')) {
return $default;
}

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

/**
* Get git user email.
*
* @param \Statamic\Contracts\Auth\User|null $committer
* @return string
*/
public function gitUserEmail($committer = null)
public function gitUserEmail()
{
$default = config('statamic.git.user.email');

if (! config('statamic.git.use_authenticated')) {
return $default;
}

return $committer?->email() ?? $default;
return $this->authenticatedUser()?->email() ?? $default;
}

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

/**
Expand Down Expand Up @@ -200,9 +213,9 @@ protected function ensureAbsolutePath($path)
* @param mixed $paths
* @param mixed $message
*/
protected function runConfiguredCommands($gitRoot, $paths, $message, $committer = null)
protected function runConfiguredCommands($gitRoot, $paths, $message)
{
$this->getParsedCommands($paths, $message, $committer)->each(function ($command) use ($gitRoot) {
$this->getParsedCommands($paths, $message)->each(function ($command) use ($gitRoot) {
GitProcess::create($gitRoot)->run($command);
});

Expand All @@ -217,9 +230,9 @@ protected function runConfiguredCommands($gitRoot, $paths, $message, $committer
* @param mixed $paths
* @param mixed $message
*/
protected function getParsedCommands($paths, $message, $committer = null)
protected function getParsedCommands($paths, $message)
{
$context = $this->getCommandContext($paths, $message, $committer);
$context = $this->getCommandContext($paths, $message);

return collect(config('statamic.git.commands'))->map(function ($command) use ($context) {
return Antlers::parse($command, $context);
Expand All @@ -233,13 +246,13 @@ protected function getParsedCommands($paths, $message, $committer = null)
* @param string $message
* @return array
*/
protected function getCommandContext($paths, $message, $committer = null)
protected function getCommandContext($paths, $message)
{
return [
'paths' => collect($paths)->implode(' '),
'message' => $this->shellEscape($message),
'name' => $this->shellEscape($this->gitUserName($committer)),
'email' => $this->shellEscape($this->gitUserEmail($committer)),
'name' => $this->shellEscape($this->gitUserName()),
'email' => $this->shellEscape($this->gitUserEmail()),
];
}

Expand Down
8 changes: 3 additions & 5 deletions src/Git/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Statamic\Events\Concerns\ListensForContentEvents;
use Statamic\Events\Subscriber as StatamicSubscriber;
use Statamic\Facades\Git;
use Statamic\Facades\User;

class Subscriber extends StatamicSubscriber
{
Expand Down Expand Up @@ -37,11 +36,10 @@ public function commit($event)
return;
}

Git::dispatchCommit(
message: $event instanceof ProvidesCommitMessage
Git::as($event->authenticatedUser)->dispatchCommit(
$event instanceof ProvidesCommitMessage
? $event->commitMessage()
: null,
committer: $event->authenticatedUser ?? User::current(),
: null
);
}

Expand Down
26 changes: 19 additions & 7 deletions tests/Git/GitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,30 @@ public function it_gets_git_user_info()
$this->assertEquals('Spock', Git::gitUserName());
$this->assertEquals('[email protected]', Git::gitUserEmail());

$user = User::make()
->email('[email protected]')
->data(['name' => 'Chewy'])
$this->actingAs(
User::make()
->email('[email protected]')
->data(['name' => 'Chewy'])
->makeSuper()
);

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

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

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

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

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

$this->assertEquals('Spock', Git::gitUserName($user));
$this->assertEquals('[email protected]', Git::gitUserEmail($user));
$this->assertEquals('Spock', Git::gitUserName());
$this->assertEquals('[email protected]', Git::gitUserEmail());
}

/** @test */
Expand Down

0 comments on commit caf98e1

Please sign in to comment.