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>
Gemfile
... ... @@ -31,7 +31,7 @@ gem &#39;jbuilder&#39;, &#39;~&gt; 2.2.2&#39;
31 31 gem 'devise', '~> 3.4.0'
32 32  
33 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 36 # PostgreSQL integration
37 37 gem "pg", "~> 0.18.1"
... ...
Gemfile.lock
1 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 11 remote: https://github.com/seyhunak/twitter-bootstrap-rails.git
3 12 revision: ba1e2bc2dad7f1e3c8b754f765dac7f4410fa06a
4 13 specs:
... ... @@ -156,10 +165,7 @@ GEM
156 165 thor (>= 0.14, < 2.0)
157 166 jquery-ui-rails (5.0.3)
158 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 169 konacha (3.3.0)
164 170 actionpack (>= 3.1, < 5)
165 171 capybara
... ... @@ -190,7 +196,7 @@ GEM
190 196 metaclass (0.0.4)
191 197 mime-types (2.5)
192 198 mini_portile (0.6.2)
193   - minitest (5.6.1)
  199 + minitest (5.7.0)
194 200 mocha (1.1.0)
195 201 metaclass (~> 0.0.1)
196 202 multi_json (1.11.0)
... ... @@ -349,7 +355,7 @@ DEPENDENCIES
349 355 jbuilder (~> 2.2.2)
350 356 jquery-rails
351 357 jquery-ui-rails (~> 5.0.0)
352   - kalibro_client
  358 + kalibro_client!
353 359 konacha (~> 3.3.0)
354 360 mocha
355 361 pg (~> 0.18.1)
... ...
app/controllers/repositories_controller.rb
... ... @@ -84,6 +84,14 @@ class RepositoriesController &lt; ApplicationController
84 84 end
85 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 95 private
88 96 def set_project_id_repository_types_and_configurations
89 97 @project_id = params[:project_id]
... ...
app/views/repositories/_form.html.erb
... ... @@ -43,7 +43,7 @@
43 43 <div class="form-row">
44 44 <div class="field-container">
45 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 47 </div>
48 48 <div class="help-container">
49 49 <p>
... ... @@ -55,7 +55,7 @@
55 55 <div class="form-row">
56 56 <div class="field-container">
57 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 59 </div>
60 60 <div class="help-container">
61 61 <p>
... ... @@ -64,15 +64,17 @@
64 64 </div>
65 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 78 </div>
77 79 </div>
78 80  
... ... @@ -106,3 +108,43 @@
106 108 <%= f.submit t('save'), class: 'btn btn-primary' %>
107 109 <%= link_to t('back'), project_path(@project_id), class: 'btn btn-default' %>
108 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 12 get '/repositories/:id/process' => 'repositories#process_repository', as: :repository_process
13 13 end
14 14  
  15 + get '/repository_branches' => 'repositories#branches', as: :repository_branches
  16 +
15 17 resources :kalibro_configurations do
16 18 get '/metric_configurations/choose_metric' => 'metric_configurations#choose_metric', as: :choose_metric
17 19 resources :metric_configurations, except: [:update, :new] do
... ...
features/repository/create.feature
... ... @@ -3,7 +3,7 @@ Feature: Repository Creation
3 3 As a regular user
4 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 7 Scenario: repository creation
8 8 Given I am a regular user
9 9 And I am signed in
... ... @@ -13,14 +13,14 @@ Scenario: repository creation
13 13 And I fill the Name field with "Kalibro"
14 14 And I fill the Description field with "Description"
15 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 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 18 And I set the select field "Process Period" as "1 day"
19 19 And I set the select field "Configuration" as "Java"
20 20 When I press the Save button
21 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 24 Scenario: repository creation blank validations
25 25 Given I am a regular user
26 26 And I am signed in
... ... @@ -37,7 +37,7 @@ Scenario: repository creation blank validations
37 37 Then I should see "Name can't be blank"
38 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 41 Scenario: repository creation with name already taken
42 42 Given I am a regular user
43 43 And I am signed in
... ... @@ -48,14 +48,14 @@ Scenario: repository creation with name already taken
48 48 And I fill the Name field with "KalibroEntities"
49 49 And I fill the Description field with "Description"
50 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 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 53 And I set the select field "Process Period" as "1 day"
54 54 And I set the select field "Configuration" as "Java"
55 55 When I press the Save button
56 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 59 Scenario: Repository name with whitespaces
60 60 Given I am a regular user
61 61 And I am signed in
... ... @@ -65,8 +65,8 @@ Scenario: Repository name with whitespaces
65 65 And I am at the New Repository page
66 66 And I fill the Name field with " Kalibro Entities "
67 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 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 70 And I set the select field "Process Period" as "1 day"
71 71 And I set the select field "Configuration" as "Java"
72 72 When I press the Save button
... ...
features/repository/show/date_select.feature
... ... @@ -3,7 +3,7 @@ Feature: Date Select
3 3 As a regular user
4 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 7 Scenario: With a specific date selected
8 8 Given I have a sample project
9 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 347 end
348 348 it { is_expected.to redirect_to(project_repository_path(repository.project_id, repository.id)) }
349 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 385 end
... ...
spec/routing/repositories_routing_spec.rb
... ... @@ -24,5 +24,7 @@ describe RepositoriesController, :type =&gt; :routing do
24 24 to(controller: :repositories, action: :state_with_date, project_id: 1, id: 1) }
25 25 it { is_expected.to route(:get, '/projects/1/repositories/1/process').
26 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 29 end
28 30 end
... ...