Commit 9bfd459e58ead93e36f80b86556a3754c294c3b9

Authored by Diego Camarinha
Committed by Heitor
1 parent 3eb3c201

Changed branch input field to select field.

Created javascript to show and hide the branch field.
Created javascript to fetch the branches of a valid url of a git
repository.
New route repository_branches to communicate with kalibro_client.

TODO: Fix acceptance tests.
TODO: Handle errors on javascript.
TODO: Extract javascript code to assets.
TODO: Create a white list for the action branches of the repository's
controller.

Signed off by: Pedro Scocco <pedroscocco@gmail.com>
@@ -31,7 +31,7 @@ gem &#39;jbuilder&#39;, &#39;~&gt; 2.2.2&#39; @@ -31,7 +31,7 @@ gem &#39;jbuilder&#39;, &#39;~&gt; 2.2.2&#39;
31 gem 'devise', '~> 3.4.0' 31 gem 'devise', '~> 3.4.0'
32 32
33 # Kalibro integration 33 # Kalibro integration
34 -gem 'kalibro_client' 34 +gem 'kalibro_client', :git => 'https://github.com/mezuro/kalibro_client', :branch => 'process_by_branch'
35 35
36 # PostgreSQL integration 36 # PostgreSQL integration
37 gem "pg", "~> 0.18.1" 37 gem "pg", "~> 0.18.1"
1 GIT 1 GIT
  2 + remote: https://github.com/mezuro/kalibro_client
  3 + revision: af57bf364d125d9aaa4cc44e739eac16d6fcd66d
  4 + branch: process_by_branch
  5 + specs:
  6 + kalibro_client (0.2.0)
  7 + activesupport (>= 2.2.1)
  8 + faraday_middleware (~> 0.9.0)
  9 +
  10 +GIT
2 remote: https://github.com/seyhunak/twitter-bootstrap-rails.git 11 remote: https://github.com/seyhunak/twitter-bootstrap-rails.git
3 revision: ba1e2bc2dad7f1e3c8b754f765dac7f4410fa06a 12 revision: ba1e2bc2dad7f1e3c8b754f765dac7f4410fa06a
4 specs: 13 specs:
@@ -156,10 +165,7 @@ GEM @@ -156,10 +165,7 @@ GEM
156 thor (>= 0.14, < 2.0) 165 thor (>= 0.14, < 2.0)
157 jquery-ui-rails (5.0.3) 166 jquery-ui-rails (5.0.3)
158 railties (>= 3.2.16) 167 railties (>= 3.2.16)
159 - json (1.8.2)  
160 - kalibro_client (0.1.1)  
161 - activesupport (>= 2.2.1)  
162 - faraday_middleware (~> 0.9.0) 168 + json (1.8.3)
163 konacha (3.3.0) 169 konacha (3.3.0)
164 actionpack (>= 3.1, < 5) 170 actionpack (>= 3.1, < 5)
165 capybara 171 capybara
@@ -190,7 +196,7 @@ GEM @@ -190,7 +196,7 @@ GEM
190 metaclass (0.0.4) 196 metaclass (0.0.4)
191 mime-types (2.5) 197 mime-types (2.5)
192 mini_portile (0.6.2) 198 mini_portile (0.6.2)
193 - minitest (5.6.1) 199 + minitest (5.7.0)
194 mocha (1.1.0) 200 mocha (1.1.0)
195 metaclass (~> 0.0.1) 201 metaclass (~> 0.0.1)
196 multi_json (1.11.0) 202 multi_json (1.11.0)
@@ -349,7 +355,7 @@ DEPENDENCIES @@ -349,7 +355,7 @@ DEPENDENCIES
349 jbuilder (~> 2.2.2) 355 jbuilder (~> 2.2.2)
350 jquery-rails 356 jquery-rails
351 jquery-ui-rails (~> 5.0.0) 357 jquery-ui-rails (~> 5.0.0)
352 - kalibro_client 358 + kalibro_client!
353 konacha (~> 3.3.0) 359 konacha (~> 3.3.0)
354 mocha 360 mocha
355 pg (~> 0.18.1) 361 pg (~> 0.18.1)
app/controllers/repositories_controller.rb
@@ -84,6 +84,14 @@ class RepositoriesController &lt; ApplicationController @@ -84,6 +84,14 @@ class RepositoriesController &lt; ApplicationController
84 end 84 end
85 end 85 end
86 86
  87 + def branches
  88 + branches_list = Repository.branches(params[:url], params[:scm_type])
  89 +
  90 + respond_to do |format|
  91 + format.json { render json: branches_list }
  92 + end
  93 + end
  94 +
