Skip to content

Commit

Permalink
Merge pull request #9 from paviliondev/add_statistics_endpoint
Browse files Browse the repository at this point in the history
Add plugin statistics endpoint
  • Loading branch information
angusmcleod authored Sep 22, 2023
2 parents 1d18ccf + 6f0da7d commit df828f6
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 6 deletions.
39 changes: 39 additions & 0 deletions app/controllers/plugin_manager/statistics_controller.rb
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
3 changes: 3 additions & 0 deletions app/models/discourse_plugin_statistics_discourse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true
class DiscoursePluginStatisticsDiscourse < ActiveRecord::Base
end
4 changes: 4 additions & 0 deletions app/models/discourse_plugin_statistics_plugin.rb
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
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PluginManager::Engine < ::Rails::Engine
put 'plugin/:plugin_name' => 'plugin#save', constraints: StaffConstraint.new
delete 'plugin/:plugin_name' => 'plugin#delete', constraints: StaffConstraint.new
post 'user/register' => 'plugin_user#register', constraints: { format: 'json' }
post 'statistics' => 'statistics#create', constraints: { format: 'json' }
end

Discourse::Application.routes.prepend do
Expand Down
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
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
6 changes: 3 additions & 3 deletions lib/plugin_manager/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,15 @@ def self.build_local_paths(root_path)
end

def self.get_local_sha(path)
PluginManager.run_shell_cmd('git rev-parse HEAD', chdir: path)
PluginManager.run_shell_cmd('git rev-parse HEAD', { chdir: path })
end

def self.get_local_branch(path)
PluginManager.run_shell_cmd('git rev-parse --abbrev-ref HEAD', chdir: path)
PluginManager.run_shell_cmd('git rev-parse --abbrev-ref HEAD', { chdir: path })
end

def self.get_local_url(path)
PluginManager.run_shell_cmd('git config --get remote.origin.url', chdir: path)
PluginManager.run_shell_cmd('git config --get remote.origin.url', { chdir: path })
end

def self.excluded_local_plugins
Expand Down
3 changes: 3 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
../app/jobs/scheduled/update_plugin_test_statuses.rb
../app/jobs/scheduled/update_plugins.rb
../app/jobs/regular/send_plugin_notification.rb
../app/models/discourse_plugin_statistics_discourse.rb
../app/models/discourse_plugin_statistics_plugin.rb
../app/controllers/plugin_manager/plugin_controller.rb
../app/controllers/plugin_manager/plugin_status_controller.rb
../app/controllers/plugin_manager/plugin_user_controller.rb
../app/controllers/plugin_manager/statistics_controller.rb
../app/serializers/plugin_manager/log_serializer.rb
../app/serializers/plugin_manager/plugin_serializer.rb
../app/serializers/plugin_manager/plugin_user_serializer.rb
Expand Down
7 changes: 7 additions & 0 deletions spec/fabricators/discourse_plugin_statistics_discourse.rb
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
7 changes: 7 additions & 0 deletions spec/fabricators/discourse_plugin_statistics_plugin.rb
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
6 changes: 3 additions & 3 deletions spec/plugin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def api_token
end

def stub_plugin_git_cmds(dir, plugin_url)
Open3.expects(:capture3).with("git rev-parse HEAD", { chdir: dir }).returns(plugin_sha).at_least_once
Open3.expects(:capture3).with("git rev-parse --abbrev-ref HEAD", { chdir: dir }).returns(plugin_branch).at_least_once
Open3.expects(:capture3).with("git config --get remote.origin.url", { chdir: dir }).returns(plugin_url || "https://github.com/paviliondev/discourse-compatible-plugin.git")
PluginManager.expects(:run_shell_cmd).with("git rev-parse HEAD", { chdir: dir }).returns(plugin_sha).at_least_once
PluginManager.expects(:run_shell_cmd).with("git rev-parse --abbrev-ref HEAD", { chdir: dir }).returns(plugin_branch).at_least_once
PluginManager.expects(:run_shell_cmd).with("git config --get remote.origin.url", { chdir: dir }).returns(plugin_url || "https://github.com/paviliondev/discourse-compatible-plugin.git")
Discourse.expects(:git_branch).returns(discourse_branch).at_least_once
Discourse.expects(:git_version).returns(discourse_sha).at_least_once
end
Expand Down
120 changes: 120 additions & 0 deletions spec/requests/plugin_manager/statistics_controller_spec.rb
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

0 comments on commit df828f6

Please sign in to comment.