Skip to content

Commit

Permalink
Merge pull request #270 from sensu/issue_256
Browse files Browse the repository at this point in the history
add extension override
  • Loading branch information
pzupan authored Jan 17, 2020
2 parents 301784f + 580ce4c commit 90e3846
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 15 deletions.
4 changes: 4 additions & 0 deletions app/authorizers/extension_authorizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ def select_default_version?
owner_or_admin?
end

def edit_extension_config_overrides?
owner_or_admin?
end

def report?
signed_in?
end
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/extensions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ def update_collection
head :ok
end

def update_config_overrides
config_overrides = {}
params[:configs].each do |config|
config.each do |input|
next if input['key'].blank?
config_overrides[ input['key'] ] = input['value']
end
end
config_overrides.sort.to_h
@extension.update(config_overrides: config_overrides)
end

def privacy
@extension.update(privacy: !@extension.privacy)
redirect_to owner_scoped_extension_url(@extension), notice: t("extension.privacy_changed")
Expand Down
2 changes: 2 additions & 0 deletions app/models/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Extension < ApplicationRecord
has_one_attached :tmp_source_file
attr_accessor :version

serialize :config_overrides, Hash

default_scope { where(enabled: true) }

#
Expand Down
8 changes: 8 additions & 0 deletions app/views/extensions/_config_overrides.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class='small-12 columns'>
<% index = 1 %>
<% extension.config_overrides.each do |key, value| %>
<div class='small-4 columns'><%= text_field_tag "configs[config_#{index}[key]]", key %></div>
<div class='small-8 columns'><%= text_field_tag "configs[config_#{index}[value]]", value %></div>
<% index += 1 %>
<% end %>
</div>
4 changes: 4 additions & 0 deletions app/views/extensions/_config_overrides_new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class='small-12 columns'>
<div class='small-4 columns'><%= text_field_tag "configs[config_new[key]]", '', placeholder: 'key' %></div>
<div class='small-8 columns'><%= text_field_tag "configs[config_new[value]]", '', placeholder: 'value' %></div>
</div>
53 changes: 53 additions & 0 deletions app/views/extensions/_manage_extension.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,59 @@

<% end %>
<% if policy(extension).edit_extension_config_overrides? %>

<li style='white-space: nowrap;'>
<%= link_to '#', class: 'default-version', rel: 'extension-config-overrides', 'data-reveal-id' => 'extension-config-overrides' do %>
<i class="fa fa-check-square-o"></i>
<span><%= t('nouns.extension').titleize %> Overrides</span>
<% end %>
</li>


<div id="extension-config-overrides" class="reveal-modal" data-reveal>
<h1><%= t('nouns.extension').titleize %> Overrides</h1>
<a class="close-reveal-modal">&#215;</a>

<%= form_for extension, url: update_config_overrides_extension_path(extension, username: extension.owner_name), method: :put, remote: true do |f| %>

<div id='config-container'>
<%= render 'extensions/config_overrides', extension: extension %>
</div>
<hr/>
<h4>New Override</h4>
<div id='config-new-container'>
<%= render 'extensions/config_overrides_new' %>
</div>

<div style="margin-top: 24px;" class="small-12 columns text-center">
<%= f.submit 'Set', class: "button primary radius tiny" %>
<a class="button secondary radius tiny close-reveal-modal">Cancel</a>
</div>
<% end %>
</div>
<% end %>
<% if policy(extension).disable? %>
<% if extension.enabled? %>
<li>
<%= link_to disable_extension_path(extension, username: extension.owner_name),
method: 'put',
rel: 'delete',
data: {confirm: "Are you sure you want to delete the #{extension.name} #{t('nouns.extension')}?"} do %>
<i class="fa fa-times"></i>
Delete <%= t('nouns.extension') %>
<% end %>
</li>
<% else %>
<li>
<%= link_to enable_extension_path(extension, username: extension.owner_name), method: 'put', rel: 'enable' do %>
<i class="fa fa-circle"></i> Enable
<% end %>
</li>
<% end %>
<% end %>
<% if version.present? && policy(version).show_config? %>
<li>
<%= link_to '#', class: 'config', rel: 'config', 'data-reveal-id' => 'config' do %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/extensions/update_config_overrides.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$("#config-container").html("<%=j (render 'extensions/config_overrides', extension: @extension) %>")
$("#config-new-container").html("<%=j (render 'extensions/config_overrides_new') %>")
21 changes: 17 additions & 4 deletions app/workers/sync_extension_contents_at_versions_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ def fetch_readme
end

