Skip to content

Commit

Permalink
Lint & fix the tests failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
tannguyen04 committed Mar 15, 2024
1 parent 2609ced commit 5ee3103
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 94 deletions.
108 changes: 44 additions & 64 deletions src/Artifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class Artifact {

Expand All @@ -22,125 +23,91 @@ class Artifact {
const GIT_REMOTE_NAME = 'dst';

/**
* Git runner.
*
* @var GitArtifactGit
* Represent to current repository.
*/
protected GitArtifactGit $git;
protected GitArtifactGitRepository $gitRepository;

/**
* Represent to current repository.
*
* @var GitArtifactGitRepository
* Source path of git repository.
*/
protected GitArtifactGitRepository $gitRepository;
protected string $sourcePathGitRepository = '';

/**
* Mode in which current build is going to run.
*
* Available modes: branch, force-push, diff.
*
* @var string
*/
protected string $mode;

/**
* Original branch in current repository.
*
* @var string|null
*/
protected ?string $originalBranch = NULL;
protected string $originalBranch = '';

/**
* Destination branch with optional tokens.
*
* @var string|null
*/
protected ?string $destinationBranch = NULL;
protected string $destinationBranch = '';

/**
* Local branch where artifact will be built.
*
* @var string|null
*/
protected ?string $artifactBranch = NULL;
protected string $artifactBranch = '';

/**
* Remote name.
*
* @var string|null
*/
protected ?string $remoteName = NULL;
protected string $remoteName = '';

/**
* Remote URL includes uri or local path.
*
* @var string|null
*/
protected ?string $remoteUrl = NULL;
protected string $remoteUrl = '';

/**
* Gitignore file to be used during artifact creation.
*
* If not set, the current `.gitignore` will be used, if any.
*
* @var string|null
*/
protected ?string $gitignoreFile = NULL;

/**
* Commit message with optional tokens.
*
* @var string|null
*/
protected ?string $message = NULL;
protected string $message = '';

/**
* Flag to specify if push is required or should be using dry run.
*
* @var bool
*/
protected bool $needsPush = FALSE;

/**
* Flag to specify if cleanup is required to run after the build.
*
* @var bool
*/
protected bool $needCleanup = TRUE;

/**
* Path to report file.
*
* @var string|null
*/
protected ?string $reportFile = NULL;
protected string $reportFile = '';

/**
* Flag to show changes made to the repo by the build in the output.
*
* @var bool
*/
protected bool $showChanges = FALSE;

/**
* Artifact build result.
*
* @var bool
*/
protected bool $result = FALSE;

/**
* Flag to print debug information.
*
* @var bool
*/
protected bool $debug = FALSE;

/**
* Internal option to set current timestamp.
*
* @var int
*/
protected int $now;

Expand All @@ -155,12 +122,14 @@ class Artifact {
* Output.
*/
public function __construct(
GitArtifactGit $git,
/**
* Git runner.
*/
protected GitArtifactGit $git,
Filesystem $fsFileSystem,
protected OutputInterface $output,
) {
$this->fsFileSystem = $fsFileSystem;
$this->git = $git;
}

/**
Expand Down Expand Up @@ -208,9 +177,9 @@ public function artifact(string $remote, array $opts = [
]): void {
try {
$error = NULL;

$this->checkRequirements();
$this->resolveOptions($remote, $opts);

// Now we have all what we need.
// Let process artifact function.
$this->printDebug('Debug messages enabled');
Expand All @@ -232,7 +201,7 @@ public function artifact(string $remote, array $opts = [
$error = $exception->getMessage();
}

if ($this->reportFile) {
if (!empty($this->reportFile)) {
$this->dumpReport();
}

Expand All @@ -249,6 +218,16 @@ public function artifact(string $remote, array $opts = [
}
}

/**
* Get source path git repository.
*
* @return string
* Source path.
*/
public function getSourcePathGitRepository(): string {
return $this->sourcePathGitRepository;
}

/**
* Branch mode.
*
Expand Down Expand Up @@ -290,7 +269,7 @@ protected function prepareArtifact(): void {
// Remove sub-repositories.
$this->removeSubReposInGitRepository();
// Disable local exclude.
$this->disableLocalExclude($this->gitRepository->getRepositoryPath());
$this->disableLocalExclude($this->getSourcePathGitRepository());
// Add files.
$this->addAllFilesInGitRepository();
// Remove other files.
Expand Down Expand Up @@ -339,22 +318,21 @@ protected function addAllFilesInGitRepository(): void {
if (!empty($this->gitignoreFile)) {
$this->replaceGitignoreInGitRepository($this->gitignoreFile);
$this->gitRepository->addAllChanges();
$this->removeIgnoredFiles($this->gitRepository->getRepositoryPath());
$this->removeIgnoredFiles($this->getSourcePathGitRepository());
}
else {
$this->gitRepository->addAllChanges();
}
}


/**
* Cleanup after build.
*
* @throws \Exception
*/
protected function cleanup(): void {
$this
->restoreLocalExclude($this->gitRepository->getRepositoryPath());
->restoreLocalExclude($this->getSourcePathGitRepository());

$this
->gitRepository
Expand All @@ -377,7 +355,7 @@ protected function cleanup(): void {
protected function doPush(): void {
try {
$options = $this->mode === self::modeForcePush() ? ['--force'] : [];
$refSpec = "refs/heads/$this->artifactBranch:refs/heads/$this->destinationBranch";
$refSpec = sprintf('refs/heads/%s:refs/heads/%s', $this->artifactBranch, $this->destinationBranch);
$this
->gitRepository
->push([$this->remoteName, $refSpec], $options);
Expand All @@ -401,10 +379,12 @@ protected function doPush(): void {
/**
* Resolve and validate CLI options values into internal values.
*
* @param string $remote
* Remote URL.
* @param array $options
* Array of CLI options.
*
* @throws \Exception
* @throws \CzProject\GitPhp\GitException
*
* @phpstan-ignore-next-line
*/
Expand All @@ -416,16 +396,16 @@ protected function resolveOptions(string $remote, array $options): void {
$this->showChanges = !empty($options['show-changes']);
$this->needCleanup = empty($options['no-cleanup']);
$this->needsPush = !empty($options['push']);
$this->reportFile = empty($options['report']) ? NULL : $options['report'];
$this->reportFile = empty($options['report']) ? '' : $options['report'];
$this->now = empty($options['now']) ? time() : (int) $options['now'];
$this->debug = !empty($options['debug']);
$this->remoteName = self::GIT_REMOTE_NAME;
$this->remoteUrl = $remote;
$this->setMode($options['mode'], $options);

// Handle some complex options.
// Get git repository source from option.
$srcPath = empty($options['src']) ? $this->fsGetRootDir() : $this->fsGetAbsolutePath($options['src']);
$this->sourcePathGitRepository = $srcPath;
// Setup Git repository from source path.
$this->initGitRepository($srcPath);
// Set original, destination, artifact branch name.
Expand Down Expand Up @@ -468,7 +448,7 @@ protected function showInfo(): void {
$lines[] = ('----------------------------------------------------------------------');
$lines[] = (' Build timestamp: ' . date('Y/m/d H:i:s', $this->now));
$lines[] = (' Mode: ' . $this->mode);
$lines[] = (' Source repository: ' . $this->gitRepository->getRepositoryPath());
$lines[] = (' Source repository: ' . $this->getSourcePathGitRepository());
$lines[] = (' Remote repository: ' . $this->remoteUrl);
$lines[] = (' Remote branch: ' . $this->destinationBranch);
$lines[] = (' Gitignore file: ' . ($this->gitignoreFile ?: 'No'));
Expand All @@ -486,7 +466,7 @@ protected function dumpReport(): void {
$lines[] = '----------------------------------------------------------------------';
$lines[] = ' Build timestamp: ' . date('Y/m/d H:i:s', $this->now);
$lines[] = ' Mode: ' . $this->mode;
$lines[] = ' Source repository: ' . $this->gitRepository->getRepositoryPath();
$lines[] = ' Source repository: ' . $this->getSourcePathGitRepository();
$lines[] = ' Remote repository: ' . $this->remoteUrl;
$lines[] = ' Remote branch: ' . $this->destinationBranch;
$lines[] = ' Gitignore file: ' . ($this->gitignoreFile ?: 'No');
Expand Down Expand Up @@ -631,7 +611,7 @@ protected function checkRequirements(): void {
* Path to new gitignore to replace current file with.
*/
protected function replaceGitignoreInGitRepository(string $filename): void {
$path = $this->gitRepository->getRepositoryPath();
$path = $this->getSourcePathGitRepository();
$this->printDebug('Replacing .gitignore: %s with %s', $path . DIRECTORY_SEPARATOR . '.gitignore', $filename);
$this->fsFileSystem->copy($filename, $path . DIRECTORY_SEPARATOR . '.gitignore', TRUE);
$this->fsFileSystem->remove($filename);
Expand Down Expand Up @@ -746,7 +726,7 @@ protected function restoreLocalExclude(string $path): void {
* If removal command finished with an error.
*/
protected function removeIgnoredFiles(string $location, string $gitignorePath = NULL): void {
$location = $this->gitRepository->getRepositoryPath();
$location = $this->getSourcePathGitRepository();
$gitignorePath = $gitignorePath ?: $location . DIRECTORY_SEPARATOR . '.gitignore';

$gitignoreContent = file_get_contents($gitignorePath);
Expand All @@ -761,7 +741,7 @@ protected function removeIgnoredFiles(string $location, string $gitignorePath =

$files = $this
->gitRepository
->lsFiles(['--directory', '-i', '-c', "--exclude-from=$gitignorePath"]);
->lsFiles(['--directory', '-i', '-c', '--exclude-from=' . $gitignorePath]);

if (!empty($files)) {
$files = array_filter($files);
Expand All @@ -788,7 +768,7 @@ protected function removeOtherFilesInGitRepository(): void {
if (!empty($files)) {
$files = array_filter($files);
foreach ($files as $file) {
$fileName = $this->gitRepository->getRepositoryPath() . DIRECTORY_SEPARATOR . $file;
$fileName = $this->getSourcePathGitRepository() . DIRECTORY_SEPARATOR . $file;
$this->printDebug('Removing other file %s', $fileName);
$this->fsFileSystem->remove($fileName);
}
Expand All @@ -806,7 +786,7 @@ protected function removeSubReposInGitRepository(): void {
->ignoreDotFiles(FALSE)
->ignoreVCS(FALSE)
->depth('>0')
->in($this->gitRepository->getRepositoryPath());
->in($this->getSourcePathGitRepository());

$dirs = iterator_to_array($dirs->directories());

Expand Down
19 changes: 0 additions & 19 deletions src/Commands/ArtifactCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@

namespace DrevOps\GitArtifact\Commands;

use CzProject\GitPhp\Git;
use DrevOps\GitArtifact\Artifact;
use DrevOps\GitArtifact\GitArtifactGit;
use DrevOps\GitArtifact\GitArtifactGitRepository;
use GitWrapper\EventSubscriber\GitLoggerEventSubscriber;
use GitWrapper\GitWrapper;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -96,22 +89,10 @@ protected function configure(): void {
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int {

// $optionDebug = $input->getOption('debug');
//
// if (($optionDebug || $output->isDebug())) {
// $logger = new Logger('git');
// $logger->pushHandler(new StreamHandler('php://stdout', Level::Debug));
// }

$fileSystem = new Filesystem();
$git = new GitArtifactGit();
// $repo = $git->open('.');
// $repo->setRemoteUrl('aaaa' , 'asd');
// die;
$artifact = new Artifact($git, $fileSystem, $output);
$remote = $input->getArgument('remote');

// @phpstan-ignore-next-line
$artifact->artifact($remote, $input->getOptions());

Expand Down
2 changes: 0 additions & 2 deletions src/FilesystemTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ trait FilesystemTrait {

/**
* Current directory where call originated.
*
* @var string
*/
protected string $fsRootDir;

Expand Down
Loading

0 comments on commit 5ee3103

Please sign in to comment.