Skip to content

Commit

Permalink
Merge pull request #20 from artemeon/clean-up
Browse files Browse the repository at this point in the history
Clean up and upgrade to PHP 8
  • Loading branch information
marcreichel authored Feb 21, 2022
2 parents b8f5b6f + ce41040 commit cf48e22
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 319 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @sidler @marcreichel
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Mantis 2 Github Connector
# Mantis 2 GitHub Connector

[![Packagist Version](https://img.shields.io/packagist/v/artemeon/mantis2github)](https://packagist.org/packages/artemeon/mantis2github)
[![Packagist Downloads](https://img.shields.io/packagist/dt/artemeon/mantis2github)](https://packagist.org/packages/artemeon/mantis2github)
Expand Down Expand Up @@ -78,25 +78,25 @@ mantis2github sync 123 456 789
Read details of a GitHub issue.

```shell
mantis2github read:github [id]
mantis2github read:github <id>
```

##### Arguments

| Argument | required | Description |
|----------|----------|-----------------|
| `id` | `false` | GitHub issue id |
| `id` | `true` | GitHub issue id |

#### `read:mantis`

Read details of a Mantis issue.

```shell
mantis2github read:mantis [id]
mantis2github read:mantis <id>
```

##### Arguments

| Argument | required | Description |
|----------|----------|-----------------|
| `id` | `false` | Mantis issue id |
| `id` | `true` | Mantis issue id |
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"require": {
"php": ">=7.4",
"php": ">=8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.3.0",
"symfony/console": "^5.0",
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions mantis2github
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/env php
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Artemeon\M2G\Command\ConfigurationCommand;
use Artemeon\M2G\Command\CreateGithubIssueFromMantisIssue;
use Artemeon\M2G\Command\ReadGithubIssueCommand;
use Artemeon\M2G\Command\ReadMantisIssueCommand;
use Artemeon\M2G\Config\ConfigReader;
use Artemeon\M2G\Service\GithubConnector;
use Artemeon\M2G\Service\MantisConnector;
use Symfony\Component\Console\Application;

(new class() {
protected string $name;
Expand All @@ -18,15 +19,15 @@
$this->autoload();
$this->fetchVersion();

$configValues = (new \Artemeon\M2G\Config\ConfigReader())->read();
$githubConnector = new \Artemeon\M2G\Service\GithubConnector($configValues);
$mantisConnector = new \Artemeon\M2G\Service\MantisConnector($configValues);
$configValues = (new ConfigReader())->read();
$githubConnector = new GithubConnector($configValues);
$mantisConnector = new MantisConnector($configValues);

$app = new \Symfony\Component\Console\Application($this->name, $this->version);
$app->add(new \Artemeon\M2G\Command\ConfigurationCommand($githubConnector));
$app->add(new \Artemeon\M2G\Command\ReadMantisIssueCommand($mantisConnector));
$app->add(new \Artemeon\M2G\Command\ReadGithubIssueCommand($githubConnector));
$app->add(new \Artemeon\M2G\Command\CreateGithubIssueFromMantisIssue($mantisConnector, $githubConnector));
$app = new Application($this->name, $this->version);
$app->add(new ConfigurationCommand());
$app->add(new ReadMantisIssueCommand($mantisConnector));
$app->add(new ReadGithubIssueCommand($githubConnector));
$app->add(new CreateGithubIssueFromMantisIssue($mantisConnector, $githubConnector));
$app->run();
}

Expand Down
12 changes: 0 additions & 12 deletions src/Command/ConfigurationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Artemeon\M2G\Command;

use Artemeon\M2G\Config\ConfigReader;
use Artemeon\M2G\Service\GithubConnector;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Yaml\Yaml;

Expand All @@ -14,17 +13,6 @@ class ConfigurationCommand extends Command
protected string $configPath = __DIR__ . '/../../../config.yaml';
protected array $config = [];

private GithubConnector $githubConnector;

/**
* @param GithubConnector $mantisConnector
*/
public function __construct(GithubConnector $mantisConnector)
{
parent::__construct();
$this->githubConnector = $mantisConnector;
}

protected function configure()
{
$this->setName('configure')
Expand Down
33 changes: 15 additions & 18 deletions src/Command/CreateGithubIssueFromMantisIssue.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Artemeon\M2G\Command;

use Artemeon\M2G\Dto\GithubIssue;
use Artemeon\M2G\Dto\MantisIssue;
use Artemeon\M2G\Service\GithubConnector;
use Artemeon\M2G\Service\MantisConnector;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

use function Termwind\render;

Expand All @@ -34,7 +23,6 @@ public function __construct(MantisConnector $mantisConnector, GithubConnector $g
$this->githubConnector = $githubConnector;
}


protected function configure()
{
$this->setName('sync')
Expand Down Expand Up @@ -99,15 +87,14 @@ protected function handle(): int

$newGithubIssue = GithubIssue::fromMantisIssue($mantisIssue);

$filteredLabels = array_filter($labels, function ($label) use ($mantisIssue) {
$filteredLabels = array_values(array_filter($labels, function ($label) use ($mantisIssue) {
return strtolower($label) === strtolower($mantisIssue->getProject());
});
}));

$newGithubIssue->setLabels($filteredLabels);
$newGithubIssue = $this->githubConnector->createIssue($newGithubIssue);

try {
$newGithubIssue = $this->githubConnector->createIssue($newGithubIssue);
} catch (GuzzleException | \Exception $e) {
if ($newGithubIssue === null) {
$issues[] = [
'id' => $id,
'icon' => '<error>✕</error>',
Expand All @@ -118,7 +105,17 @@ protected function handle(): int
}

$mantisIssue->setUpstreamTicket($newGithubIssue->getIssueUrl());
$this->mantisConnector->patchUpstreamField($mantisIssue);
$patched = $this->mantisConnector->patchUpstreamField($mantisIssue);

if ($patched === false) {
$issues[] = [
'id' => $id,
'icon' => '<error>✕</error>',
'message' => '<error>Upstream ticket URL could not be updated.</error>',
'issue' => '',
];
continue;
}

$issues[] = [
'id' => $id,
Expand Down
22 changes: 5 additions & 17 deletions src/Command/ReadGithubIssueCommand.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Artemeon\M2G\Command;

Expand All @@ -32,7 +24,7 @@ public function __construct(GithubConnector $mantisConnector)
protected function configure()
{
$this->setName('read:github')
->addArgument('id', InputArgument::OPTIONAL, 'GitHub issue id')
->addArgument('id', InputArgument::REQUIRED, 'GitHub issue id')
->setDescription('Read details of a GitHub issue');
}

Expand All @@ -49,7 +41,7 @@ protected function handle(): int
{
$this->header();

$issue = $this->askForIssue();
$issue = $this->fetchIssueDetails();

terminal()->clear();

Expand Down Expand Up @@ -112,17 +104,13 @@ protected function handle(): int
return 0;
}

protected function askForIssue(): GithubIssue
protected function fetchIssueDetails(): GithubIssue
{
$id = $this->argument('id') ?? $this->ask(' GitHub Issue ID:');
$id = $this->argument('id');

if (!is_numeric($id)) {
$this->error('Please provide a valid issue id.');

if (empty($this->argument('id'))) {
$this->askForIssue();
}

exit(1);
}

Expand All @@ -134,7 +122,7 @@ protected function askForIssue(): GithubIssue
$this->error('Issue not found.');

if (empty($this->argument('id'))) {
$this->askForIssue();
$this->fetchIssueDetails();
}

exit(1);
Expand Down
32 changes: 8 additions & 24 deletions src/Command/ReadMantisIssueCommand.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Artemeon\M2G\Command;

Expand All @@ -30,7 +22,7 @@ protected function configure()
{
$this->setName('read:mantis')
->setDescription('Read details of a Mantis issue')
->addArgument('id', InputArgument::OPTIONAL, 'The issue id');
->addArgument('id', InputArgument::REQUIRED, 'The issue id');
}

protected function header(): void
Expand All @@ -46,21 +38,21 @@ protected function handle(): int
{
$this->header();

$issue = $this->askForIssue();
$issue = $this->fetchIssueDetails();

terminal()->clear();

if ($issue->getResolution() === 'open') {
if (in_array($issue->getResolution(), ['open', 'reopened'])) {
render(<<<HTML
<div class="my-1 mx-1 px-1 bg-green-500 text-gray-900">
Issue is open
Issue is {$issue->getResolution()}
</div>
HTML);
} else if ($issue->getResolution() === 'fixed') {
} else {
render(
<<<HTML
<div class="my-1 mx-1 px-1 bg-purple-500 text-gray-900">
Issue is fixed
Issue is {$issue->getResolution()}
</div>
HTML
);
Expand Down Expand Up @@ -88,17 +80,13 @@ protected function handle(): int
return 0;
}

protected function askForIssue(): ?MantisIssue
protected function fetchIssueDetails(): ?MantisIssue
{
$id = $this->argument('id') ?? $this->ask(' Mantis Issue ID:');
$id = $this->argument('id');

if (!is_numeric($id)) {
$this->error('Please provide a valid issue id.');

if (empty($this->argument('id'))) {
$this->askForIssue();
}

exit(1);
}

Expand All @@ -109,10 +97,6 @@ protected function askForIssue(): ?MantisIssue
if (!$issue) {
$this->error('Issue not found.');

if (empty($this->argument('id'))) {
$this->askForIssue();
}

exit(1);
}

Expand Down
8 changes: 0 additions & 8 deletions src/Config/ConfigReader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Artemeon\M2G\Config;

Expand Down
8 changes: 0 additions & 8 deletions src/Config/ConfigValues.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Artemeon\M2G\Config;

Expand Down
Loading

0 comments on commit cf48e22

Please sign in to comment.