def check_for_overrides(version)
return if version.blank? || version.config["overrides"].nil?
overrides = version.config["overrides"][0]
if overrides["readme_url"].present?
override_readme(version, overrides["readme_url"])
return if version.blank?
version_overrides = version.config["overrides"].nil? ? {} : version.config["overrides"][0]
extension_overrides = version.extension.config_overrides
if version_overrides.present? && version_overrides["readme_url"].present?
override_readme(version, version_overrides["readme_url"])
elsif extension_overrides["readme_url"].present?
override_readme(version, extension_overrides["readme_url"])
end
end

Expand All @@ -135,6 +138,16 @@ def override_readme(version, readme_url)
message << "#{version.version} override readme_url is not a valid markdown file."
end

# resconstruct Github url to get file, not html
# https://github.com/jspaleta/sensu-plugins-redis/blob/master/README.md
# should translate to
# https://raw.githubusercontent.com/jspaleta/sensu-plugins-redis/master/README.md

if ['github.com', 'www.github.com'].include?(url.host)
url.host = "raw.githubusercontent.com"
url.path.gsub!('/blob', '')
end

# get file contents
begin
file = url.open
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
get :sync_status
put :select_default_version
put :update_collection
put :update_config_overrides
put :privacy
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddConfigOverridesToExtensions < ActiveRecord::Migration[5.2]
def change
add_column :extensions, :config_overrides, :text
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_31_182558) do
ActiveRecord::Schema.define(version: 2020_01_13_232729) do

# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -365,6 +365,7 @@
t.string "parent_owner_name"
t.string "compilation_error"
t.string "parent_html_url"
t.text "config_overrides"
t.index ["enabled"], name: "index_extensions_on_enabled"
t.index ["github_organization_id"], name: "index_extensions_on_github_organization_id"
t.index ["name"], name: "index_extensions_on_name"
Expand Down
46 changes: 36 additions & 10 deletions spec/workers/sync_extension_contents_at_versions_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@
end

def stub_aws
Aws::S3::Resource.any_instance.stub_chain(:bucket, :exists?).and_return(true)
Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :exists?).and_return(true)
Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :public_url).and_return('https://s3.us-west-2.amazonaws.com/bucket/example.com')
Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :last_modified).and_return(DateTime.now.to_s(:db))
Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :delete).and_return(true)
Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :put).and_return(true)
allow_any_instance_of(Aws::S3::Resource).to receive_message_chain('bucket.exists?') {true}
allow_any_instance_of(Aws::S3::Resource).to receive_message_chain('bucket.object.exists?') {true}
allow_any_instance_of(Aws::S3::Resource).to receive_message_chain('bucket.object.public_url') {'https://s3.us-west-2.amazonaws.com/bucket/example.com'}
allow_any_instance_of(Aws::S3::Resource).to receive_message_chain('bucket.object.last_modified') {DateTime.now.to_s(:db)}
allow_any_instance_of(Aws::S3::Resource).to receive_message_chain('bucket.object.delete') {true}
allow_any_instance_of(Aws::S3::Resource).to receive_message_chain('bucket.object.put') {true}

#Aws::S3::Resource.any_instance.stub_chain(:bucket, :exists?).and_return(true)
#Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :exists?).and_return(true)
#Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :public_url).and_return('https://s3.us-west-2.amazonaws.com/bucket/example.com')
#Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :last_modified).and_return(DateTime.now.to_s(:db))
#Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :delete).and_return(true)
#Aws::S3::Resource.any_instance.stub_chain(:bucket, :object, :put).and_return(true)
end

describe 'tag checking' do
Expand Down Expand Up @@ -108,18 +115,37 @@ def stub_aws
describe 'overrides' do
let(:config_hash) { version.config }
let(:tags) { [version.version] }

it 'loads an alternate readme file' do
config_hash["overrides"] = {
config_hash["overrides"] = [{
"readme_url"=>"https://raw.githubusercontent.com/sensu/bonsai/master/README.md"
}]
version.update_column(:config, config_hash)
subject.perform(extension.id, tags)
version.reload
expect(version.readme).to include('bonsai.sensu.io')
end

it 'corrects link to github to load readme file as markdown' do
config_hash["overrides"] = [{
"readme_url"=>"https://github.com/sensu/bonsai/blob/master/README.md"
}
}]
version.update_column(:config, config_hash)
subject.perform(extension.id, tags)
version.reload
expect(version.readme).to include('bonsai.sensu.io')
end

end
it 'loads an alternate readme file based on extension override' do
config_overrides = {
"readme_url"=>"https://raw.githubusercontent.com/sensu/bonsai/master/README.md"
}
extension.update_column(:config_overrides, config_overrides)
subject.perform(extension.id, tags)
version.reload
expect(version.readme).to include('bonsai.sensu.io')
end

end

end

0 comments on commit 90e3846

Please sign in to comment.