knife-stash 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ## 0.2.0
2
+
3
+ * Backend class refactoring
4
+
1
5
  ## v0.1.0
2
6
 
3
7
  * Initial release
data/README.md CHANGED
@@ -2,9 +2,15 @@
2
2
 
3
3
  A knife plugin for Atlassian Stash.
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/knife-stash.png)](http://badge.fury.io/rb/knife-stash)
6
+  
7
+ [![Code Climate](https://codeclimate.com/github/bflad/knife-stash.png)](https://codeclimate.com/github/bflad/knife-stash)
8
+
5
9
  ## Installation
6
10
 
7
- * Clone repo to ~/.chef/plugins/knife or path/to/cheforg/.chef/plugins/knife
11
+ * `gem install knife-rhn` (if using omnibus install, `/opt/chef/embedded/bin/gem install knife-rhn`)
12
+ * or... copy `lib/chef/knife/*` files to `~/.chef/plugins/knife`
13
+ * or... copy `lib/chef/knife/*` files to `path/to/cheforg/.chef/plugins/knife`
8
14
 
9
15
  ## Usage
10
16
 
@@ -0,0 +1,97 @@
1
+ #
2
+ # Author:: Brian Flad (<bflad417@gmail.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+
6
+ require 'chef/knife'
7
+ require 'highline/import'
8
+ require 'faraday'
9
+
10
+ class Chef
11
+ class Knife
12
+ module StashBase
13
+
14
+ def self.included(includer)
15
+ includer.class_eval do
16
+
17
+ deps do
18
+ require 'readline'
19
+ require 'chef/json_compat'
20
+ end
21
+
22
+ unless defined? $default
23
+ $default = Hash.new
24
+ end
25
+
26
+ option :noop,
27
+ :long => "--noop",
28
+ :description => "Perform no modifying operations",
29
+ :boolean => false
30
+
31
+ option :stash_username,
32
+ :short => "-u USERNAME",
33
+ :long => "--stash-username USERNAME",
34
+ :description => "The username for Stash"
35
+ $default[:stash_username] = ENV['USER']
36
+
37
+ option :stash_password,
38
+ :short => "-p PASSWORD",
39
+ :long => "--stash-password PASSWORD",
40
+ :description => "The password for Stash"
41
+
42
+ option :stash_hostname,
43
+ :long => "--stash-hostname HOSTNAME",
44
+ :description => "The hostname for Stash"
45
+ end
46
+ end
47
+
48
+ def display_stash_error(message,response)
49
+ ui.fatal message
50
+ JSON.parse(response.body)['errors'].each do |error|
51
+ ui.fatal "#{error['context'] ? error['context'] + ": " : "" }#{error['message']}"
52
+ end
53
+ end
54
+
55
+ def get_all_values(stash,url,response)
56
+ r = JSON.parse(response.body)
57
+ yield r['values']
58
+ until r['isLastPage']
59
+ response = stash.get url, { :start => r['nextPageStart'] }
60
+ r = JSON.parse(response.body)
61
+ yield r['values']
62
+ end
63
+ end
64
+
65
+ def get_config(key)
66
+ key = key.to_sym
67
+ rval = config[key] || Chef::Config[:knife][key] || $default[key]
68
+ Chef::Log.debug("value for config item #{key}: #{rval}")
69
+ rval
70
+ end
71
+
72
+ def get_stash_connection
73
+ config[:stash_hostname] = ask("Stash Hostname: ") { |q| q.echo = "*" } unless get_config(:stash_hostname)
74
+ config[:stash_username] = ask("Stash Username for #{get_config(:stash_hostname)}: ") { |q| q.echo = "*" } unless get_config(:stash_username)
75
+ config[:stash_password] = ask("Stash Password for #{get_config(:stash_username)}: ") { |q| q.echo = "*" } unless get_config(:stash_password)
76
+
77
+ connection = Faraday.new(:url => "https://#{get_config(:stash_hostname)}", :ssl => {:verify => false}) do |faraday|
78
+ faraday.request :url_encoded # form-encode POST params
79
+ #faraday.response :logger # log requests to STDOUT
80
+ faraday.adapter :net_http # make requests with Net::HTTP
81
+ end
82
+ connection.basic_auth(get_config(:stash_username),get_config(:stash_password))
83
+ connection.url_prefix = "https://#{get_config(:stash_hostname)}/rest/api/1.0"
84
+ connection
85
+ end
86
+
87
+ def get_repo_https_url(project_key,repo)
88
+ "https://#{get_config(:stash_username)}@#{get_config(:stash_hostname)}/scm/#{project_key}/#{repo}.git"
89
+ end
90
+
91
+ def get_repo_ssh_url(project_key,repo)
92
+ "ssh://git@#{get_config(:stash_hostname)}:7999/#{project_key}/#{repo}.git"
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -3,66 +3,68 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashProjectCreate < Knife
9
11
 
10
- class StashProjectCreate < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash project create KEY NAME (options)"
13
- category "stash"
14
+ banner "knife stash project create KEY NAME (options)"
15
+ category "stash"
14
16
 
15
- option :description,
16
- :short => "-d DESCRIPTION",
17
- :long => "--description DESCRIPTION",
18
- :description => "The description for the project"
17
+ option :description,
18
+ :short => "-d DESCRIPTION",
19
+ :long => "--description DESCRIPTION",
20
+ :description => "The description for the project"
19
21
 
20
- get_common_options
22
+ def run
23
+ $stdout.sync = true
24
+
25
+ key = name_args.first
21
26
 
22
- def run
23
-
24
- key = name_args.first
25
-
26
- if key.nil?
27
- ui.fatal "You need a project key!"
28
- show_usage
29
- exit 1
30
- end
27
+ if key.nil?
28
+ ui.fatal "You need a project key!"
29
+ show_usage
30
+ exit 1
31
+ end
31
32
 
32
- name = name_args[1]
33
+ name = name_args[1]
33
34
 
34
- if name.nil?
35
- ui.fatal "You need a project name!"
36
- show_usage
37
- exit 1
38
- end
35
+ if name.nil?
36
+ ui.fatal "You need a project name!"
37
+ show_usage
38
+ exit 1
39
+ end
39
40
 
40
- args = name_args[2]
41
- if args.nil?
42
- args = ""
43
- end
41
+ args = name_args[2]
42
+ if args.nil?
43
+ args = ""
44
+ end
44
45
 
45
- stash = get_stash_connection
46
- project = { :name => name, :key => key }
47
- project[:description] = get_config(:description) if get_config(:description)
46
+ stash = get_stash_connection
47
+ project = { :name => name, :key => key }
48
+ project[:description] = get_config(:description) if get_config(:description)
48
49
 
49
- if get_config(:noop)
50
- ui.info "#{ui.color "Skipping project creation process because --noop specified.", :red}"
51
- else
52
- response = stash.post do |post|
53
- post.url "projects"
54
- post.headers['Content-Type'] = "application/json"
55
- post.body = JSON.generate(project)
56
- end
57
- if response.success?
58
- ui.info "Created Stash Project: #{key} (#{name})"
50
+ if get_config(:noop)
51
+ ui.info "#{ui.color "Skipping project creation process because --noop specified.", :red}"
59
52
  else
60
- display_stash_error "Could not create Stash project!", response
61
- exit 1
53
+ response = stash.post do |post|
54
+ post.url "projects"
55
+ post.headers['Content-Type'] = "application/json"
56
+ post.body = JSON.generate(project)
57
+ end
58
+ if response.success?
59
+ ui.info "Created Stash Project: #{key} (#{name})"
60
+ else
61
+ display_stash_error "Could not create Stash project!", response
62
+ exit 1
63
+ end
62
64
  end
65
+
63
66
  end
64
-
65
- end
66
67
 
68
+ end
67
69
  end
68
70
  end
@@ -3,64 +3,66 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashProjectDelete < Knife
9
11
 
10
- class StashProjectDelete < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash project delete KEY (options)"
13
- category "stash"
14
+ banner "knife stash project delete KEY (options)"
15
+ category "stash"
14
16
 
15
- option :delete_repos,
16
- :short => "-d",
17
- :long => "--delete-repos",
18
- :description => "Delete all repositories in project.",
19
- :boolean => false
17
+ option :delete_repos,
18
+ :short => "-d",
19
+ :long => "--delete-repos",
20
+ :description => "Delete all repositories in project.",
21
+ :boolean => false
20
22
 
21
- get_common_options
23
+ def run
24
+ $stdout.sync = true
25
+
26
+ key = name_args.first
22
27
 
23
- def run
24
-
25
- key = name_args.first
26
-
27
- if key.nil?
28
- ui.fatal "You need a project key!"
29
- show_usage
30
- exit 1
31
- end
28
+ if key.nil?
29
+ ui.fatal "You need a project key!"
30
+ show_usage
31
+ exit 1
32
+ end
32
33
 
33
- stash = get_stash_connection
34
+ stash = get_stash_connection
34
35
 
35
- if get_config(:noop)
36
- ui.info "#{ui.color "Skipping project deletion process because --noop specified.", :red}"
37
- else
38
- if get_config(:delete_repos)
39
- url = "projects/#{key}/repos"
40
- response = stash.get url
41
- if response.success?
42
- get_all_values stash,url,response do |values|
43
- values.each do |repo|
44
- repo_delete = StashRepoDelete.new
45
- repo_delete.name_args = [ key, repo['name'] ]
46
- repo_delete.run
36
+ if get_config(:noop)
37
+ ui.info "#{ui.color "Skipping project deletion process because --noop specified.", :red}"
38
+ else
39
+ if get_config(:delete_repos)
40
+ url = "projects/#{key}/repos"
41
+ response = stash.get url
42
+ if response.success?
43
+ get_all_values stash,url,response do |values|
44
+ values.each do |repo|
45
+ repo_delete = StashRepoDelete.new
46
+ repo_delete.name_args = [ key, repo['name'] ]
47
+ repo_delete.run
48
+ end
47
49
  end
50
+ else
51
+ display_stash_error "Could not delete Stash project repositories!", response
52
+ exit 1
48
53
  end
54
+ end
55
+ response = stash.delete "projects/#{key}"
56
+ if response.success?
57
+ ui.info "Deleted Stash Project: #{key}"
49
58
  else
50
- display_stash_error "Could not delete Stash project repositories!", response
59
+ display_stash_error "Could not delete Stash project!", response
51
60
  exit 1
52
61
  end
53
62
  end
54
- response = stash.delete "projects/#{key}"
55
- if response.success?
56
- ui.info "Deleted Stash Project: #{key}"
57
- else
58
- display_stash_error "Could not delete Stash project!", response
59
- exit 1
60
- end
63
+
61
64
  end
62
-
63
- end
64
65
 
66
+ end
65
67
  end
66
68
  end
@@ -3,43 +3,45 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashProjectRepos < Knife
9
11
 
10
- class StashProjectRepos < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash project repos KEY (options)"
13
- category "stash"
14
+ banner "knife stash project repos KEY (options)"
15
+ category "stash"
14
16
 
15
- get_common_options
17
+ def run
18
+ $stdout.sync = true
19
+
20
+ key = name_args.first
16
21
 
17
- def run
18
-
19
- key = name_args.first
20
-
21
- if key.nil?
22
- ui.fatal "You need a project key!"
23
- show_usage
24
- exit 1
25
- end
22
+ if key.nil?
23
+ ui.fatal "You need a project key!"
24
+ show_usage
25
+ exit 1
26
+ end
26
27
 
27
- stash = get_stash_connection
28
- url = "projects/#{key}/repos"
29
- response = stash.get url
28
+ stash = get_stash_connection
29
+ url = "projects/#{key}/repos"
30
+ response = stash.get url
30
31
 
31
- if response.success?
32
- get_all_values stash,url,response do |values|
33
- values.each do |repo|
34
- ui.info "#{repo['slug']} (#{repo['name']})"
32
+ if response.success?
33
+ get_all_values stash,url,response do |values|
34
+ values.each do |repo|
35
+ ui.info "#{repo['slug']} (#{repo['name']})"
36
+ end
35
37
  end
38
+ else
39
+ display_stash_error "Could not list Stash project repositories!", response
40
+ exit 1
36
41
  end
37
- else
38
- display_stash_error "Could not list Stash project repositories!", response
39
- exit 1
42
+
40
43
  end
41
-
42
- end
43
44
 
45
+ end
44
46
  end
45
47
  end
@@ -3,35 +3,37 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashProjects < Knife
9
11
 
10
- class StashProjects < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash projects (options)"
13
- category "stash"
14
+ banner "knife stash projects (options)"
15
+ category "stash"
14
16
 
15
- get_common_options
17
+ def run
18
+ $stdout.sync = true
19
+
20
+ stash = get_stash_connection
21
+ url = "projects"
22
+ response = stash.get url
16
23
 
17
- def run
18
-
19
- stash = get_stash_connection
20
- url = "projects"
21
- response = stash.get url
22
-
23
- if response.success?
24
- get_all_values stash,url,response do |values|
25
- values.each do |project|
26
- ui.info "#{project['key']}: #{project['name']}"
24
+ if response.success?
25
+ get_all_values stash,url,response do |values|
26
+ values.each do |project|
27
+ ui.info "#{project['key']}: #{project['name']}"
28
+ end
27
29
  end
30
+ else
31
+ display_stash_error "Could not list Stash projects!", response
32
+ exit 1
28
33
  end
29
- else
30
- display_stash_error "Could not list Stash projects!", response
31
- exit 1
34
+
32
35
  end
33
-
34
- end
35
36
 
37
+ end
36
38
  end
37
39
  end
@@ -3,62 +3,64 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashRepoCreate < Knife
9
11
 
10
- class StashRepoCreate < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash repo create PROJECT_KEY NAME (options)"
13
- category "stash"
14
+ banner "knife stash repo create PROJECT_KEY NAME (options)"
15
+ category "stash"
14
16
 
15
- get_common_options
17
+ def run
18
+ $stdout.sync = true
19
+
20
+ project_key = name_args.first
16
21
 
17
- def run
18
-
19
- project_key = name_args.first
20
-
21
- if project_key.nil?
22
- ui.fatal "You need a project key!"
23
- show_usage
24
- exit 1
25
- end
22
+ if project_key.nil?
23
+ ui.fatal "You need a project key!"
24
+ show_usage
25
+ exit 1
26
+ end
26
27
 
27
- name = name_args[1]
28
+ name = name_args[1]
28
29
 
29
- if name.nil?
30
- ui.fatal "You need a repository name!"
31
- show_usage
32
- exit 1
33
- end
30
+ if name.nil?
31
+ ui.fatal "You need a repository name!"
32
+ show_usage
33
+ exit 1
34
+ end
34
35
 
35
- args = name_args[2]
36
- if args.nil?
37
- args = ""
38
- end
36
+ args = name_args[2]
37
+ if args.nil?
38
+ args = ""
39
+ end
39
40
 
40
- stash = get_stash_connection
41
- repo = { :name => name, :scmId => "git" }
41
+ stash = get_stash_connection
42
+ repo = { :name => name, :scmId => "git" }
42
43
 
43
- if get_config(:noop)
44
- ui.info "#{ui.color "Skipping repo creation process because --noop specified.", :red}"
45
- else
46
- response = stash.post do |post|
47
- post.url "projects/#{project_key}/repos"
48
- post.headers['Content-Type'] = "application/json"
49
- post.body = JSON.generate(repo)
50
- end
51
- if response.success?
52
- ui.info "Created Stash Repository: #{project_key}/#{name}"
53
- ui.info "Available via (HTTPS): #{get_repo_https_url(project_key,name)}"
54
- ui.info "Available via (SSH): #{get_repo_ssh_url(project_key,name)}"
44
+ if get_config(:noop)
45
+ ui.info "#{ui.color "Skipping repo creation process because --noop specified.", :red}"
55
46
  else
56
- display_stash_error "Could not create Stash repository!", response
57
- exit 1
47
+ response = stash.post do |post|
48
+ post.url "projects/#{project_key}/repos"
49
+ post.headers['Content-Type'] = "application/json"
50
+ post.body = JSON.generate(repo)
51
+ end
52
+ if response.success?
53
+ ui.info "Created Stash Repository: #{project_key}/#{name}"
54
+ ui.info "Available via (HTTPS): #{get_repo_https_url(project_key,name)}"
55
+ ui.info "Available via (SSH): #{get_repo_ssh_url(project_key,name)}"
56
+ else
57
+ display_stash_error "Could not create Stash repository!", response
58
+ exit 1
59
+ end
58
60
  end
61
+
59
62
  end
60
-
61
- end
62
63
 
64
+ end
63
65
  end
64
66
  end
@@ -3,50 +3,52 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashRepoDelete < Knife
9
11
 
10
- class StashRepoDelete < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash repo delete KEY REPO (options)"
13
- category "stash"
14
+ banner "knife stash repo delete KEY REPO (options)"
15
+ category "stash"
14
16
 
15
- get_common_options
17
+ def run
18
+ $stdout.sync = true
19
+
20
+ key = name_args.first
16
21
 
17
- def run
18
-
19
- key = name_args.first
20
-
21
- if key.nil?
22
- ui.fatal "You need a project key!"
23
- show_usage
24
- exit 1
25
- end
22
+ if key.nil?
23
+ ui.fatal "You need a project key!"
24
+ show_usage
25
+ exit 1
26
+ end
26
27
 
27
- repo = name_args[1]
28
+ repo = name_args[1]
28
29
 
29
- if repo.nil?
30
- ui.fatal "You need a repository name!"
31
- show_usage
32
- exit 1
33
- end
30
+ if repo.nil?
31
+ ui.fatal "You need a repository name!"
32
+ show_usage
33
+ exit 1
34
+ end
34
35
 
35
- stash = get_stash_connection
36
+ stash = get_stash_connection
36
37
 
37
- if get_config(:noop)
38
- ui.info "#{ui.color "Skipping project repository deletion process because --noop specified.", :red}"
39
- else
40
- response = stash.delete "projects/#{key}/repos/#{repo}"
41
- if response.success?
42
- ui.info "Deleted Stash Repository: #{key}/#{repo}"
38
+ if get_config(:noop)
39
+ ui.info "#{ui.color "Skipping project repository deletion process because --noop specified.", :red}"
43
40
  else
44
- display_stash_error "Could not delete Stash respository!", response
45
- exit 1
41
+ response = stash.delete "projects/#{key}/repos/#{repo}"
42
+ if response.success?
43
+ ui.info "Deleted Stash Repository: #{key}/#{repo}"
44
+ else
45
+ display_stash_error "Could not delete Stash respository!", response
46
+ exit 1
47
+ end
46
48
  end
49
+
47
50
  end
48
-
49
- end
50
51
 
52
+ end
51
53
  end
52
54
  end
@@ -3,47 +3,49 @@
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
 
6
- module StashKnifePlugin
6
+ require 'chef/knife/stash_base'
7
7
 
8
- require 'chef/knife'
8
+ class Chef
9
+ class Knife
10
+ class StashRepos < Knife
9
11
 
10
- class StashRepos < BaseStashCommand
12
+ include Knife::StashBase
11
13
 
12
- banner "knife stash repos (options)"
13
- category "stash"
14
+ banner "knife stash repos (options)"
15
+ category "stash"
14
16
 
15
- get_common_options
17
+ def run
18
+ $stdout.sync = true
16
19
 
17
- def run
20
+ stash = get_stash_connection
21
+ url = "projects"
22
+ project_response = stash.get url
18
23
 
19
- stash = get_stash_connection
20
- url = "projects"
21
- project_response = stash.get url
24
+ if project_response.success?
25
+ get_all_values stash,url,project_response do |project_values|
26
+ project_values.each do |project|
27
+ url = "projects/#{project['key']}/repos"
28
+ repo_response = stash.get url
22
29
 
23
- if project_response.success?
24
- get_all_values stash,url,project_response do |project_values|
25
- project_values.each do |project|
26
- url = "projects/#{project['key']}/repos"
27
- repo_response = stash.get url
28
-
29
- if repo_response.success?
30
- get_all_values stash,url,repo_response do |repo_values|
31
- repo_values.each do |repo|
32
- ui.info "#{project['key']}/#{repo['slug']}"
30
+ if repo_response.success?
31
+ get_all_values stash,url,repo_response do |repo_values|
32
+ repo_values.each do |repo|
33
+ ui.info "#{project['key']}/#{repo['slug']}"
34
+ end
33
35
  end
36
+ else
37
+ display_stash_error "Could not list Stash project repositories!", repo_response
38
+ exit 1
34
39
  end
35
- else
36
- display_stash_error "Could not list Stash project repositories!", repo_response
37
- exit 1
38
40
  end
39
41
  end
42
+ else
43
+ display_stash_error "Could not list Stash projects!", project_response
44
+ exit 1
40
45
  end
41
- else
42
- display_stash_error "Could not list Stash projects!", project_response
43
- exit 1
46
+
44
47
  end
45
-
46
- end
47
48
 
49
+ end
48
50
  end
49
51
  end
@@ -1,5 +1,6 @@
1
1
  module Knife
2
2
  module Stash
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
+ MAJOR, MINOR, TINY = VERSION.split('.')
4
5
  end
5
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-stash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-20 00:00:00.000000000 Z
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -40,7 +40,7 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - knife-stash.gemspec
43
- - lib/chef/knife/BaseStashCommand.rb
43
+ - lib/chef/knife/stash_base.rb
44
44
  - lib/chef/knife/stash_project_create.rb
45
45
  - lib/chef/knife/stash_project_delete.rb
46
46
  - lib/chef/knife/stash_project_repos.rb
@@ -1,93 +0,0 @@
1
- #
2
- # Author:: Brian Flad (<bflad417@gmail.com>)
3
- # License:: Apache License, Version 2.0
4
- #
5
-
6
- module StashKnifePlugin
7
-
8
- require 'chef/knife'
9
- require 'highline/import'
10
- require 'faraday'
11
-
12
- class BaseStashCommand < Chef::Knife
13
-
14
- deps do
15
- require 'highline/import'
16
- end
17
-
18
- def self.get_common_options
19
- unless defined? $default
20
- $default = Hash.new
21
- end
22
-
23
- option :noop,
24
- :long => "--noop",
25
- :description => "Perform no modifying operations",
26
- :boolean => false
27
-
28
- option :stash_username,
29
- :short => "-u USERNAME",
30
- :long => "--stash-username USERNAME",
31
- :description => "The username for Stash"
32
- $default[:stash_username] = ENV['USER']
33
-
34
- option :stash_password,
35
- :short => "-p PASSWORD",
36
- :long => "--stash-password PASSWORD",
37
- :description => "The password for Stash"
38
-
39
- option :stash_hostname,
40
- :long => "--stash-hostname HOSTNAME",
41
- :description => "The hostname for Stash"
42
-
43
- end
44
-
45
- def display_stash_error(message,response)
46
- ui.fatal message
47
- JSON.parse(response.body)['errors'].each do |error|
48
- ui.fatal "#{error['context'] ? error['context'] + ": " : "" }#{error['message']}"
49
- end
50
- end
51
-
52
- def get_all_values(stash,url,response)
53
- r = JSON.parse(response.body)
54
- yield r['values']
55
- until r['isLastPage']
56
- response = stash.get url, { :start => r['nextPageStart'] }
57
- r = JSON.parse(response.body)
58
- yield r['values']
59
- end
60
- end
61
-
62
- def get_config(key)
63
- key = key.to_sym
64
- rval = config[key] || Chef::Config[:knife][key] || $default[key]
65
- Chef::Log.debug("value for config item #{key}: #{rval}")
66
- rval
67
- end
68
-
69
- def get_stash_connection
70
- config[:stash_hostname] = ask("Stash Hostname: ") { |q| q.echo = "*" } unless get_config(:stash_hostname)
71
- config[:stash_username] = ask("Stash Username for #{get_config(:stash_hostname)}: ") { |q| q.echo = "*" } unless get_config(:stash_username)
72
- config[:stash_password] = ask("Stash Password for #{get_config(:stash_username)}: ") { |q| q.echo = "*" } unless get_config(:stash_password)
73
-
74
- connection = Faraday.new(:url => "https://#{get_config(:stash_hostname)}", :ssl => {:verify => false}) do |faraday|
75
- faraday.request :url_encoded # form-encode POST params
76
- #faraday.response :logger # log requests to STDOUT
77
- faraday.adapter :net_http # make requests with Net::HTTP
78
- end
79
- connection.basic_auth(get_config(:stash_username),get_config(:stash_password))
80
- connection.url_prefix = "https://#{get_config(:stash_hostname)}/rest/api/1.0"
81
- connection
82
- end
83
-
84
- def get_repo_https_url(project_key,repo)
85
- "https://#{get_config(:stash_username)}@#{get_config(:stash_hostname)}/scm/#{project_key}/#{repo}.git"
86
- end
87
-
88
- def get_repo_ssh_url(project_key,repo)
89
- "ssh://git@#{get_config(:stash_hostname)}:7999/#{project_key}/#{repo}.git"
90
- end
91
-
92
- end
93
- end