-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from paviliondev/add_statistics_endpoint
Add plugin statistics endpoint
- Loading branch information
Showing
12 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class PluginManager::StatisticsController < ApplicationController | ||
skip_before_action :check_xhr, :preload_json, :verify_authenticity_token | ||
|
||
def create | ||
received_at = Time.now | ||
|
||
if discourse = DiscoursePluginStatisticsDiscourse.find_by(host: discourse_params[:host]) | ||
discourse.update!(discourse_params) | ||
else | ||
discourse = DiscoursePluginStatisticsDiscourse.create!(discourse_params) | ||
end | ||
raise Discourse::InvalidParameters.new('invalid discourse') unless discourse | ||
|
||
plugin_params[:plugins].each do |plugin| | ||
if ::PluginManager::Plugin.exists?(plugin[:name]) | ||
DiscoursePluginStatisticsPlugin.create!( | ||
received_at: received_at, | ||
discourse_id: discourse.id, | ||
**plugin.to_h | ||
) | ||
end | ||
end | ||
|
||
render json: success_json | ||
end | ||
|
||
protected | ||
|
||
def discourse_params | ||
params.require(:discourse).permit(:host, :branch, :sha) | ||
end | ||
|
||
def plugin_params | ||
params.require(:plugins) | ||
params.permit(plugins: %i(name branch sha data)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
class DiscoursePluginStatisticsDiscourse < ActiveRecord::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
class DiscoursePluginStatisticsPlugin < ActiveRecord::Base | ||
belongs_to :discourse, class_name: "DiscoursePluginStatisticsDiscourse", foreign_key: "discourse_id" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
db/migrate/20230913021018_create_discourse_plugin_statistics_discourse.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
class CreateDiscoursePluginStatisticsDiscourse < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :discourse_plugin_statistics_discourses do |t| | ||
t.string :host | ||
t.string :branch | ||
t.string :sha | ||
t.timestamps | ||
end | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
db/migrate/20230913021539_create_discourse_plugin_statistics_plugin.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
class CreateDiscoursePluginStatisticsPlugin < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :discourse_plugin_statistics_plugins do |t| | ||
t.integer :discourse_id | ||
t.datetime :received_at | ||
t.string :name | ||
t.string :branch | ||
t.string :sha | ||
t.json :data | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
Fabricator(:discourse_plugin_statistics_discourse) do | ||
host { "forum.external.com" } | ||
branch { "main" } | ||
sha { sequence(:sha) { |i| "#{i}123456" } } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
Fabricator(:discourse_plugin_statistics_plugin) do | ||
host { "forum.external.com" } | ||
branch { "main" } | ||
sha { sequence(:sha) { |i| "#{i}123456" } } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
spec/requests/plugin_manager/statistics_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
describe PluginManager::StatisticsController do | ||
let(:registered_plugin) { compatible_plugin } | ||
let(:non_registered_plugin) { third_party_plugin } | ||
let(:plugin_sha) { "12345678910" } | ||
let(:plugin_branch) { "plugin_branch" } | ||
let(:plugin_data) do | ||
{ | ||
data_key_1: "data-val-1", | ||
data_key_2: "data-val-2" | ||
} | ||
end | ||
let(:discourse_sha) { "678910" } | ||
let(:discourse_branch) { "discourse_branch" } | ||
let(:discourse_host) { "forum.external.com" } | ||
let(:params) do | ||
{ | ||
discourse: { | ||
host: discourse_host, | ||
branch: discourse_branch, | ||
sha: discourse_sha | ||
}, | ||
plugins: [ | ||
{ | ||
name: registered_plugin, | ||
branch: plugin_branch, | ||
sha: plugin_sha, | ||
data: plugin_data | ||
} | ||
] | ||
} | ||
end | ||
|
||
before do | ||
stub_github_plugin_request | ||
stub_github_user_request | ||
setup_test_plugin(registered_plugin) | ||
freeze_time | ||
end | ||
|
||
describe "#process" do | ||
it "requires valid params" do | ||
post "/plugin-manager/statistics" | ||
expect(response).not_to be_successful | ||
end | ||
|
||
context "with a new discourse" do | ||
it "creates a new discourse record" do | ||
post "/plugin-manager/statistics", params: params | ||
expect(response).to be_successful | ||
expect( | ||
DiscoursePluginStatisticsDiscourse.exists?( | ||
host: discourse_host, | ||
branch: discourse_branch, | ||
sha: discourse_sha | ||
) | ||
).to eq(true) | ||
end | ||
end | ||
|
||
context "with an existing discourse" do | ||
let!(:discourse) { Fabricate(:discourse_plugin_statistics_discourse, host: discourse_host) } | ||
|
||
it "updates the existing discourse record" do | ||
new_sha = "11121314" | ||
params[:discourse][:sha] = new_sha | ||
post "/plugin-manager/statistics", params: params | ||
expect(response).to be_successful | ||
expect( | ||
DiscoursePluginStatisticsDiscourse.exists?( | ||
host: discourse_host, | ||
branch: discourse_branch, | ||
sha: new_sha | ||
) | ||
).to eq(true) | ||
expect( | ||
DiscoursePluginStatisticsDiscourse.where(host: discourse_host).size | ||
).to eq(1) | ||
end | ||
end | ||
|
||
context "with a registered plugin" do | ||
it "saves a new plugin record" do | ||
post "/plugin-manager/statistics", params: params | ||
expect(response).to be_successful | ||
|
||
discourse = DiscoursePluginStatisticsDiscourse.find_by(host: discourse_host) | ||
expect( | ||
DiscoursePluginStatisticsPlugin.exists?( | ||
received_at: Time.now, | ||
discourse_id: discourse.id, | ||
name: registered_plugin, | ||
branch: plugin_branch, | ||
sha: plugin_sha | ||
) | ||
).to eq(true) | ||
end | ||
end | ||
|
||
context "with a non-reigstered plugin" do | ||
it "does not save a new plugin record" do | ||
params[:plugins][0][:name] = non_registered_plugin | ||
post "/plugin-manager/statistics", params: params | ||
expect(response).to be_successful | ||
|
||
discourse = DiscoursePluginStatisticsDiscourse.find_by(host: discourse_host) | ||
expect( | ||
DiscoursePluginStatisticsPlugin.exists?( | ||
received_at: Time.now, | ||
discourse_id: discourse.id, | ||
name: non_registered_plugin, | ||
branch: plugin_branch, | ||
sha: plugin_sha | ||
) | ||
).to eq(false) | ||
end | ||
end | ||
end | ||
end |