Skip to content

Commit

Permalink
Merge pull request #16 from artemeon/batch-sync
Browse files Browse the repository at this point in the history
Introduce batch sync
  • Loading branch information
marcreichel authored Feb 17, 2022
2 parents 87247e5 + 344127f commit b666092
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 85 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,31 @@ mantis2github [command]

#### `sync`

Create a GitHub issue from a Mantis issue.
Create a GitHub issue from a list of Mantis issues.

```shell
mantis2github sync [id]
mantis2github sync <ids>...
```

##### Arguments

| Argument | required | Description |
|----------|----------|-----------------|
| `id` | `false` | Mantis issue id |
| Argument | required | Description |
|----------|----------|------------------|
| `ids` | `true` | Mantis issue ids |

##### Examples

###### Sync a single issue

```shell
mantis2github sync 123
```

###### Sync multiple issues

```shell
mantis2github sync 123 456 789
```

#### `read:github`

Expand Down
4 changes: 2 additions & 2 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function arguments(): array
return $this->input->getArguments();
}

protected function argument(string $name): ?string
protected function argument(string $name)
{
return $this->input->getArgument($name);
}
Expand All @@ -38,7 +38,7 @@ protected function options(): array
return $this->input->getOptions();
}

protected function option(string $name): ?string
protected function option(string $name)
{
return $this->input->getOption($name);
}
Expand Down
132 changes: 54 additions & 78 deletions src/Command/CreateGithubIssueFromMantisIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
use Artemeon\M2G\Service\GithubConnector;
use Artemeon\M2G\Service\MantisConnector;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Input\InputOption;

use function Termwind\render;

Expand All @@ -41,7 +39,7 @@ protected function configure()
{
$this->setName('sync')
->setDescription('Synchronize a Mantis issue to GitHub')
->addArgument('id', InputArgument::OPTIONAL, 'Mantis issue id');
->addArgument('ids', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Mantis issue ids');
}

protected function header(): void
Expand All @@ -63,100 +61,78 @@ protected function handle(): int

$this->success(" Creates a new GitHub issue from a Mantis issue.");

$mantisIssue = $this->askForMantisIssue();
$ids = array_unique($this->argument('ids'));

$this->info('');

$table = new Table($this->output);
$table->setHeaders(['ID', 'Summary', 'Resolution']);
$table->addRow([$mantisIssue->getId(), $mantisIssue->getSummary(), $mantisIssue->getResolution()]);
$table->render();

if (!empty($mantisIssue->getUpstreamTicket())) {
$this->info('');
$this->warn("GitHub issue already exists");
$this->info("\n {$mantisIssue->getUpstreamTicket()}\n");

return 1;
}

$confirmation = $this->ask("\n Do you want to create a new issue on GitHub? [Y/n] ", 'n');
$progressBar = new ProgressBar($this->output, count($ids));
$progressBar->start();

if ($confirmation !== 'Y') {
$this->error('Aborted.');

return 1;
}

$this->info("\n Creating new GitHub issue ...");

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

$labels = array_values(array_map(function ($label) {
$labels = array_map(function ($label) {
return $label['name'];
}, array_filter($this->githubConnector->getLabels(), function ($label) use ($mantisIssue) {
return strtolower($label['name']) === strtolower($mantisIssue->getProject());
})));

$newGithubIssue->setLabels($labels);

try {
$newGithubIssue = $this->githubConnector->createIssue($newGithubIssue);
} catch (GuzzleException | \Exception $e) {
$this->error('Failed to create GitHub issue.');

return 1;
}
}, $this->githubConnector->getLabels());

$this->success("\n Successfully created GitHub issue #{$newGithubIssue->getNumber()}.");
$issues = [];

$this->info("\n Updating upstream ticket of Mantis issue ...");
foreach ($ids as $id) {
$mantisIssue = $this->mantisConnector->readIssue((int)$id);

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

$this->success("\n Mantis upstream issue updated successfully.");
$this->success(" {$mantisIssue->getUpstreamTicket()}");

if (empty($this->argument('id'))) {
$startOver = $this->ask("\n Do you want to sync another issue? [Y/n] ", 'n');

if ($startOver === 'Y') {
$this->handle();
if (!empty($mantisIssue->getUpstreamTicket())) {
$issues[] = [
'id' => $id,
'icon' => '<comment>!</comment>',
'message' => '<comment>Mantis issue already synchronized.</comment>',
'issue' => $mantisIssue->getUpstreamTicket(),
];
continue;
}
}

return 0;
}
$newGithubIssue = GithubIssue::fromMantisIssue($mantisIssue);

protected function askForMantisIssue(): MantisIssue
{
$id = $this->argument('id') ?? $this->ask("\n Mantis ID: ");
$filteredLabels = array_filter($labels, function ($label) use ($mantisIssue) {
return strtolower($label) === strtolower($mantisIssue->getProject());
});

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

if (empty($this->argument('id'))) {
$this->askForMantisIssue();
try {
$newGithubIssue = $this->githubConnector->createIssue($newGithubIssue);
} catch (GuzzleException | \Exception $e) {
$issues[] = [
'id' => $id,
'icon' => '<error>✕</error>',
'message' => '<error>GitHub issue could not be created.</error>',
'issue' => '',
];
continue;
}

exit(1);
}
$mantisIssue->setUpstreamTicket($newGithubIssue->getIssueUrl());
$this->mantisConnector->patchUpstreamField($mantisIssue);

$this->info("\n Fetching issue details ...");
$issues[] = [
'id' => $id,
'icon' => '<info>✓</info>',
'message' => '<info>Mantis issue has been synchronized.</info>',
'issue' => $newGithubIssue->getIssueUrl(),
];

$mantisIssue = $this->mantisConnector->readIssue((int)$id);
$progressBar->advance();
}

if ($mantisIssue === null) {
$this->error('Issue not found.');
$progressBar->finish();

if (empty($this->argument('id'))) {
$this->askForMantisIssue();
}
$this->info("\n");

exit(1);
$table = new Table($this->output);
$table->setHeaders(['', 'Mantis issue ID', 'Message', 'GitHub Issue']);
foreach($issues as $issue) {
$table->addRow([$issue['icon'], $issue['id'], $issue['message'], $issue['issue']]);
}
$table->render();

return $mantisIssue;
$this->info('');

return 0;
}
}

0 comments on commit b666092

Please sign in to comment.