コマンドラインからBitbucketのリポジトリを操作するためのツール「bitbucket-cli」でリポジトリを作成してみる

2019-03-09Bash,Git

はじめに

会社で利用しているGitのホスティングサービスは Bitbucket logo ですが、コマンドラインからリポジトリ作成やプルリクエストを投げられると便利です。

自分のPCに導入している bitbucket-cli を紹介します。

※ちなみにGithub用のコマンドラインツールは Githubの無料アカウントユーザにもPrivateリポジトリ作成が無制限になったのでコマンド一発でどんどん作成するための環境を作ってみる を参照ください。

検証環境

$ uname -a
Linux 8f10b748d929 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

$ bash -version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

$ python --version
Python 2.7.5

必要なもの

  • Python(2.7.9以上のバージョン)

インストール

pip のインストール

Pythonで動作するツールということで、 pip のインストールが必要。

以下のコマンドを実行し、バージョンが表示されていればすでにインストール済みなので、 pip のセットアップは不要。

$ pip --version
pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)

インストールは簡単。

pipのインストール方法 ページにある通り、実行するだけ。

$ cd /tmp

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

$ python get-pip.py
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
    100% |################################| 1.4MB 3.6MB/s
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/d1/6a/4b2fcefd2ea0868810e92d519dacac1ddc64a2e53ba9e3422c3b62b378a6/setuptools-40.8.0-py2.py3-none-any.whl (575kB)
    100% |################################| 583kB 7.5MB/s
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/96/ba/a4702cbb6a3a485239fbe9525443446203f00771af9ac000fa3ef2788201/wheel-0.33.1-py2.py3-none-any.whl
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-19.0.3 setuptools-40.8.0 wheel-0.33.1

bitbucket-cliのインストール

以下のコマンドを実行するだけです。

$ pip install bitbucket-cli

最後に以下のメッセージが表示されていればインストールは成功です。

Successfully installed bitbucket-cli-0.5.1 certifi-2018.11.29 chardet-3.0.4 idna-2.8 requests-2.21.0 urllib3-1.24.1

実行できるかを確認してみましょう。
コマンド名は bb 。( bitbucket というコマンドのショートカットが bb のようですが、長いのでこちらを使っています。 )

[root@8f10b748d929 /]$ bb help
usage: bitbucket <command> [<args>]
bb: error: argument create
  update
  delete
  clone
  create_from_local
  pull_request
  pull
  download
  list
  privilege: invalid choice: 'help' (choose from 'create', 'update', 'delete', 'clone', 'pull', 'create_from_local', 'pull_request', 'download', 'list', 'privilege', 'group-privilege', 'add_remote')

一番下のメッセージがエラーっぽく見えてドキッとするが、動作はしているようです。

使ってみる

リモートリポジトリを作成する(すでに存在しているローカルリポジトリから)

# ディレクトリを作成
$ mkdir bb-test
$ cd bb-test/

# Git管理対象とする
$ git init
Initialized empty Git repository in /bb-test/.git/

# README.mdファイルを作成しコミットしておく
$ echo hello > README.md
$ git add .
$ git commit -m "first commit."
[master (root-commit) a783f9d] first commit.
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

bb コマンドを使ってローカルリポジトリからリモートリポジトリを作成してみます。

# 実行!
$ bb create_from_local
username: genzouw
password:
Username for 'https://bitbucket.org': genzouw
Password for 'https://genzouw@bitbucket.org':
Counting objects: 3, done.
Writing objects: 100% (3/3), 216 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bitbucket.org/genzouw/bb-test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

何故かパスワードを2回聞かれました。

パスワード入力を不要とするための設定を行う

作業効率を上げるためにパスワード入力を不要としておきたい と思います。

ホームディレクトリに以下のような設定ファイルを作成すれば、パスワード入力を飛ばすことができます。
( ファイル名は ~/.bitbucket )

cat <<'EOF' > ~/.bitbucket
[auth]
username = genzouw
password = *************

[options]
scm = 'git'
protocol = 'https'
EOF

この設定を行った後に再度リモートリポジトリ作成をしてみました。

# bb-test2で作成してみた。
$ pwd
/bb-test2

$ bb create_from_local
****************************************************
  Warning: config file is readable by other users.
  If you are storing your password in this file,
  it may not be secure
****************************************************
Username for 'https://bitbucket.org': genzouw
Password for 'https://genzouw@bitbucket.org':
Counting objects: 3, done.
Writing objects: 100% (3/3), 216 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bitbucket.org/genzouw/bb-test2.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

パスワードを聞かれる回数は2回から1回に減ったが、やはりリポジトリ作成時には入力が必要でした。。。

リモートリポジトリを一覧表示する

$ bb list
[- git] genzouw/bb-test
[- git] genzouw/bb-test2

リモートリポジトリを削除する

$ bb delete bb-test
****************************************************
  Warning: config file is readable by other users.
  If you are storing your password in this file,
  it may not be secure
****************************************************
genzouw/bb-test was deleted.

プルリクエストを上げる

# developブランチからmaterブランチへのプルリクエストをあげる
$ bb pull_request --owner genzouw develop master

ひとこと

こちら、CentOS環境だと問題ないのですが、最近 Python3.7 にバージョンアップしたMac環境だとうまく動きませんでした。

(2019-03-09追記)

以下に Python3 環境の解消方法をまとめました!

2019-03-09Bash,Git