Skip to content

Commit

Permalink
Merge pull request #630 from shulard/fix/normalize
Browse files Browse the repository at this point in the history
Apply some normalization on Gitlab API handling
  • Loading branch information
cordoval authored Aug 24, 2016
2 parents 52617b5 + 4de7ae7 commit 28b5ae9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 84 deletions.
28 changes: 11 additions & 17 deletions src/Command/Issue/IssueShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
namespace Gush\Command\Issue;

use Gush\Command\BaseCommand;
use Gush\Exception\UserException;
use Gush\Feature\IssueTrackerRepoFeature;
use Gush\Helper\StyleHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -60,9 +58,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$comments = [];
$tracker = $this->getIssueTracker();
$issue = $tracker->getIssue($issueNumber);
if (true === $input->getOption('with-comments')) {
$comments = $tracker->getComments($issueNumber);
}

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

if (true === $input->getOption('with-comments') && count($comments) > 0) {
if (true === $input->getOption('with-comments')) {
$comments = $tracker->getComments($issueNumber);
$styleHelper->section('Comments');
foreach ($comments as $comment) {
$styleHelper->listing([
sprintf(
'Comment #%s by %s on %s',
$comment['id'],
$comment['user'],
$comment['created_at']->format('r')
),
'Link: '.$comment['url'],
'',
wordwrap($comment['body'], 100),
]);
$styleHelper->text(sprintf(
'<fg=white>Comment #%s</> by %s on %s',
$comment['id'],
$comment['user'],
$comment['created_at']->format('r')
));
$styleHelper->detailsTable([['Link', $comment['url']]]);
$styleHelper->text(explode("\n", wordwrap($comment['body'], 100)));
$styleHelper->text([]);
}
}

Expand Down
60 changes: 25 additions & 35 deletions src/ThirdParty/Gitlab/Adapter/GitLabIssueTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ function ($issue) {
*/
public function updateIssue($id, array $parameters)
{
$issue = $this->client->api('issues')->show($this->getCurrentProject()->id, $id);
$issue = Issue::fromArray($this->client, $this->getCurrentProject(), $issue);

if (isset($parameters['assignee'])) {
$assignee = $this->client->api('users')->search($parameters['assignee']);

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

$issue->update([
'assignee_id' => current($assignee)['id'],
]);
$this->client
->api('issues')
->update(
$this->getCurrentProject()->id,
$id,
['assignee_id' => current($assignee)['id']]
);
}
}

Expand All @@ -162,55 +162,45 @@ public function updateIssue($id, array $parameters)
*/
public function closeIssue($id)
{
$issue = $this->client->api('issues')->show($this->getCurrentProject()->id, $id);

Issue::fromArray($this->client, $this->getCurrentProject(), $issue);
$this->client
->api('issues')
->update($this->getCurrentProject()->id, $id, ['state_event' => 'close']);
}

/**
* {@inheritdoc}
*/
public function createComment($id, $message)
{
$issue = Issue::fromArray(
$this->client,
$this->getCurrentProject(),
$this->client->api('issues')->show($this->getCurrentProject()->id, $id)
);
$comment = $this
->api('issues')
->addComment($this->getCurrentProject()->id, $id, ['body' => $message]);

$comment = $issue->addComment($message);

return sprintf('%s#note_%d', $this->getIssueUrl($id), $comment->id);
return sprintf('%s#note_%d', $this->getIssueUrl($id), $comment['id']);
}

/**
* {@inheritdoc}
*/
public function getComments($id)
{
$issue = Issue::fromArray(
$this->client,
$this->getCurrentProject(),
$this->client->api('issues')->show($this->getCurrentProject()->id, $id)
);
$comments = $this->client->api('issues')->showComments($this->getCurrentProject()->id, $id);

$comments = [];
array_map(function ($comment) use (&$comments) {
$comments[] = [
'id' => $comment->id,
return array_map(function ($comment) {
return [
'id' => $comment['id'],
'url' => sprintf(
'%s/issues/%d/#note_%d',
$this->getCurrentProject()->web_url,
$comment->parent->iid,
$comment->id
$comment['parent']['iid'],
$comment['id']
),
'user' => ['login' => $comment->author->username],
'body' => $comment->body,
'created_at' => new \DateTime($comment->created_at),
'user' => $comment['author']['username'],
'body' => $comment['body'],
'created_at' => !empty($comment['created_at']) ? new \DateTime($comment['created_at']) : null,
'updated_at' => !empty($comment['updated_at']) ? new \DateTime($comment['updated_at']) : null,
];
}, $issue->showComments());

return $comments;
}, $comments);
}

/**
Expand Down
62 changes: 30 additions & 32 deletions src/ThirdParty/Gitlab/Adapter/GitLabRepoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Gush\Adapter\BaseAdapter;
use Gush\Exception\UnsupportedOperationException;
use Gush\ThirdParty\Gitlab\Model\Issue;
use Gush\ThirdParty\Gitlab\Model\MergeRequest;
use Gush\ThirdParty\Gitlab\Model\Project;
use Gush\Util\ArrayUtil;
Expand Down Expand Up @@ -51,10 +50,8 @@ public function getRepositoryInfo($org, $repository)
public function getPullRequestUrl($id)
{
return sprintf(
'%s/%s/%s/merge_requests/%d',
'%s/merge_requests/%d',
$this->configuration['repo_domain_url'],
$this->getUsername(),
$this->getRepository(),
$this->getPullRequest($id)['iid']
);
}
Expand All @@ -64,13 +61,9 @@ public function getPullRequestUrl($id)
*/
public function createComment($id, $message)
{
$issue = MergeRequest::fromArray(
$this->client,
$this->getCurrentProject(),
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
);

$comment = $issue->addComment($message);
$comment = $this
->api('merge_requests')
->addComment($this->getCurrentProject()->id, $id, ['body' => $message]);

return sprintf('%s#note_%d', $this->getPullRequestUrl($id), $comment->id);
}
Expand All @@ -80,13 +73,17 @@ public function createComment($id, $message)
*/
public function getComments($id)
{
$issue = MergeRequest::fromArray(
$this->client,
$this->getCurrentProject(),
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
);

return $issue->showComments();
$comments = $this->client->api('merge_requests')->showNotes($this->getCurrentProject()->id, $id);

return array_filter(array_map(function ($note) {
return [
'id' => $note['id'],
'user' => $note['author']['username'],
'body' => $note['body'],
'created_at' => !empty($note['created_at']) ? new \DateTime($note['created_at']) : null,
'updated_at' => !empty($note['updated_at']) ? new \DateTime($note['updated_at']) : null,
];
}, $comments));
}

/**
Expand All @@ -113,19 +110,20 @@ public function getMilestones(array $parameters = [])
*/
public function updatePullRequest($id, array $parameters)
{
$issue = $this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id);
$issue = Issue::fromArray($this->client, $this->getCurrentProject(), $issue);

if (isset($parameters['assignee'])) {
$assignee = $this->client->api('users')->search($parameters['assignee']);

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

$issue->update([
'assignee_id' => current($assignee)['id'],
]);
$this->client
->api('merge_requests')
->update(
$this->getCurrentProject()->id,
$id,
['assignee_id' => current($assignee)['id']]
);
}
}

Expand All @@ -134,13 +132,9 @@ public function updatePullRequest($id, array $parameters)
*/
public function closePullRequest($id)
{
$mr = MergeRequest::fromArray(
$this->client,
$this->getCurrentProject(),
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
);

return $mr->close()->id;
$this->client
->api('merge_requests')
->update($this->getCurrentProject()->id, $id, ['state_event' => 'close']);
}

/**
Expand Down Expand Up @@ -174,7 +168,7 @@ public function getPullRequest($id)
$this->client->api('merge_requests')->show($this->getCurrentProject()->id, $id)
);

return array_merge(
$data = array_merge(
$mr->toArray(),
[
'url' => sprintf(
Expand All @@ -186,6 +180,10 @@ public function getPullRequest($id)
),
]
);
$data['milestone'] = $data['milestone']->title;
$data['user'] = $data['author'];

return $data;
}

/**
Expand Down

0 comments on commit 28b5ae9

Please sign in to comment.