Skip to content

Commit 9c4413e

Browse files
marvil07mglaman
andauthored
Add an issue:show command (#197)
Co-authored-by: Matt Glaman <nmd.matt@gmail.com>
1 parent 1b450e5 commit 9c4413e

5 files changed

Lines changed: 166 additions & 2 deletions

File tree

src/Api/IssueTrait.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace mglaman\DrupalOrg;
4+
5+
trait IssueTrait
6+
{
7+
/**
8+
* Produces the label for a given category id.
9+
*
10+
* @param int $value
11+
* Category identifier.
12+
*
13+
* @return string
14+
* The label if known, or the casted string value if not.
15+
*/
16+
public function getIssueCategoryLabel(int $value): string
17+
{
18+
switch ($value) {
19+
case 1:
20+
return 'Bug report';
21+
case 2:
22+
return 'Task';
23+
case 3:
24+
return 'Feature request';
25+
case 4:
26+
return 'Support request';
27+
case 5:
28+
return 'Plan';
29+
default:
30+
return (string)$value;
31+
}
32+
}
33+
34+
/**
35+
* Produces the label for a given priority id.
36+
*
37+
* @param int $value
38+
* Priority identifier.
39+
*
40+
* @return string
41+
* The label if known, or the casted string value if not.
42+
*/
43+
public function getIssuePriorityLabel(int $value): string
44+
{
45+
switch ($value) {
46+
case 100:
47+
return 'Minor';
48+
case 200:
49+
return 'Normal';
50+
case 300:
51+
return 'Major';
52+
case 400:
53+
return 'Critical';
54+
default:
55+
return (string)$value;
56+
}
57+
}
58+
59+
/**
60+
* Produces the label for a given status id.
61+
*
62+
* @param int $value
63+
* Status identifier.
64+
*
65+
* @return string
66+
* The label if known, or the casted string value if not.
67+
*/
68+
public function getIssueStatusLabel(int $value): string
69+
{
70+
switch ($value) {
71+
case 1:
72+
return 'Active';
73+
case 2:
74+
return 'Fixed';
75+
case 13:
76+
return 'Needs Work';
77+
case 8:
78+
return 'Needs Review';
79+
case 16:
80+
return 'Postponed [NMI]';
81+
case 14:
82+
return 'RTBC';
83+
default:
84+
return (string)$value;
85+
}
86+
}
87+
}

src/Api/RawResponse.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ public function get(string $key)
2424
}
2525
return null;
2626
}
27+
28+
/**
29+
* Get the full response body.
30+
*
31+
* @return mixed
32+
* The JSON parsed body in the response.
33+
*/
34+
public function getContent()
35+
{
36+
return $this->response;
37+
}
2738
}

src/Cli/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getCommands(): array
4747
$commands[] = new Command\Issue\Patch();
4848
$commands[] = new Command\Issue\Interdiff();
4949
$commands[] = new Command\Issue\Apply();
50+
$commands[] = new Command\Issue\Show();
5051
$commands[] = new Command\Project\Link();
5152
$commands[] = new Command\Project\Kanban();
5253
$commands[] = new Command\Project\ProjectIssues();

src/Cli/Command/Issue/Show.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace mglaman\DrupalOrgCli\Command\Issue;
4+
5+
use Symfony\Component\Console\Input\InputArgument;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
use mglaman\DrupalOrg\IssueTrait;
10+
11+
class Show extends IssueCommandBase
12+
{
13+
use IssueTrait;
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function configure(): void
19+
{
20+
$this
21+
->setName('issue:show')
22+
->addArgument('nid', InputArgument::REQUIRED, 'The issue node ID')
23+
->addOption(
24+
'format',
25+
'f',
26+
InputOption::VALUE_OPTIONAL,
27+
'Output options: text, json. Defaults to text.',
28+
'text'
29+
)
30+
->setDescription('Show a given issue information.');
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
protected function execute(InputInterface $input, OutputInterface $output): int
37+
{
38+
$nid = $this->stdIn->getArgument('nid');
39+
$issue = $this->client->getNode($nid);
40+
$issue_data = $issue->getContent();
41+
$format = $this->stdIn->getOption('format');
42+
43+
if ($format == 'json') {
44+
$this->stdOut->writeln(json_encode($issue_data));
45+
return 0;
46+
}
47+
// format option is text.
48+
$this->stdOut->writeln(sprintf('Title: %s', $issue->get('title')));
49+
$this->stdOut->writeln(sprintf('Status: %s', $this->getIssueStatusLabel($issue->get('field_issue_status'))));
50+
$this->stdOut->writeln(sprintf('Project: %s', $issue_data->field_project->machine_name));
51+
$this->stdOut->writeln(sprintf('Version: %s', $issue->get('field_issue_version')));
52+
$this->stdOut->writeln(sprintf('Component: %s', $issue->get('field_issue_component')));
53+
$this->stdOut->writeln(sprintf('Priority: %s', $this->getIssuePriorityLabel($issue->get('field_issue_priority'))));
54+
$this->stdOut->writeln(sprintf('Category: %s', $this->getIssueCategoryLabel($issue->get('field_issue_category'))));
55+
// Assigned field does not seem to be exposed on API.
56+
// TODO Convert to username.
57+
$this->stdOut->writeln(sprintf('Reporter: %s', $issue_data->author->id));
58+
$this->stdOut->writeln(sprintf('Created: %s', date('r', $issue->get('created'))));
59+
$this->stdOut->writeln(sprintf('Updated: %s', date('r', $issue->get('changed'))));
60+
$this->stdOut->writeln(sprintf("\nIssue summary:\n%s", strip_tags($issue_data->body->value)));
61+
return 0;
62+
}
63+
}

tests/src/RawResponseTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use mglaman\DrupalOrg\RawResponse;
66
use PHPUnit\Framework\TestCase;
77

8-
class RawResponseTest extends TestCase {
8+
class RawResponseTest extends TestCase
9+
{
910

10-
public function testGet(): void {
11+
public function testGet(): void
12+
{
1113
$sut = new RawResponse('{"message": "foobar"}');
1214
self::assertEquals('foobar', $sut->get('message'));
1315
self::assertEquals(null, $sut->get('baz'));

0 commit comments

Comments
 (0)