Skip to content

Commit 28b5ae9

Browse files
authored
Merge pull request #630 from shulard/fix/normalize
Apply some normalization on Gitlab API handling
2 parents 52617b5 + 4de7ae7 commit 28b5ae9

File tree

3 files changed

+66
-84
lines changed

3 files changed

+66
-84
lines changed

src/Command/Issue/IssueShowCommand.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
namespace Gush\Command\Issue;
1313

1414
use Gush\Command\BaseCommand;
15-
use Gush\Exception\UserException;
1615
use Gush\Feature\IssueTrackerRepoFeature;
17-
use Gush\Helper\StyleHelper;
1816
use Symfony\Component\Console\Input\InputArgument;
1917
use Symfony\Component\Console\Input\InputInterface;
2018
use Symfony\Component\Console\Input\InputOption;
@@ -60,9 +58,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6058
$comments = [];
6159
$tracker = $this->getIssueTracker();
6260
$issue = $tracker->getIssue($issueNumber);
63-
if (true === $input->getOption('with-comments')) {
64-
$comments = $tracker->getComments($issueNumber);
65-
}
6661

6762
$styleHelper = $this->getHelper('gush_style');
6863
$styleHelper->title(
@@ -89,20 +84,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
8984
$styleHelper->section('Body');
9085
$styleHelper->text(explode("\n", $issue['body']));
9186

92-
if (true === $input->getOption('with-comments') && count($comments) > 0) {
87+
if (true === $input->getOption('with-comments')) {
88+
$comments = $tracker->getComments($issueNumber);
9389
$styleHelper->section('Comments');
9490
foreach ($comments as $comment) {
95-
$styleHelper->listing([
96-
sprintf(
97-
'Comment #%s by %s on %s',
98-
$comment['id'],
99-
$comment['user'],
100-
$comment['created_at']->format('r')
101-
),
102-
'Link: '.$comment['url'],
103-
'',
104-
wordwrap($comment['body'], 100),
105-
]);
91+
$styleHelper->text(sprintf(
92+
'<fg=white>Comment #%s</> by %s on %s',
93+
$comment['id'],
94+
$comment['user'],
95+
$comment['created_at']->format('r')
96+
));
97+
$styleHelper->detailsTable([['Link', $comment['url']]]);
98+
$styleHelper->text(explode("\n", wordwrap($comment['body'], 100)));
99+
$styleHelper->text([]);
106100
}
107101
}
108102

src/ThirdParty/Gitlab/Adapter/GitLabIssueTracker.php

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ function ($issue) {
141141
*/
142142
public function updateIssue($id, array $parameters)
143143
{
144-
$issue = $this->client->api('issues')->show($this->getCurrentProject()->id, $id);
145-
$issue = Issue::fromArray($this->client, $this->getCurrentProject(), $issue);
146-
147144
if (isset($parameters['assignee'])) {
148145
$assignee = $this->client->api('users')->search($parameters['assignee']);
149-
150146
if (count($assignee) === 0) {
151147
throw new \InvalidArgumentException(sprintf('Could not find user %s', $parameters['assignee']));
152148
}
153149

154-
$issue->update([
155-
'assignee_id' => current($assignee)['id'],
156-
]);
150+
$this->client
151+
->api('issues')
152+
->update(
153+
$this->getCurrentProject()->id,
154+
$id,
155+
['assignee_id' => current($assignee)['id']]
156+
);
157157
}
158158
}
159159

@@ -162,55 +162,45 @@ public function updateIssue($id, array $parameters)
162162
*/
163163
public function closeIssue($id)
164164
{
165-
$issue = $this->client->api('issues')->show($this->getCurrentProject()->id, $id);
166-
167-
Issue::fromArray($this->client, $this->getCurrentProject(), $issue);
165+
$this->client
166+
->api('issues')
167+
->update($this->getCurrentProject()->id, $id, ['state_event' => 'close']);
168168
}
169169

170170
/**
171171
* {@inheritdoc}
172172
*/
173173
public function createComment($id, $message)
174174
{
175-
$issue = Issue::fromArray(
176-
$this->client,
177-
$this->getCurrentProject(),
178-
$this->client->api('issues')->show($this->getCurrentProject()->id, $id)
179-
);
175+
$comment = $this
176+
->api('issues')
177+
->addComment($this->getCurrentProject()->id, $id, ['body' => $message]);
180178

181-
$comment = $issue->addComment($message);
182-
183-
return sprintf('%s#note_%d', $this->getIssueUrl($id), $comment->id);
179+
return sprintf('%s#note_%d', $this->getIssueUrl($id), $comment['id']);
184180
}
185181

186182
/**
187183
* {@inheritdoc}
188184
*/
189185
public function getComments($id)
190186
{
191-
$issue = Issue::fromArray(
192-
$this->client,
193-
$this->getCurrentProject(),
194-
$this->client->api('issues')->show($this->getCurrentProject()->id, $id)
195-
);
187+
$comments = $this->client->api('issues')->showComments($this->getCurrentProject()->id, $id);
196188

197-
$comments = [];
198-
array_map(function ($comment) use (&$comments) {
199-
$comments[] = [
200-
'id' => $comment->id,
189+
return array_map(function ($comment) {
190+
return [
191+
'id' => $comment['id'],
201192
'url' => sprintf(
202193
'%s/issues/%d/#note_%d',
203194
$this->getCurrentProject()->web_url,
204-
$comment->parent->iid,
205-
$comment->id
195+
$comment['parent']['iid'],
196+
$comment['id']
206197
),
207-
'user' => ['login' => $comment->author->username],
208-
'body' => $comment->body,
209-
'created_at' => new \DateTime($comment->created_at),
198+
'user' => $comment['author']['username'],
199+
'body' => $comment['body'],
200+
'created_at' => !empty($comment['created_at']) ? new \DateTime($comment['created_at']) : null,
201+
'updated_at' => !empty($comment['updated_at']) ? new \DateTime($comment['updated_at']) : null,
210202
];
211-
}, $issue->showComments());
212-
213-
return $comments;
203+
}, $comments);
214204
}
215205

216206
/**

src/ThirdParty/Gitlab/Adapter/GitLabRepoAdapter.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Gush\Adapter\BaseAdapter;
1515
use Gush\Exception\UnsupportedOperationException;
16-
use Gush\ThirdParty\Gitlab\Model\Issue;
1716
use Gush\ThirdParty\Gitlab\Model\MergeRequest;
1817
use Gush\ThirdParty\Gitlab\Model\Project;
1918
use Gush\Util\ArrayUtil;
@@ -51,10 +50,8 @@ public function getRepositoryInfo($org, $repository)
5150
public function getPullRequestUrl($id)
5251
{
5352
return sprintf(
54-
'%s/%s/%s/merge_requests/%d',
53+
'%s/merge_requests/%d',
5554
$this->configuration['repo_domain_url'],
56-
$this->getUsername(),
57-
$this->getRepository(),
5855
$this->getPullRequest($id)['iid']
5956
);
6057
}
@@ -64,13 +61,9 @@ public function getPullRequestUrl($id)
6461
*/
6562
public function createComment($id, $message)
6663
{
67-
$issue = MergeRequest::fromArray(
68-
$this->client,
69-
$this->getCurrentProject(),
70-
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
71-
);
72-
73-
$comment = $issue->addComment($message);
64+
$comment = $this
65+
->api('merge_requests')
66+
->addComment($this->getCurrentProject()->id, $id, ['body' => $message]);
7467

7568
return sprintf('%s#note_%d', $this->getPullRequestUrl($id), $comment->id);
7669
}
@@ -80,13 +73,17 @@ public function createComment($id, $message)
8073
*/
8174
public function getComments($id)
8275
{
83-
$issue = MergeRequest::fromArray(
84-
$this->client,
85-
$this->getCurrentProject(),
86-
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
87-
);
88-
89-
return $issue->showComments();
76+
$comments = $this->client->api('merge_requests')->showNotes($this->getCurrentProject()->id, $id);
77+
78+
return array_filter(array_map(function ($note) {
79+
return [
80+
'id' => $note['id'],
81+
'user' => $note['author']['username'],
82+
'body' => $note['body'],
83+
'created_at' => !empty($note['created_at']) ? new \DateTime($note['created_at']) : null,
84+
'updated_at' => !empty($note['updated_at']) ? new \DateTime($note['updated_at']) : null,
85+
];
86+
}, $comments));
9087
}
9188

9289
/**
@@ -113,19 +110,20 @@ public function getMilestones(array $parameters = [])
113110
*/
114111
public function updatePullRequest($id, array $parameters)
115112
{
116-
$issue = $this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id);
117-
$issue = Issue::fromArray($this->client, $this->getCurrentProject(), $issue);
118-
119113
if (isset($parameters['assignee'])) {
120114
$assignee = $this->client->api('users')->search($parameters['assignee']);
121115

122116
if (count($assignee) === 0) {
123117
throw new \InvalidArgumentException(sprintf('Could not find user %s', $parameters['assignee']));
124118
}
125119

126-
$issue->update([
127-
'assignee_id' => current($assignee)['id'],
128-
]);
120+
$this->client
121+
->api('merge_requests')
122+
->update(
123+
$this->getCurrentProject()->id,
124+
$id,
125+
['assignee_id' => current($assignee)['id']]
126+
);
129127
}
130128
}
131129

@@ -134,13 +132,9 @@ public function updatePullRequest($id, array $parameters)
134132
*/
135133
public function closePullRequest($id)
136134
{
137-
$mr = MergeRequest::fromArray(
138-
$this->client,
139-
$this->getCurrentProject(),
140-
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
141-
);
142-
143-
return $mr->close()->id;
135+
$this->client
136+
->api('merge_requests')
137+
->update($this->getCurrentProject()->id, $id, ['state_event' => 'close']);
144138
}
145139

146140
/**
@@ -174,7 +168,7 @@ public function getPullRequest($id)
174168
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
175169
);
176170

177-
return array_merge(
171+
$data = array_merge(
178172
$mr->toArray(),
179173
[
180174
'url' => sprintf(
@@ -186,6 +180,10 @@ public function getPullRequest($id)
186180
),
187181
]
188182
);
183+
$data['milestone'] = $data['milestone']->title;
184+
$data['user'] = $data['author'];
185+
186+
return $data;
189187
}
190188

191189
/**

0 commit comments

Comments
 (0)