Skip to content

Commit

Permalink
Merge pull request joshsoftware#221 from BandanaPandey/royalty_points…
Browse files Browse the repository at this point in the history
…_validations

Added IgnoredFile UI.
  • Loading branch information
sethu authored Dec 13, 2016
2 parents c87ccbc + ca0350d commit b00979f
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 14 deletions.
15 changes: 15 additions & 0 deletions app/assets/javascripts/admin/ignored_files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$(document).on('page:change', function(event) {

$(function() {
$('#file').bootstrapToggle();
})

$('#file').change(function() {
query = $('#q').val();
$.ajax({
type: 'get',
url: '/admin/ignored_files',
data: {'ignored': !($(this).is(':checked')), query: query}
})
})
});
4 changes: 4 additions & 0 deletions app/assets/stylesheets/admin/ignored_files.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.slide{
padding-left: 5px;
float: right;
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@import "bootstrap";
@import "font-awesome";
@import "admin/redeem_requests";
@import "admin/ignored_files";
@import "style";
@import "home";
@import "bars-pill";
Expand Down
67 changes: 67 additions & 0 deletions app/controllers/admin/ignored_files_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Admin::IgnoredFilesController < ApplicationController
include IgnoredFileHelper

before_action :authenticate_user!
before_action :authenticate_admin!
before_action :find_ignored_file, except: [:index, :new, :create]

def index
@status = params[:ignored] ? params[:ignored] : false

@ignored_files = FileToBeIgnored.where(:ignored => @status, name: /#{params[:query]}/).page(params[:page])

if request.xhr?
respond_to do|format|
format.js
end
end
end

def new
@ignored_file = FileToBeIgnored.new
end

def create
@ignored_file = FileToBeIgnored.new(files_params)
if @ignored_file.save
redirect_to admin_ignored_files_path
else
render :new
end
end

def edit
end

def update
if @ignored_file.update_attributes(files_params)
redirect_to admin_ignored_files_path
else
render :edit
end
end

def search
if params[:q].blank?
redirect_to admin_ignored_files_path
return
end

@ignored_files = FileToBeIgnored.where(name: /#{params[:q]}/).order(name: :asc)
@ignored_files = @ignored_files.page(1)
render :index
end

def destroy
@ignored_file.destroy
flash[:success] = "Deleted Successfully"
redirect_to admin_ignored_files_path
end

private

def files_params
params.fetch(:file_to_be_ignored).permit(:name, :programming_language, :ignored)
end

end
7 changes: 7 additions & 0 deletions app/controllers/concerns/ignored_file_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module IgnoredFileHelper

def find_ignored_file
@ignored_file = FileToBeIgnored.where(id: params[:id]).first
end

end
6 changes: 4 additions & 2 deletions app/models/file_to_be_ignored.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ class FileToBeIgnored

field :name, type: String
field :programming_language, type: String, default: "ruby"
field :ignored, type: Boolean, default: false
field :count, type: Integer, default: 0

validates :name, presence: true
validates :name, :programming_language, presence: true

def self.name_exist?(file_path)
file_name = File.basename(file_path)

return false if file_name.blank?

where(name: /(#{Regexp.escape(file_name)})$/).any?
where(name: /(#{Regexp.escape(file_name)})$/, ignored: true).any?
end

end
6 changes: 4 additions & 2 deletions app/models/scoring_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def bugspots_score(commit)
bugspots = Bugspots.scan(repo_dir, branch).last

#bugspot scoring of such files which are in the Ignored_list should be zero.

bugspots_scores = bugspots.inject({}) do |r, s|
if (FileToBeIgnored.name_exist?(s.file))
s.score = "0.0000"
Expand Down Expand Up @@ -94,7 +93,10 @@ def bugspots_score(commit)

file_score = bugspots_scores[file.filename]

unless FileToBeIgnored.name_exist?(file.filename)
if FileToBeIgnored.name_exist?(file.filename)
ignored_file = FileToBeIgnored.where(name: file.filename).first
ignored_file.inc(count: 1)
else
total_changes += file.changes
files_count += 1
end
Expand Down
6 changes: 6 additions & 0 deletions app/views/admin/ignored_files/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= simple_form_for @ignored_file, url: path do |f|
.col-xs-6
= f.input :name
= f.input :programming_language
= f.input :ignored, as: :select, collection: { 'Yes' => 'true', 'No' => 'false'}, include_blank: false
%button.btn.btn-primary{type: 'submit'}= @ignored_file.persisted? ? 'Update' : 'Create'
8 changes: 8 additions & 0 deletions app/views/admin/ignored_files/_ignored_file.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%tr
%td= ignored_file.name
%td= (ignored_file.programming_language).titleize
%td= ignored_file.count
%td= ignored_file.ignored ? 'Yes' : 'No'
%td
= link_to 'Edit', edit_admin_ignored_file_path(ignored_file), class: 'btn btn-xs btn-primary'
= link_to 'Delete', admin_ignored_file_path(ignored_file), class: 'btn btn-xs btn-danger', data: { method: 'delete', confirm: 'Are you sure?' }
9 changes: 9 additions & 0 deletions app/views/admin/ignored_files/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%section.content-header
%h1
Edit File
%section.content
.row
.col-xs-12
.box.box-primary
.box-body
= render partial: 'form', locals: { path: admin_ignored_file_path(@ignored_file) }
34 changes: 34 additions & 0 deletions app/views/admin/ignored_files/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
%section.content-header
%h1
Files
%label.slide
%input.check#file{checked: 'checked', name: 'ignored', type: 'checkbox', data: {toggle: 'toggle', width: '80', height: '34', on: 'Scored', off: 'Ignored'}}
= link_to new_admin_ignored_file_path, class: 'btn btn-success pull-right'do
%b Add File
%section.content
.row
.col-xs-12
.box.box-primary
.box-header
%h3.box-title
%span Files For Scoring:
.box-tools
= form_tag(search_admin_ignored_files_path, method: :get, class: 'col-xs-4 pull-right') do
.input-group.input-group-sm
= text_field_tag('q', params[:q], class: 'form-control pull-right', placeholder: 'filename/filepath')
.input-group-btn
%button.btn.btn-default{type: 'submit'} Go
.box-body.table-responsive
- if @ignored_files.any?
%table.table.table-bordered
%thead
%tr
%th.col-xs-5 Name/Path
%th.col-xs-3 Programming Language
%th.col-xs-1 Count
%th.col-xs-1 Ignored
%th.col-xs-2 Actions
%tbody#ignored_file
= render partial: 'ignored_file', collection: @ignored_files
.pagination-container
= paginate @ignored_files, remote: true
2 changes: 2 additions & 0 deletions app/views/admin/ignored_files/index.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$('#ignored_file').html("<%=j(render(partial: 'ignored_file', collection: @ignored_files)) %>");
$(".pagination-container").html("<%=j(paginate @ignored_files, remote: true)%>")
9 changes: 9 additions & 0 deletions app/views/admin/ignored_files/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%section.content-header
%h1
Add File
%section.content
.row
.col-xs-12
.box.box-primary
.box-body
= render partial: 'form', locals: { path: admin_ignored_files_path }
4 changes: 4 additions & 0 deletions app/views/application/_admin_sidebar.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
%a{href: admin_judges_path}
%i.fa.fa-circle-o
%span Judges
%li
%a{href: admin_ignored_files_path}
%i.fa.fa-circle-o
%span Ignored Files
%li
%a{href: admin_redeem_requests_path}
%i.fa.fa-credit-card-alt
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@
resources :rounds, only: [:index] do
get :mark_as_close
end

resources :redeem_requests, only: [:index, :update, :destroy]

resources :ignored_files, except: [:show] do
get :search, on: :collection
end

get 'dashboard/index', as: 'dashboard'
end

Expand Down
9 changes: 9 additions & 0 deletions test/controllers/admin/ignored_files_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "test_helper"

class Admin::IgnoredFilesControllerTest < ActionController::TestCase
def test_index
skip "need test_cases"
get :index
assert_response :success
end
end
12 changes: 6 additions & 6 deletions test/models/file_to_be_ignored_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ def setup
end

test 'file name is present in the ignore list' do
file_1 = create :file_to_be_ignored, name: "Gemfile", programming_language: "ruby"
file_2 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby"
file_1 = create :file_to_be_ignored, name: "Gemfile", programming_language: "ruby", ignored: true
file_2 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby", ignored: true
file_3 = create :file_to_be_ignored, name: "config/application.rb", programming_language: "ruby"
file_4 = create :file_to_be_ignored, name: "app/models/application.rb", programming_language: "ruby"
assert FileToBeIgnored.name_exist?("Gemfile")
assert FileToBeIgnored.name_exist?("Gemfile.lock")
assert FileToBeIgnored.name_exist?("application.rb")
assert_not FileToBeIgnored.name_exist?("application.rb")
end

test 'file name is absent in the ignore list' do
file_1 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby"
file_1 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby", ignored: true
assert_not FileToBeIgnored.name_exist?("Gemfile")
assert_not FileToBeIgnored.name_exist?("application.rb")
end

test 'file path is present in the ignore list' do
file_1 = create :file_to_be_ignored, name: "config/application.rb", programming_language: "ruby"
file_1 = create :file_to_be_ignored, name: "config/application.rb", programming_language: "ruby", ignored: true
file_2 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby"
assert FileToBeIgnored.name_exist?("config/application.rb")
end

test 'file path is absent in the ignore list' do
file_1 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby"
file_1 = create :file_to_be_ignored, name: "Gemfile.lock", programming_language: "ruby", ignored: true
assert_not FileToBeIgnored.name_exist?("config/application.rb")
end

Expand Down
10 changes: 6 additions & 4 deletions test/models/scoring_engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ def stub_get(path, endpoint = Github.endpoint.to_s)
test 'check commit scoring should not be done for ignored_files' do
stub_commit_for_bugspots_scoring
@engine = ScoringEngine.new(@repo)
file_to_be_ignored = create :file_to_be_ignored, name: "Gemfile.lock"
file_to_be_ignored = create :file_to_be_ignored, name: "Gemfile.lock", ignored: true
@engine.bugspots_score(@commit)
assert_equal 1, file_to_be_ignored.reload.count
assert_equal 103, @commit.info.stats.total
assert_equal 2.25, @engine.commit_score(@commit)
end
Expand All @@ -102,14 +103,15 @@ def stub_get(path, endpoint = Github.endpoint.to_s)
test 'check bugspots scoring when files are excluded' do
stub_commit_for_bugspots_scoring
@engine = ScoringEngine.new(@repo)
file_to_be_ignored = create :file_to_be_ignored, name: "Gemfile.lock"
assert_equal (0.126), @engine.bugspots_score(@commit).round(3)
file_to_be_ignored = create :file_to_be_ignored, name: "Gemfile.lock", ignored: true
assert_equal (0.126).round(2), @engine.bugspots_score(@commit).round(2)
assert_equal 1, file_to_be_ignored.reload.count
end

test 'check bugspots should not do scoring of files such as Gemfile.lock or README' do
stub_commit_for_bugspots_scoring
@engine = ScoringEngine.new(@repo)
file_to_be_ignored = create :file_to_be_ignored, name: "Gemfile.lock"
file_to_be_ignored = create :file_to_be_ignored, name: "Gemfile.lock", ignored: true

Bugspots.stubs(:scan).returns(YAML.load(File.read("test/fixtures/bugspot.yml")))
repo_dir = Rails.root.join("repositories", @repo.id.to_s).to_s
Expand Down

0 comments on commit b00979f

Please sign in to comment.