repositories_controller.rb
4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
include OwnershipAuthentication
class RepositoriesController < ApplicationController
before_action :authenticate_user!, except: [:show, :state, :state_with_date, :index]
before_action :project_owner?, only: [:new, :create], unless: Proc.new { params[:project_id].nil? }
before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository]
before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :state_with_date, :process_repository]
before_action :set_project_id_repository_types_and_configurations, only: [:new, :edit]
def index
@repositories = Repository.all
end
# GET /projects/1/repositories/1
# GET /projects/1/repositories/1.json
# GET /projects/1/repositories/1/modules/1
# GET /projects/1/repositories/1/modules/1.json
def show
set_kalibro_configuration
end
# GET projects/1/repositories/new
def new
@repository = Repository.new
end
# GET /repositories/1/edit
def edit; end
# POST /projects/1/repositories
# POST /projects/1/repositories.json
def create
@repository = Repository.new(repository_params)
respond_to do |format|
create_and_redir(format)
end
end
# PUT /projects/1/repositories/1
# PUT /projects/1/repositories/1.json
def update
respond_to do |format|
if @repository.update(repository_params)
format.html { redirect_to(repository_path(@repository.id), notice: t('successfully_updated', :record => t(@repository.class))) }
format.json { head :no_content }
else
failed_action(format, 'edit')
end
end
end
# DELETE /projects/1/repositories/1
# DELETE /projects/1/repositories/1.json
def destroy
@repository.destroy
respond_to do |format|
format.html { redirect_to projects_path }
format.json { head :no_content }
end
end
# POST /projects/1/repositories/1/state
def state
if params[:last_state] != 'READY'
@processing = @repository.last_processing
respond_to_processing_state
else
head :ok, :content_type => 'text/html' # Just don't do anything
end
end
# POST /projects/1/repositories/1/state_with_date
def state_with_date
year, month, day = params[:year], params[:month], params[:day]
@processing = @repository.processing_with_date("#{year}-#{month}-#{day}")
respond_to_processing_state
end
# GET /projects/1/repositories/1/process
def process_repository
@repository.process
set_kalibro_configuration
respond_to do |format|
format.html { redirect_to repository_path(@repository.id) }
end
end
def branches
branch_params = branches_params
branches_list = Repository.branches(branch_params[:url], branch_params[:scm_type])
respond_to do |format|
format.json { render json: branches_list }
end
end
private
def set_project_id_repository_types_and_configurations
@project_id = params[:project_id]
@repository_types = Repository.repository_types
@configurations = KalibroConfiguration.public_or_owned_by_user(current_user).map { |conf|
[conf.name, conf.id]
}
end
# Duplicated code on create and update actions extracted here
def failed_action(format, destiny_action)
set_project_id_repository_types_and_configurations
format.html { render action: destiny_action }
format.json { render json: @repository.errors, status: :unprocessable_entity }
end
# Use callbacks to share common setup or constraints between actions.
def set_repository
@repository = Repository.find(params[:id].to_i)
end
def set_kalibro_configuration
if @repository
@kalibro_configuration = KalibroConfiguration.find(@repository.kalibro_configuration_id)
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def repository_params
params[:repository][:name].strip!
params[:repository][:address].strip!
params[:repository][:project_id] = params[:project_id] if params.key? :project_id
params[:repository]
end
def branches_params
params.permit(:scm_type, :url)
end
# Code extracted from create action
def create_and_redir(format)
if @repository.save
current_user.repository_attributes.create(repository_id: @repository.id)
format.html { redirect_to repository_process_path(@repository.id), notice: t('successfully_created', :record => t(@repository.class)) }
else
failed_action(format, 'new')
end
end
def respond_to_processing_state
respond_to do |format|
if @processing.state == 'READY'
format.js { render action: 'load_ready_processing' }
elsif @processing.state == 'ERROR'
format.js { render action: 'load_error' }
else
format.js { render action: 'reload_processing' }
end
end
end
end