Skip to content

Commit

Permalink
init implementation (#1)
Browse files Browse the repository at this point in the history
* init implementation

* sigh... again?
  • Loading branch information
lifeofguenter authored Jan 19, 2018
1 parent 1a661e2 commit bb2d5a4
Show file tree
Hide file tree
Showing 20 changed files with 882 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .bash/functions.shlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
throw_exception() {
consolelog "Ooops!" "error"
echo 'Stack trace:' 1>&2
while caller $((n++)) 1>&2; do :; done;
exit 1
}

consolelog() {
local color
local ts

# el-cheapo way to detect if timestamp prefix needed
if [[ ! -z "${JENKINS_HOME}" ]]; then
ts=''
else
ts="[$(date -u +'%Y-%m-%d %H:%M:%S')] "
fi

color_reset='\e[0m'

case "${2}" in
success )
color='\e[0;32m'
;;
error )
color='\e[1;31m'
;;
* )
color='\e[0;37m'
;;
esac

if [[ ! -z "${1}" ]] && [[ ! -z "${2}" ]] && [[ "${2}" = "error" ]]; then
printf "${color}%s%s: %s${color_reset}\n" "${ts}" "${0##*/}" "${1}" >&2
elif [[ ! -z "${1}" ]]; then
printf "${color}%s%s: %s${color_reset}\n" "${ts}" "${0##*/}" "${1}"
fi

return 0
}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.buildpath
/.project
/.settings/

/version.txt
/bitbucket.phar

/vendor/
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

matrix:
include:
- php: 7.2
- php: 7.1
env: DEPLOY=true

sudo: false

script: "./build.sh"

