Class: Stash::Client
- Inherits:
-
Object
- Object
- Stash::Client
- Defined in:
- lib/stash-api/client.rb
Constant Summary collapse
- SETTINGS_HOOKS_URL =
File.join('settings', 'hooks')
- BRANCHING_MODEL_URL =
File.join('automerge', 'enabled')
Instance Attribute Summary collapse
-
#project ⇒ Object
Returns the value of attribute project.
-
#repository_name ⇒ Object
Returns the value of attribute repository_name.
-
#server ⇒ Object
Returns the value of attribute server.
-
#ssh_url ⇒ Object
Returns the value of attribute ssh_url.
Instance Method Summary collapse
- #get_automatic_merging ⇒ Object
- #get_branch_permissions ⇒ Object
- #get_hooks ⇒ Object
- #get_pull_request_settings ⇒ Object
-
#initialize(username, password, config = {:follow_fork => true, :url => nil, :verify_ssl => true}) ⇒ Client
constructor
A new instance of Client.
- #set_automatic_merging ⇒ Object
- #set_branch_permissions(permissions) ⇒ Object
- #set_hooks(hooks) ⇒ Object
- #set_pull_request_settings(settings) ⇒ Object
Constructor Details
#initialize(username, password, config = {:follow_fork => true, :url => nil, :verify_ssl => true}) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/stash-api/client.rb', line 8 def initialize(username, password, config = {:follow_fork => true, :url => nil, :verify_ssl => true}) raise 'API username must be specified' if !username raise 'API password must be specified' if !password @username = username @password = password remote_origin_url = config[:url] || %x[git config --get remote.origin.url] raise "Repository URL is not set and cannot be inferred from the git config." if !remote_origin_url match = remote_origin_url.match(/(ssh|https?):\/\/([^@]*@)?(?<server>[^\:\/]*)[^\/]*\/(scm\/)?(?<project>[^\/].*)\/(?<repository_name>[^\/]*)\.git$/) raise "Remote origin cannot be inferred from the url: #{remote_origin_url}. Run `git remote add origin URL` to add an origin." if !match @server = match[:server] repository_information = nil RestClient::Request.new( :method => :get, :url => URI::encode(File.join("https://#{@server}", 'rest', 'api', '1.0', 'projects', match[:project], 'repos', match[:repository_name])), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| repository_information = JSON.parse(response.body) raise "Could not retrieve repository information - #{JSON::pretty_generate(repository_information)}" if !response.code.to_s.match(/^2\d{2}$/) end #If the repository is a fork, use it's forked source to get this information repository_information = repository_information['origin'] if repository_information['origin'] && config[:follow_fork] @project = repository_information['project']['key'] @repository_name = repository_information['slug'] @ssh_url = repository_information['links']['clone'].select{|link| link['name'].match(/^ssh$/i)}.first['href'] #Set remote api URLs @remote_api_url = File.join("https://#{@server}", 'rest', 'api', '1.0', 'projects', @project, 'repos', @repository_name) @branch_permissions_url = File.join("https://#{@server}", 'rest', 'branch-permissions', '2.0', 'projects', @project, 'repos', @repository_name, 'restrictions') @branchring_model_url = File.join("https://#{@server}", 'rest', 'branch-utils', '1.0', 'projects', @project, 'repos', @repository_name) @pull_request_settings = File.join("https://#{@server}", 'rest', 'pullrequest-settings', '1.0', 'projects', @project, 'repos', @repository_name) end |
Instance Attribute Details
#project ⇒ Object
Returns the value of attribute project.
6 7 8 |
# File 'lib/stash-api/client.rb', line 6 def project @project end |
#repository_name ⇒ Object
Returns the value of attribute repository_name.
6 7 8 |
# File 'lib/stash-api/client.rb', line 6 def repository_name @repository_name end |
#server ⇒ Object
Returns the value of attribute server.
6 7 8 |
# File 'lib/stash-api/client.rb', line 6 def server @server end |
#ssh_url ⇒ Object
Returns the value of attribute ssh_url.
6 7 8 |
# File 'lib/stash-api/client.rb', line 6 def ssh_url @ssh_url end |
Instance Method Details
#get_automatic_merging ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/stash-api/client.rb', line 178 def get_automatic_merging() RestClient::Request.new( :method => :get, :url => URI::encode(File.join(@branchring_model_url, BRANCHING_MODEL_URL)), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not get branchig model - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) return response.code == 204 end end |
#get_branch_permissions ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/stash-api/client.rb', line 114 def () RestClient::Request.new( :method => :get, :url => URI::encode("#{@branch_permissions_url}?limit=1000"), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not get branch permissions - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) return JSON::parse(response) end end |
#get_hooks ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/stash-api/client.rb', line 73 def get_hooks() config = {} RestClient::Request.new( :method => :get, :url => URI::encode("#{File.join(@remote_api_url, SETTINGS_HOOKS_URL)}?limit=1000"), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not get hooks - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) JSON::parse(response)['values'].map do |h| hook = h['details']['key'] config[hook] = {:config => nil, :enabled => h['enabled']} RestClient::Request.new( :method => :get, :url => URI::encode(File.join(@remote_api_url, SETTINGS_HOOKS_URL, hook, 'settings')), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| config[hook][:config] = JSON::parse(response != '' ? response : '{}') if response.code.to_s.match(/^2\d{2}$/) end end end config end |
#get_pull_request_settings ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/stash-api/client.rb', line 145 def get_pull_request_settings() settings = {} RestClient::Request.new( :method => :get, :url => URI::encode(File.join(@pull_request_settings, 'requiredBuildsCount')), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| settings[:builds] = response.body if response.code.to_s.match(/^2\d{2}$/) end RestClient::Request.new( :method => :get, :url => URI::encode(File.join(@pull_request_settings, 'requiredApproversCount')), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| settings[:approvers] = response.body if response.code.to_s.match(/^2\d{2}$/) end settings end |
#set_automatic_merging ⇒ Object
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/stash-api/client.rb', line 167 def set_automatic_merging() RestClient::Request.new( :method => :put, :url => URI::encode(File.join(@branchring_model_url, BRANCHING_MODEL_URL)), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not get branchig model - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) end end |
#set_branch_permissions(permissions) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/stash-api/client.rb', line 99 def () raise "permissions list is required" if ! [].flatten.each do || RestClient::Request.new( :method => :post, :url => URI::encode("#{@branch_permissions_url}?"), :user => @username, :password => @password, :payload => .to_json, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not set branch permissions - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) end end end |
#set_hooks(hooks) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/stash-api/client.rb', line 48 def set_hooks(hooks) hooks.keys.each do |hook| config = hooks[hook][:config] if config RestClient::Request.new( :method => :put, :url => URI::encode("#{File.join(@remote_api_url, SETTINGS_HOOKS_URL)}/#{hook}/settings"), :user => @username, :password => @password, :payload => config.to_json, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not configure hook: #{hook} - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) end end RestClient::Request.new( :method => :put, :url => URI::encode("#{File.join(@remote_api_url, SETTINGS_HOOKS_URL)}/#{hook}/enabled"), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not enable hook: #{hook} - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) end end end |
#set_pull_request_settings(settings) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/stash-api/client.rb', line 126 def set_pull_request_settings(settings) RestClient::Request.new( :method => :put, :url => URI::encode(File.join(@pull_request_settings, 'requiredBuildsCount', settings[:builds].to_s)), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not get set required builds - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) end if settings[:builds] RestClient::Request.new( :method => :put, :url => URI::encode(File.join(@pull_request_settings, 'requiredApproversCount', settings[:approvers].to_s)), :user => @username, :password => @password, :headers => { :accept => :json, :content_type => :json }).execute do |response, request, result| raise "Could not get set pull request approvers - #{JSON::pretty_generate(JSON::parse(response.body))}" if !response.code.to_s.match(/^2\d{2}$/) end if settings[:approvers] end |