Skip to content

Commit

Permalink
Merge pull request #631 from shulard/feature/pull-request-get-commits
Browse files Browse the repository at this point in the history
Add support for pullRequestGetCommits in GitlabRepoAdapter
  • Loading branch information
cordoval authored Dec 14, 2016
2 parents f1be270 + 1d40f2a commit 678d9a8
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ public function getCommands()
new Cmd\PullRequest\PullRequestMilestoneListCommand(),
new Cmd\PullRequest\PullRequestFixerCommand(),
new Cmd\PullRequest\PullRequestCheckoutCommand(),
new Cmd\PullRequest\PullRequestShowCommand(),
new Cmd\Util\DocumentationCommand(),
new Cmd\Util\StyleCIPatchCommand(),
new Cmd\Util\MetaHeaderCommand(),
Expand Down
137 changes: 137 additions & 0 deletions src/Command/PullRequest/PullRequestShowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/*
* This file is part of Gush package.
*
* (c) Luis Cordova <cordoval@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Gush\Command\PullRequest;

use Gush\Command\BaseCommand;
use Gush\Feature\GitRepoFeature;
use Gush\Feature\TableFeature;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PullRequestShowCommand extends BaseCommand implements TableFeature, GitRepoFeature
{
/**
* {@inheritdoc}
*/
public function getTableDefaultLayout()
{
return 'default';
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('pull-request:show')
->addArgument(
'id',
InputArgument::REQUIRED,
'Identifier of the PR to retrieve'
)
->addOption(
'with-comments',
null,
InputOption::VALUE_NONE,
'Display comments from this pull request'
)
->addOption(
'with-commits',
null,
InputOption::VALUE_NONE,
'Display commits from this pull request'
)
->setDescription('Retrieve detail for a specific pull request')
->setHelp(
<<<EOF
The <info>%command.name%</info> command retrieves details for a pull requests:
<info>$ gush %command.name%</info>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$id = $input->getArgument('id');
$adapter = $this->getAdapter();
$pr = $adapter->getPullRequest($id);

$styleHelper = $this->getHelper('gush_style');
$styleHelper->title(
sprintf(
'Pull Request #%s - %s by %s [<fg='.'%s>%s</>]',
$pr['number'],
$pr['title'],
$pr['user'],
'closed' === $pr['state'] ? 'red' : 'green',
$pr['state']
)
);

$styleHelper->detailsTable(
[
['Org/Repo', $input->getOption('org').'/'.$input->getOption('repo')],
['Link', $pr['url']],
['Labels', implode(', ', $pr['labels']) ?: '<comment>None</comment>'],
['Milestone', $pr['milestone'] ?: '<comment>None</comment>'],
['Assignee', $pr['assignee'] ?: '<comment>None</comment>'],
['Source => Target', $pr['head']['user'].'/'.$pr['head']['repo'].'#'.$pr['head']['ref'].' => '.$pr['base']['user'].'/'.$pr['base']['repo'].'#'.$pr['base']['ref']],
]
);

$styleHelper->section('Body');
$styleHelper->text(explode("\n", $pr['body']));

if (true === $input->getOption('with-commits')) {
$commits = $adapter->getPullRequestCommits($id);
$styleHelper->section('Commits');
foreach ($commits as $commit) {
$styleHelper->text(sprintf(
'<fg=white>Commit #%s</> by %s',
$commit['sha'],
$commit['user']
));
$styleHelper->text(explode("\n", wordwrap($commit['message'], 100)));
$styleHelper->text([]);
}
}

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

return self::COMMAND_SUCCESS;
}
}
11 changes: 10 additions & 1 deletion src/ThirdParty/Gitlab/Adapter/GitLabRepoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,16 @@ public function getPullRequest($id)
*/
public function getPullRequestCommits($id)
{
return [];
$commits = $this->client->api('merge_requests')->commits($this->getCurrentProject()->id, $id);

return array_map(function ($commit) {
return [
'sha' => $commit['id'],
'short_sha' => $commit['short_id'],
'user' => $commit['author_name'].' <'.$commit['author_email'].'>',
'message' => trim($commit['message']),
];
}, $commits);
}

/**
Expand Down

0 comments on commit 678d9a8

Please sign in to comment.