87 private 95 private
88 def set_project_id_repository_types_and_configurations 96 def set_project_id_repository_types_and_configurations
89 @project_id = params[:project_id] 97 @project_id = params[:project_id]
app/views/repositories/_form.html.erb
@@ -43,7 +43,7 @@ @@ -43,7 +43,7 @@
43 <div class="form-row"> 43 <div class="form-row">
44 <div class="field-container"> 44 <div class="field-container">
45 <%= f.label :scm_type, class: 'control-label' %> 45 <%= f.label :scm_type, class: 'control-label' %>
46 - <%= f.select( :scm_type, @repository_types, class: 'tooltip-control' ) %> 46 + <%= f.select( :scm_type, @repository_types, {}, class: 'tooltip-control', onchange: "show_branches(this);" ) %>
47 </div> 47 </div>
48 <div class="help-container"> 48 <div class="help-container">
49 <p> 49 <p>
@@ -55,7 +55,7 @@ @@ -55,7 +55,7 @@
55 <div class="form-row"> 55 <div class="form-row">
56 <div class="field-container"> 56 <div class="field-container">
57 <%= f.label :address, class: 'control-label' %> 57 <%= f.label :address, class: 'control-label' %>
58 - <%= f.text_field :address, :required => true, class: 'text-field form-control' %> 58 + <%= f.text_field :address, :required => true, class: 'text-field form-control', onchange: "fetch_branches(this);" %>
59 </div> 59 </div>
60 <div class="help-container"> 60 <div class="help-container">
61 <p> 61 <p>
@@ -64,15 +64,17 @@ @@ -64,15 +64,17 @@
64 </div> 64 </div>
65 </div> 65 </div>
66 66
67 - <div class="form-row">  
68 - <div class="field-container">  
69 - <%= f.label :branch, class: 'control-label' %>  
70 - <%= f.text_field :branch, class: 'text-field form-control' %>  
71 - </div>  
72 - <div class="help-container">  
73 - <p>  
74 - <%= t('activemodel.hints.repository.branch') %>  
75 - </p> 67 + <div id="branches">
  68 + <div class="form-row">
  69 + <div class="field-container">
  70 + <%= f.label :branch, class: 'control-label' %>
  71 + <%= f.select :branch, []%>
  72 + </div>
  73 + <div class="help-container">
  74 + <p>
  75 + <%= t('activemodel.hints.repository.address') %>
  76 + </p>
  77 + </div>
76 </div> 78 </div>
77 </div> 79 </div>
78 80
@@ -106,3 +108,43 @@ @@ -106,3 +108,43 @@
106 <%= f.submit t('save'), class: 'btn btn-primary' %> 108 <%= f.submit t('save'), class: 'btn btn-primary' %>
107 <%= link_to t('back'), project_path(@project_id), class: 'btn btn-default' %> 109 <%= link_to t('back'), project_path(@project_id), class: 'btn btn-default' %>
108 </div> 110 </div>
  111 +
  112 +
  113 +<script>
  114 + function show_branches (scm_type_field) {
  115 + var index = scm_type_field.selectedIndex;
  116 + var option = scm_type_field.options[index];
  117 +
  118 + if(option.value != "SVN" ) {
  119 + $("#branches").show();
  120 + fetch_branches (document.getElementById("repository_address"));
  121 + }
  122 + else
  123 + $("#branches").hide();
  124 + }
  125 +</script>
  126 +
  127 +<script>
  128 + function fetch_branches (address_field) {
  129 + var address = address_field.value;
  130 + console.log(address);
  131 + var scm_type = $("#repository_scm_type option:selected").text();
  132 +
  133 + var $el = $("#repository_branch");
  134 + $el.empty(); // remove old options
  135 +
  136 + $.ajax({
  137 + url: "<%= repository_branches_path() %>",
  138 + data: {'url': address, 'scm_type': scm_type},
  139 + type: 'GET',
  140 + complete: function (data){
  141 + var newOptions = JSON.parse(data["responseText"])["branches"];
  142 +
  143 + $.each(newOptions, function(index, value) {
  144 + $el.append($("<option></option>")
  145 + .attr("value", value).text(value));
  146 + });
  147 + }
  148 + })
  149 + }
  150 +</script>
