PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/stash

https://bitbucket.org/sqctest02/stash-command-line-tools
Ruby | 93 lines | 73 code | 17 blank | 3 comment | 6 complexity | bb2cb86fd71613859bc405cc0e89122b MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require File.dirname(__FILE__) + "/../lib/stash_cli"
  4. #require 'stash_cli'
  5. require 'commander/import'
  6. require 'yaml'
  7. program :name, "Atlassian Stash CLI"
  8. program :version, Atlassian::Stash::Version::STRING
  9. program :description, "Provides convenient functions for interacting with Atlassian Stash through the command line"
  10. include Atlassian::Stash
  11. include Atlassian::Stash::Git
  12. $configFile = File.join(Etc.getpwuid.dir, ".stashconfig.yml")
  13. def load_config
  14. raise "No Stash configuration found; please run configure" unless File.exists? $configFile
  15. config = YAML.load_file($configFile)
  16. raise "Stash configuration file is incomplete, please re-run configure" unless config['username'] and config['password'] and config['stash_url']
  17. config
  18. end
  19. command 'configure' do |c|
  20. c.syntax = 'configure'
  21. c.description = 'Setup configuration details to your Stash instance'
  22. c.example 'stash configure --username sebr --password s3cre7 --stash_url http://stash.mycompany.com', 'Setup Stash CLI with credentials to the Stash server'
  23. c.option '--username user', String, 'Writes your Stash username to the configuration file'
  24. c.option '--password password', String, 'Writes your Stash user password to the configuration file. If omitted, password will be prompted to be entered'
  25. c.option '--stashUrl', String, 'Writes the Stash server url to the configuration file'
  26. c.action do |args, options|
  27. username = options.username ? options.username : ask("Stash Username: ")
  28. password = options.password ? options.password : ask("Stash Password: ") { |q| q.echo = "*" }
  29. stashUrl = options.stashUrl ? options.stashUrl : ask("Stash URL: ")
  30. c = {
  31. 'username' => username.to_s,
  32. 'password' => password.to_s,
  33. 'stash_url' => stashUrl.to_s
  34. }
  35. File.open($configFile, 'w') do |out|
  36. YAML.dump(c, out)
  37. end
  38. File.chmod 0600, $configFile
  39. create_git_alias if agree "Create a git alias 'git create-pull-request'? "
  40. end
  41. end
  42. command 'pull-request' do |c|
  43. def extract_reviewers(args = [])
  44. args.collect { |user|
  45. user[1..-1] if user.start_with?("@")
  46. }.compact
  47. end
  48. c.syntax = 'pull-request [sourceBranch] targetBranch [@reviewer1 @reviewer2]'
  49. c.description = 'Create a pull request in Stash'
  50. c.example 'stash pull-request topicBranch master @michael', "Create a pull request from branch 'topicBranch' into 'master' with 'michael' added as a reviewer"
  51. c.example 'stash pull-request master', "Create a pull request from the current git branch into 'master'"
  52. c.action do |args, options|
  53. if args.length == 0
  54. command(:help).run('pull-request')
  55. Process.exit
  56. end
  57. source = args.shift
  58. if args.empty? or args.first.start_with?("@")
  59. target = source
  60. source = get_current_branch()
  61. reviewers = extract_reviewers args
  62. else
  63. target = args.shift
  64. reviewers = extract_reviewers args
  65. end
  66. ensure_within_git! do
  67. cpr = CreatePullRequest.new(load_config)
  68. cpr.create_pull_request source, target, reviewers
  69. end
  70. end
  71. end
  72. default_command :help
  73. program :help_formatter, :compact
  74. program :help, 'Authors', 'Seb Ruiz <sruiz@atlassian.com> with others'
  75. program :help, 'Website', 'https://bitbucket.org/atlassian/stash-command-line-tools'
  76. # vim set ft=ruby