Skip to content

Commit

Permalink
Add an issue:show command (#197)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Glaman <[email protected]>
  • Loading branch information
marvil07 and mglaman authored May 4, 2023
1 parent 1b450e5 commit 9c4413e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 2 deletions.
87 changes: 87 additions & 0 deletions src/Api/IssueTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace mglaman\DrupalOrg;

trait IssueTrait
{
/**
* Produces the label for a given category id.
*
* @param int $value
* Category identifier.
*
* @return string
* The label if known, or the casted string value if not.
*/
public function getIssueCategoryLabel(int $value): string
{
switch ($value) {
case 1:
return 'Bug report';
case 2:
return 'Task';
case 3:
return 'Feature request';
case 4:
return 'Support request';
case 5:
return 'Plan';
default:
return (string)$value;
}
}

/**
* Produces the label for a given priority id.
*
* @param int $value
* Priority identifier.
*
* @return string
* The label if known, or the casted string value if not.
*/
public function getIssuePriorityLabel(int $value): string
{
switch ($value) {
case 100:
return 'Minor';
case 200:
return 'Normal';
case 300:
return 'Major';
case 400:
return 'Critical';
default:
return (string)$value;
}
}

/**
* Produces the label for a given status id.
*
* @param int $value
* Status identifier.
*
* @return string
* The label if known, or the casted string value if not.
*/
public function getIssueStatusLabel(int $value): string
{
switch ($value) {
case 1:
return 'Active';
case 2:
return 'Fixed';
case 13:
return 'Needs Work';
case 8:
return 'Needs Review';
case 16:
return 'Postponed [NMI]';
case 14:
return 'RTBC';
default:
return (string)$value;
}
}
}
11 changes: 11 additions & 0 deletions src/Api/RawResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ public function get(string $key)
}
return null;
}

/**
* Get the full response body.
*
* @return mixed
* The JSON parsed body in the response.
*/
public function getContent()
{
return $this->response;
}
}
1 change: 1 addition & 0 deletions src/Cli/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getCommands(): array
$commands[] = new Command\Issue\Patch();
$commands[] = new Command\Issue\Interdiff();
$commands[] = new Command\Issue\Apply();
$commands[] = new Command\Issue\Show();
$commands[] = new Command\Project\Link();
$commands[] = new Command\Project\Kanban();
$commands[] = new Command\Project\ProjectIssues();
Expand Down
63 changes: 63 additions & 0 deletions src/Cli/Command/Issue/Show.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace mglaman\DrupalOrgCli\Command\Issue;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use mglaman\DrupalOrg\IssueTrait;

class Show extends IssueCommandBase
{
use IssueTrait;

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setName('issue:show')
->addArgument('nid', InputArgument::REQUIRED, 'The issue node ID')
->addOption(
'format',
'f',
InputOption::VALUE_OPTIONAL,
'Output options: text, json. Defaults to text.',
'text'
)
->setDescription('Show a given issue information.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$nid = $this->stdIn->getArgument('nid');
$issue = $this->client->getNode($nid);
$issue_data = $issue->getContent();
$format = $this->stdIn->getOption('format');

if ($format == 'json') {
$this->stdOut->writeln(json_encode($issue_data));
return 0;
}
// format option is text.
$this->stdOut->writeln(sprintf('Title: %s', $issue->get('title')));
$this->stdOut->writeln(sprintf('Status: %s', $this->getIssueStatusLabel($issue->get('field_issue_status'))));
$this->stdOut->writeln(sprintf('Project: %s', $issue_data->field_project->machine_name));
$this->stdOut->writeln(sprintf('Version: %s', $issue->get('field_issue_version')));
$this->stdOut->writeln(sprintf('Component: %s', $issue->get('field_issue_component')));
$this->stdOut->writeln(sprintf('Priority: %s', $this->getIssuePriorityLabel($issue->get('field_issue_priority'))));
$this->stdOut->writeln(sprintf('Category: %s', $this->getIssueCategoryLabel($issue->get('field_issue_category'))));
// Assigned field does not seem to be exposed on API.
// TODO Convert to username.
$this->stdOut->writeln(sprintf('Reporter: %s', $issue_data->author->id));
$this->stdOut->writeln(sprintf('Created: %s', date('r', $issue->get('created'))));
$this->stdOut->writeln(sprintf('Updated: %s', date('r', $issue->get('changed'))));
$this->stdOut->writeln(sprintf("\nIssue summary:\n%s", strip_tags($issue_data->body->value)));
return 0;
}
}
6 changes: 4 additions & 2 deletions tests/src/RawResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use mglaman\DrupalOrg\RawResponse;
use PHPUnit\Framework\TestCase;

class RawResponseTest extends TestCase {
class RawResponseTest extends TestCase
{

public function testGet(): void {
public function testGet(): void
{
$sut = new RawResponse('{"message": "foobar"}');
self::assertEquals('foobar', $sut->get('message'));
self::assertEquals(null, $sut->get('baz'));
Expand Down

0 comments on commit 9c4413e

Please sign in to comment.