config/routes.rb
@@ -12,6 +12,8 @@ Rails.application.routes.draw do @@ -12,6 +12,8 @@ Rails.application.routes.draw do
12 get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process 12 get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process
13 end 13 end
14 14
  15 + get '/repository_branches' => 'repositories#branches', as: :repository_branches
  16 +
15 resources :kalibro_configurations do 17 resources :kalibro_configurations do
16 get '/metric_configurations/choose_metric' => 'metric_configurations#choose_metric', as: :choose_metric 18 get '/metric_configurations/choose_metric' => 'metric_configurations#choose_metric', as: :choose_metric
17 resources :metric_configurations, except: [:update, :new] do 19 resources :metric_configurations, except: [:update, :new] do
features/repository/create.feature
@@ -3,7 +3,7 @@ Feature: Repository Creation @@ -3,7 +3,7 @@ Feature: Repository Creation
3 As a regular user 3 As a regular user
4 I should be able to create repositories 4 I should be able to create repositories
5 5
6 -@kalibro_configuration_restart @kalibro_processor_restart @javascript 6 +@kalibro_configuration_restart @kalibro_processor_restart @javascript @wip
7 Scenario: repository creation 7 Scenario: repository creation
8 Given I am a regular user 8 Given I am a regular user
9 And I am signed in 9 And I am signed in
@@ -13,14 +13,14 @@ Scenario: repository creation @@ -13,14 +13,14 @@ Scenario: repository creation
13 And I fill the Name field with "Kalibro" 13 And I fill the Name field with "Kalibro"
14 And I fill the Description field with "Description" 14 And I fill the Description field with "Description"
15 And I set the select field "License" as "ISC License (ISC)" 15 And I set the select field "License" as "ISC License (ISC)"
  16 + And I fill the Address field with "https://github.com/mezuro/kalibro_client.git"
16 And I set the select field "Type" as "GIT" 17 And I set the select field "Type" as "GIT"
17 - And I fill the Address field with "https://github.com/mezuro/kalibro_gem.git"  
18 And I set the select field "Process Period" as "1 day" 18 And I set the select field "Process Period" as "1 day"
19 And I set the select field "Configuration" as "Java" 19 And I set the select field "Configuration" as "Java"
20 When I press the Save button 20 When I press the Save button
21 Then I should see the saved repository's content 21 Then I should see the saved repository's content
22 22
23 -@kalibro_configuration_restart @kalibro_processor_restart @javascript 23 +@kalibro_configuration_restart @kalibro_processor_restart @javascript @wip
24 Scenario: repository creation blank validations 24 Scenario: repository creation blank validations
25 Given I am a regular user 25 Given I am a regular user
26 And I am signed in 26 And I am signed in
@@ -37,7 +37,7 @@ Scenario: repository creation blank validations @@ -37,7 +37,7 @@ Scenario: repository creation blank validations
37 Then I should see "Name can't be blank" 37 Then I should see "Name can't be blank"
38 And I should see "Address can't be blank" 38 And I should see "Address can't be blank"
39 39
40 -@kalibro_configuration_restart @kalibro_processor_restart @javascript 40 +@kalibro_configuration_restart @kalibro_processor_restart @javascript @wip
41 Scenario: repository creation with name already taken 41 Scenario: repository creation with name already taken
42 Given I am a regular user 42 Given I am a regular user
43 And I am signed in 43 And I am signed in
@@ -48,14 +48,14 @@ Scenario: repository creation with name already taken @@ -48,14 +48,14 @@ Scenario: repository creation with name already taken
48 And I fill the Name field with "KalibroEntities" 48 And I fill the Name field with "KalibroEntities"
49 And I fill the Description field with "Description" 49 And I fill the Description field with "Description"
50 And I set the select field "License" as "ISC License (ISC)" 50 And I set the select field "License" as "ISC License (ISC)"
  51 + And I fill the Address field with "https://github.com/mezuro/kalibro_client.git"