deploy:
skip_cleanup: true
provider: releases
api_key:
secure: my141Ro0d++ChMEqF1lk6jMNI0VDguJMxdJ39A/x+snmxPBX79750vtdarvyEb3ZSE5oaPt6u3iVlhgK0PWI0wCg0hYkMA8qdIU7UB6pI500HmyVNA0X6kkloypDY6zFFOn7E7mtBT8jxl9+sQscw5Ihojny3Nn0ajXxgxkJbLDzXMZDiBUPUuAHrQ4e9OOfAEVhWj7E9yiP6AOu5xJZgXrh8pILQ3/uvkGLFujcbpjeZFk4wLVechJrj78Jc6uoX5CdgaIvlVnQIQGYEzom6A3ZdQaP72vfjaopaZ7cCV6e/Z2w05v0HkbbTMpsu/5amXoYVtQaeejhy1PTrFZIeczEM252N7T+MPDNKUX/ISNxb4nchliaIRnrREj6YeyiQMIy1x0cq9PkMkSpj/4v5AGy9pb5ZWuwWrkS/ncQi7fxdUq8Ir3guRFEUXlHlQml9iKTx1AqcmENgqgj/uh5dK6+FDe4d5wZsOopNCk18fv+eTs6jW9hTrCja9wyK3Esf/ovNEZ+HGQ7FqsM45+LlLuhSCBmydpDWsvM3q3So5XjH81VJvRb05sv3yQf4PO94SFFr7/08dG974nTZX27K/EL0yNKve892YCHQawI7QE8YMkpqq3mnZCZhVuwHZbT8gfBl8SjYsZNExscJEp+grOXRTBx8GznF+QZsD7F/Fw=
file: bitbucket.phar
on:
tags: true
condition: $DEPLOY = true
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# bitbucket-pr
[![Build Status](https://travis-ci.org/NINEJKH/bitbucket-cli.svg?branch=master)](https://travis-ci.org/NINEJKH/bitbucket-cli)

# bitbucket-cli

A bitbucket cli tool.

## Installation

```bash
$ sudo curl -#fLo /usr/local/bin/bitbucket https://github.com/NINEJKH/bitbucket-cli/releases/download/0.0.1/bitbucket.phar && sudo chmod +x /usr/local/bin/bitbucket
```

## Auth

1. Create App password: https://bitbucket.org/account/user/YOUR_USERNAME/app-passwords
2. Add those details in `~/.bitbucket`

```
[auth]
username = <Your Username>
password = <Your Password>
```

## Usage

### Pull requests

#### Get pull request

```bash
$ bitbucket repositories:pullrequests:get NINEJKH/bitbucket-cli 4
```

#### Update pull request

```bash
$ bitbucket repositories:pullrequests:update NINEJKH/bitbucket-cli 4 < payload.json
```

### Update pull request description

```bash
$ bitbucket repositories:pullrequests:update:description NINEJKH/bitbucket-cli 4 "new description (or via STDIN)"
```

23 changes: 23 additions & 0 deletions app/CommandLoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App;

use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;

class CommandLoaderFactory implements CommandLoaderInterface
{
public function get($name)
{

}

public function has($name)
{

}

public function getNames()
{

}
}
28 changes: 28 additions & 0 deletions app/Commands/BitbucketCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Commands;

use Buzz\Message\MessageInterface;
use Symfony\Component\Console\Command\Command;
use RuntimeException;

abstract class BitbucketCommand extends Command
{
protected function throwApiResponseError(MessageInterface $message)
{
$error_message = $message->getContent();
if (preg_match('~^{.*}$~', $error_message)) {
$error_message = json_decode($error_message, true);
if (!empty($error_message['error'])) {
$error_message = sprintf('[%s]%s%s', $error_message['error']['message'], PHP_EOL, $error_message['error']['detail']);
}
}

throw new RuntimeException($error_message, $message->getStatusCode());
}

protected function splitRepo($repo)
{
return explode('/', $repo, 2);
}
}
37 changes: 37 additions & 0 deletions app/Commands/Repositories/PullRequests/GetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Commands\Repositories\PullRequests;

use App\Commands\Repositories\PullRequestsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GetCommand extends PullRequestsCommand
{
protected function configure()
{
$this
->setName('repositories:pullrequests:get')
->setDescription('Get a specific pull request')
->addArgument('repo', InputArgument::REQUIRED, 'owner/repository_slug')
->addArgument('pull_request_id', InputArgument::REQUIRED, 'The id of the pull request.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$pull = $this->create();

list($username, $repo_slug) = $this->splitRepo($input->getArgument('repo'));

$pr = $pull->get($username, $repo_slug, $input->getArgument('pull_request_id'));

if (!$pr->isOk()) {
$this->throwApiResponseError($pr);
}

//$output->writeln(json_encode(json_decode($pr->getContent(), true), JSON_PRETTY_PRINT));
$output->writeln($pr->getContent());
}
}
36 changes: 36 additions & 0 deletions app/Commands/Repositories/PullRequests/GetDescriptionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Commands\Repositories\PullRequests;

use App\Commands\Repositories\PullRequestsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GetDescriptionCommand extends PullRequestsCommand
{
protected function configure()
{
$this
->setName('repositories:pullrequests:get:description')
->setDescription('Get a specific pull request description')
->addArgument('repo', InputArgument::REQUIRED, 'owner/repository_slug')
->addArgument('pull_request_id', InputArgument::REQUIRED, 'The id of the pull request.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$pull = $this->create();

list($username, $repo_slug) = $this->splitRepo($input->getArgument('repo'));

$pr = $pull->get($username, $repo_slug, $input->getArgument('pull_request_id'));

if (!$pr->isOk()) {
$this->throwApiResponseError($pr);
}

$output->writeln(json_decode($pr->getContent(), true)['description']);
}
}
53 changes: 53 additions & 0 deletions app/Commands/Repositories/PullRequests/UpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Commands\Repositories\PullRequests;

use App\Commands\Repositories\PullRequestsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use RuntimeException;

class UpdateCommand extends PullRequestsCommand
{
protected function configure()
{
$this
->setName('repositories:pullrequests:update')
->setDescription('Get a specific pull request')
->addOption('file', 'f', InputOption::VALUE_OPTIONAL,'Path to JSON formatted input file')
->addArgument('repo', InputArgument::REQUIRED, 'owner/repository_slug')
->addArgument('pull_request_id', InputArgument::REQUIRED, 'The id of the pull request.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($filename = $input->getOption('file')) {
$payload = file_get_contents($filename);
} elseif (ftell(STDIN) === 0) {
$payload = stream_get_contents(STDIN);
} else {
throw new RuntimeException('Please provide a filename or pipe content to STDIN.');
}

if (empty($payload)) {
throw new RuntimeException('Empty payload.');
}

$payload = json_decode($payload, true);

$pull = $this->create();

list($username, $repo_slug) = $this->splitRepo($input->getArgument('repo'));

$pr = $pull->update($username, $repo_slug, $input->getArgument('pull_request_id'), $payload);

if (!$pr->isOk()) {
$this->throwApiResponseError($pr);
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Commands\Repositories\PullRequests;

use App\Commands\Repositories\PullRequestsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use RuntimeException;

class UpdateDescriptionCommand extends PullRequestsCommand
{
protected function configure()
{
$this
->setName('repositories:pullrequests:update:description')
->setDescription('Get a specific pull request')
->addArgument('repo', InputArgument::REQUIRED, 'owner/repository_slug')
->addArgument('pull_request_id', InputArgument::REQUIRED, 'The id of the pull request.')
->addArgument('description', InputArgument::OPTIONAL, 'New description to set.')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->hasArgument('description')) {
$description = $input->getArgument('description');
} elseif (ftell(STDIN) === 0) {
$description = stream_get_contents(STDIN);
} else {
throw new RuntimeException('Empty description.');
}

if (empty($description)) {
throw new RuntimeException('Empty description.');
}

$pull = $this->create();

list($username, $repo_slug) = $this->splitRepo($input->getArgument('repo'));

$pr = $pull->get($username, $repo_slug, $input->getArgument('pull_request_id'));
if (!$pr->isOk()) {
$this->throwApiResponseError($pr);
}

$current_pr = json_decode($pr->getContent(), true);

$payload = [
'title' => $current_pr['title'],
'description' => $description,
'destination' => [
'branch' => [
'name' => $current_pr['destination']['branch']['name']
]
]
];

$pr = $pull->update($username, $repo_slug, $input->getArgument('pull_request_id'), $payload);

if (!$pr->isOk()) {
$this->throwApiResponseError($pr);
}

return true;
}
}
23 changes: 23 additions & 0 deletions app/Commands/Repositories/PullRequestsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Commands\Repositories;

use App\Commands\BitbucketCommand;
use App\Providers\BitbucketConfigProvider;
use Bitbucket\API\Http\Listener\BasicAuthListener;
use Bitbucket\API\Repositories\PullRequests;

abstract class PullRequestsCommand extends BitbucketCommand
{
protected function create()
{
$bitbucket_config = new BitbucketConfigProvider;

$pull = new PullRequests;
$pull->getClient()->addListener(
new BasicAuthListener($bitbucket_config->getBasicAuthUsername(), $bitbucket_config->getBasicAuthPassword())
);

return $pull;
}
}
Loading

0 comments on commit bb2d5a4

Please sign in to comment.