Skip to content

Commit bebc3e9

Browse files
authored
Merge pull request #1 from jaggy/issue-labels-api
Add an API for the labels & add an issue with labels
2 parents 71df251 + fae0b8b commit bebc3e9

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

src/Dto/Label.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Linear\Dto;
4+
5+
final class Label
6+
{
7+
public function __construct(
8+
readonly ?string $id,
9+
readonly string $name,
10+
readonly bool $isGroup,
11+
) {}
12+
13+
}

src/Dto/Labels.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Linear\Dto;
4+
5+
final class Labels
6+
{
7+
public function __construct(
8+
/** @var array<Label> */
9+
readonly array $nodes
10+
) {}
11+
12+
}

src/Sdk/Issues.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,15 @@ public function delete(Dto\Issue $issue): bool
104104
return $issueArr['issueDelete']['success'];
105105
}
106106

107-
public function create(string $title, string $description, Dto\Team $team, ?int $priority = 0, ?Dto\Project $project = null): Dto\Issue
107+
public function create(string $title, string $description, Dto\Team $team, ?int $priority = 0, ?Dto\Project $project = null, array $labels = []): Dto\Issue
108108
{
109109
$input = [
110110
'title' => $title,
111111
'description' => $description,
112112
'teamId' => $team->id,
113113
'priority' => $priority,
114114
'projectId' => $issue->project->id ?? null,
115+
'labelIds' => $labels ?? [],
115116
];
116117

117118
$query = "
@@ -124,6 +125,7 @@ public function create(string $title, string $description, Dto\Team $team, ?int
124125
number
125126
priority
126127
priorityLabel
128+
labelIds
127129
state {
128130
id
129131
name
@@ -145,6 +147,8 @@ public function create(string $title, string $description, Dto\Team $team, ?int
145147

146148
$issueArr = $this->process($response);
147149

150+
unset($issueArr['issueCreate']['issue']['labelIds']);
151+
148152
try {
149153
return Mapper::get()->map(Dto\Issue::class, Source::array($issueArr['issueCreate']['issue']));
150154
} catch (MappingError $error) {

src/Sdk/Labels.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Linear\Sdk;
4+
5+
use CuyZ\Valinor\Mapper\MappingError;
6+
use CuyZ\Valinor\Mapper\Source\Source;
7+
use Linear\Utils\Mapper;
8+
use Linear\Dto;
9+
use Exception;
10+
use Symfony\Contracts\Cache\ItemInterface;
11+
12+
13+
class Labels extends Client
14+
{
15+
public function getAll(): Dto\Labels
16+
{
17+
$query = "
18+
query Labels {
19+
issueLabels {
20+
nodes {
21+
id
22+
name
23+
isGroup
24+
}
25+
}
26+
}
27+
";
28+
29+
$labelsArr = $this->cache->get('labels', function (ItemInterface $item) use ($query) {
30+
$response = $this->http->post('/', ['query' => $query]);
31+
return $this->process($response);
32+
});
33+
34+
try {
35+
return Mapper::get()->map(Dto\Labels::class, Source::array($labelsArr['issueLabels']));
36+
} catch (MappingError $error) {
37+
throw new Exception('Teams DTO Mapping error:' . $error->getMessage());
38+
}
39+
}
40+
41+
public function getOne(string $id): Dto\Label
42+
{
43+
$query = "
44+
query Label {
45+
issueLabel(id: \"$id\" ) {
46+
id
47+
name
48+
isGroup
49+
}
50+
}";
51+
52+
53+
$response = $this->cache->get('label_' . $id, function (ItemInterface $item) use ($query) {
54+
$response = $this->http->post('/', ['query' => $query]);
55+
56+
return $this->process($response);
57+
});
58+
59+
try {
60+
return Mapper::get()->map(Dto\Label::class, Source::array($response['issueLabel']));
61+
} catch (MappingError $error) {
62+
throw new Exception('Team DTO Mapping error:' . $error->getMessage());
63+
}
64+
}
65+
66+
}

tests/Api/LabelsTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Linear\Tests\Api;
4+
5+
use Linear\Sdk;
6+
use Linear\Dto;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class LabelsTest extends TestCase
10+
{
11+
protected ?Sdk\Labels $labels;
12+
protected ?Sdk\Teams $teams;
13+
14+
public function setUp(): void
15+
{
16+
$this->labels = new Sdk\Labels(getenv('LINEAR_API_KEY'), 1);
17+
$this->teams = new Sdk\Teams(getenv('LINEAR_API_KEY'));
18+
}
19+
20+
public function tearDown(): void
21+
{
22+
$this->labels = null;
23+
$this->teams = null;
24+
}
25+
26+
public function testGetAll()
27+
{
28+
$response = $this->labels->getAll();
29+
30+
$this->assertInstanceOf(Dto\Labels::class, $response);
31+
}
32+
33+
public function testGetOne()
34+
{
35+
$response = $this->labels->getOne('4704736f-cdd4-40e2-831c-3b372e8f3077');
36+
$this->assertInstanceOf(Dto\Label::class, $response);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)