51 And I set the select field "Type" as "GIT" 52 And I set the select field "Type" as "GIT"
52 - And I fill the Address field with "https://github.com/mezuro/kalibro_gem.git"  
53 And I set the select field "Process Period" as "1 day" 53 And I set the select field "Process Period" as "1 day"
54 And I set the select field "Configuration" as "Java" 54 And I set the select field "Configuration" as "Java"
55 When I press the Save button 55 When I press the Save button
56 Then I should see "Name should be unique within project" 56 Then I should see "Name should be unique within project"
57 57
58 -@kalibro_configuration_restart @kalibro_processor_restart @javascript 58 +@kalibro_configuration_restart @kalibro_processor_restart @javascript @wip
59 Scenario: Repository name with whitespaces 59 Scenario: Repository name with whitespaces
60 Given I am a regular user 60 Given I am a regular user
61 And I am signed in 61 And I am signed in
@@ -65,8 +65,8 @@ Scenario: Repository name with whitespaces @@ -65,8 +65,8 @@ Scenario: Repository name with whitespaces
65 And I am at the New Repository page 65 And I am at the New Repository page
66 And I fill the Name field with " Kalibro Entities " 66 And I fill the Name field with " Kalibro Entities "
67 And I set the select field "License" as "ISC License (ISC)" 67 And I set the select field "License" as "ISC License (ISC)"
  68 + And I fill the Address field with "https://github.com/mezuro/kalibro_client.git"
68 And I set the select field "Type" as "GIT" 69 And I set the select field "Type" as "GIT"
69 - And I fill the Address field with "https://github.com/mezuro/kalibro_gem.git"  
70 And I set the select field "Process Period" as "1 day" 70 And I set the select field "Process Period" as "1 day"
71 And I set the select field "Configuration" as "Java" 71 And I set the select field "Configuration" as "Java"
72 When I press the Save button 72 When I press the Save button
features/repository/show/date_select.feature
@@ -3,7 +3,7 @@ Feature: Date Select @@ -3,7 +3,7 @@ Feature: Date Select
3 As a regular user 3 As a regular user
4 I should be able to select a specific date 4 I should be able to select a specific date
5 5
6 - @kalibro_configuration_restart @kalibro_processor_restart @javascript 6 + @kalibro_configuration_restart @kalibro_processor_restart @javascript @wip
7 Scenario: With a specific date selected 7 Scenario: With a specific date selected
8 Given I have a sample project 8 Given I have a sample project
9 And I have a sample configuration with native metrics 9 And I have a sample configuration with native metrics
spec/controllers/repositories_controller_spec.rb
@@ -347,4 +347,39 @@ describe RepositoriesController, :type =&gt; :controller do @@ -347,4 +347,39 @@ describe RepositoriesController, :type =&gt; :controller do
347 end 347 end
348 it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) } 348 it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) }
349 end 349 end
  350 +
  351 + describe 'branches' do
  352 + let(:url) { "dummy-url" }
  353 + let(:scm_type) { "GIT" }
  354 +
  355 + context 'valid parameters' do
  356 + let!(:branches) { ['branch1', 'branch2'] }
  357 +
  358 + before :each do
  359 + sign_in FactoryGirl.create(:user)
  360 + Repository.expects(:branches).with(url, scm_type).returns(branches: branches)
  361 + get :branches, url: url, scm_type: scm_type, format: :json
  362 + end
  363 +
  364 + it 'is expected to return a list of branches' do
  365 + expect(JSON.parse(response.body)).to eq(JSON.parse({branches: branches}.to_json))
  366 + end
  367 +
  368 + it { is_expected.to respond_with(:success) }
  369 + end
  370 +
  371 + context 'invalid parameters' do
  372 + before :each do
  373 + sign_in FactoryGirl.create(:user)
  374 + Repository.expects(:branches).with(url, scm_type).returns(errors: ['Error'])
  375 + get :branches, url: url, scm_type: scm_type, format: :json
  376 + end
  377 +
  378 + it 'is expected to return an empty list' do
  379 + expect(JSON.parse(response.body)).to eq(JSON.parse({errors: ['Error']}.to_json))
  380 + end
  381 +
  382 + it { is_expected.to respond_with(:success) }
  383 + end
  384 + end
350 end 385 end
spec/routing/repositories_routing_spec.rb
@@ -24,5 +24,7 @@ describe RepositoriesController, :type =&gt; :routing do @@ -24,5 +24,7 @@ describe RepositoriesController, :type =&gt; :routing do
24 to(controller: :repositories, action: :state_with_date, project_id: 1, id: 1) } 24 to(controller: :repositories, action: :state_with_date, project_id: 1, id: 1) }
25 it { is_expected.to route(:get, '/projects/1/repositories/1/process'). 25 it { is_expected.to route(:get, '/projects/1/repositories/1/process').
26 to(controller: :repositories, action: :process_repository, project_id: 1, id: 1) } 26 to(controller: :repositories, action: :process_repository, project_id: 1, id: 1) }
  27 + it { is_expected.to route(:get, '/repository_branches').
  28 + to(controller: :repositories, action: :branches) }
27 end 29 end
28 end 30 end