Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Decouple Autoupdater from immediate env access
Browse files Browse the repository at this point in the history
  • Loading branch information
tehplague committed Jun 5, 2020
1 parent 5729b60 commit 96e8a7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
4 changes: 1 addition & 3 deletions autoupdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
->setVersion('@package_version@')
->setCode(function (InputInterface $input, OutputInterface $output) {
$repositoryPath = getcwd();
$gitlabProjectName = getenv('AUTOUPDATER_PROJECT_NAME');

$autoupdater = new Autoupdater($gitlabProjectName, $repositoryPath);
$autoupdater = new Autoupdater($repositoryPath);
$autoupdater->run($input, $output);

return;
});
$application->run();

39 changes: 24 additions & 15 deletions src/Autoupdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Mfc\Autoupdater;

use DateTime;
use DateTimeZone;
use Exception;
use Gitlab\Client as GitLabClient;
use Gitlab\Model\MergeRequest;
use Gitlab\Model\Project;
Expand All @@ -24,10 +26,6 @@
*/
class Autoupdater
{
/**
* @var string
*/
private $projectName;
/**
* @var string
*/
Expand Down Expand Up @@ -56,11 +54,9 @@ class Autoupdater
/**
* Autoupdater constructor.
* @param string $directory
* @param string $projectName
*/
public function __construct(string $projectName, string $directory)
public function __construct(string $directory)
{
$this->projectName = $projectName;
$this->projectRoot = $directory;
$this->loadAppConfiguration();
$this->loadProjectConfiguration($this->projectRoot . '/autoupdater.yaml');
Expand All @@ -82,6 +78,12 @@ private function loadProjectConfiguration(string $filename): void
$this->projectConfiguration = new ProjectConfiguration($filename);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
public function run(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
Expand Down Expand Up @@ -176,6 +178,11 @@ private function performUpdates(SymfonyStyle $io): void
}
}

/**
* @param SymfonyStyle $io
* @return bool
* @throws Exception
*/
private function createUpdateCommit(SymfonyStyle $io): bool
{
$io->section('Creating update commit');
Expand Down Expand Up @@ -204,13 +211,12 @@ private function createUpdateCommit(SymfonyStyle $io): bool

/**
* @return string
* @throws \Exception
* @throws Exception
*/
private static function getUpdateTitle(): string
{
$date = (new \DateTime('now', new DateTimeZone('Europe/Berlin')))->format('r');
$commitTitle = "Automatic update on ${date}";
return $commitTitle;
$date = (new DateTime('now', new DateTimeZone('Europe/Berlin')))->format('r');
return "Automatic update on ${date}";
}

/**
Expand Down Expand Up @@ -240,12 +246,11 @@ private function getUpdateMessages(string $commitTitle, bool $markdown = false):
}
}

$commitMessage = <<<MSG
return <<<MSG
${commitTitle}
${updateMessages}
MSG;
return $commitMessage;
}

private function pushUpdateCommit(SymfonyStyle $io): void
Expand All @@ -262,7 +267,7 @@ private function pushUpdateCommit(SymfonyStyle $io): void
'upstream'
]
);
} catch (\Exception $ex) {
} catch (Exception $ex) {
}

$repository->run(
Expand Down Expand Up @@ -290,11 +295,15 @@ private function pushUpdateCommit(SymfonyStyle $io): void
$repository->getWorkingCopy()->checkout('develop');
}

/**
* @param SymfonyStyle $io
* @throws Exception
*/
private function createOrUpdateMergeRequest(SymfonyStyle $io): void
{
$gitlabProject = Project::fromArray(
$this->gitlabClient,
$this->gitlabClient->projects->show($this->projectName)
$this->gitlabClient->projects->show($this->projectConfiguration->getGitlabProjectName())
);
$currentMergeRequests = $this->gitlabClient->merge_requests->all(
$gitlabProject->id,
Expand Down

0 comments on commit 96e8a7c

Please sign in to comment.