Skip to content

Commit 630dcc5

Browse files
authored
Merge pull request #49 from ProyectoIntegrador2018/develop
Develop
2 parents 2ab5111 + e2fb628 commit 630dcc5

12 files changed

+217
-134
lines changed

app/controllers/organizations_controller.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ def repos
8484
#Get repos from specific organization
8585
repos_from_org = self.get_repos_from_orgs org_name
8686

87-
pp "WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"
88-
pp repos_from_org["repos_stored"]
89-
9087
#Filter repos to send by specified date filter
9188
repos_to_send = filter_org_repos_between repos_from_org, start_date, end_date
9289

9390

9491
#Render partial and send it as a string
9592
render json: { repos: render_to_string('repositories/_repo', layout: false, locals: {org_repos_stored: repos_to_send['repos_stored'], org_repos_not_stored: repos_to_send['repos_not_stored'] }) }
93+
else
94+
respond_to do |format|
95+
format.json { render :json => { :error_message => @campaign.errors.full_messages }, :status => :unprocessable_entity }
96+
end
9697
end
9798

9899
end

app/controllers/repositories_controller.rb

+45-17
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ def create
9090
if remote_repo.organization
9191
#if the org exists on database
9292
if Organization.where(github_id: remote_repo.organization.id).any?
93-
self.organization = Organization.where(github_id: remote_repo.organization.id).first
93+
org_t = Organization.where(github_id: remote_repo.organization.id).first
94+
current_user.organizations << org_t
95+
current_user.save!
96+
@repository.organization = org_t
9497
else
9598
org = Organization.new
9699
remote_org = github.org remote_repo.organization.login
@@ -101,26 +104,32 @@ def create
101104
org.private_repos = remote_org.total_private_repos
102105
org.total_repos = org.public_repos + org.private_repos
103106
org.collaborators = remote_org.collaborators
104-
org.save
107+
org.save!
105108
current_user.organizations << org
106-
current_user.save
107-
109+
current_user.save!
110+
108111
@repository.organization = org
112+
#@repository.save!
109113
end
110114
else
111115
@repository.organization = Organization.where(name: username).first
112116
end
113117

114-
@repository.save
115-
@repository.delay.deliver(current_user.id)
118+
#@repository.delay.deliver(current_user.id)
119+
120+
121+
116122
@name_repo = @repository&.full_name || nil
117123
respond_to do |format|
118-
if @repository.save
119-
format.html { redirect_to @repository, notice: 'Repository was successfully created.' }
120-
format.json { render :show, status: :created, location: @repository }
124+
if @repository.save!
125+
#EXECUTE CUSTOM job
126+
Delayed::Job.enqueue RepoUpdater::ProcessNewReposContentJob.new(@repository.id, current_user.id)
127+
128+
format.html { redirect_to @repository, notice: 'Repository was successfully created.' }
129+
format.json { render :show, status: :created, location: @repository }
121130
else
122-
format.html { render :new }
123-
format.json { render json: @repository.errors, status: :unprocessable_entity }
131+
format.html { redirect_to repositories_path, notice:'Repository was not able to be created' }
132+
format.json { render json: @repository.errors, status: :unprocessable_entity }
124133
end
125134
end
126135
end
@@ -190,18 +199,37 @@ def update
190199
# DELETE /repositories/1
191200
# DELETE /repositories/1.json
192201
def destroy
193-
202+
194203
@repository.destroy
195204
respond_to do |format|
196205
format.html { redirect_to repositories_path, notice: 'Repository was successfully destroyed.' }
197206
format.json { head :no_content }
198207
end
199208
end
200209

201-
def profile
202-
#author.find
203-
puts("saludos Oscar---------------------------------------------------------")
204-
#params[id]
210+
def check_if_its_updating
211+
repo_id = params[:repo_id]
212+
213+
if Delayed::Job.where(delayed_reference_id: repo_id.to_i, delayed_reference_type: 'RepoUpdater::NewReposContent').any?
214+
render json:{updating: "yes"}
215+
else
216+
render json:{updating: "no"}
217+
end
218+
# respond_to do |format|
219+
# if Delayed::Job.where(delayed_reference_id: repo_id.to_i, delayed_reference_type: 'RepoUpdater::NewReposContent').any?
220+
# format.json do
221+
# render json{updating:'true'}.to_json
222+
# end
223+
# else
224+
# format.json do
225+
# render json{updating:'false'}.to_json
226+
# end
227+
# end
228+
# end
229+
end
230+
231+
232+
def profile
205233
@repository = Repository.find(params[:repository_id])
206234
@author = Author.find(params[:id])
207235

@@ -275,5 +303,5 @@ def set_initial_variables
275303

276304

277305
end
278-
306+
279307
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module RepoUpdater
2+
class ProcessNewReposContentJob < Struct.new(:repo_id, :user_id)
3+
4+
def enqueue(job)
5+
job.delayed_reference_id = repo_id
6+
job.delayed_reference_type = 'RepoUpdater::NewReposContent'
7+
job.save!
8+
end
9+
10+
#Push commits for the newly created repository
11+
def perform()
12+
user = User.find(user_id)
13+
selfRepo = Repository.find(repo_id)
14+
github = Octokit::Client.new access_token: user.oauth_token
15+
remote_repo = github.repo selfRepo.full_name
16+
if remote_repo
17+
#the request fails if the repo is empty so is request by try catch block
18+
name_repo = remote_repo.full_name
19+
begin
20+
commits = github.commits name_repo
21+
rescue
22+
commits = nil
23+
end
24+
25+
if commits
26+
new_commits = []
27+
commits.each do |c|
28+
cTemp = github.commit name_repo, c.sha
29+
commit = Commit.new
30+
#if Author not exists in that repository
31+
creator = Author.where(username: cTemp.commit.author.email.to_s)
32+
if !creator.any?
33+
author = Author.new
34+
author.username = cTemp.commit.author.email.to_s
35+
author.name = cTemp.commit.author.name.to_s
36+
author.repositories << selfRepo
37+
author.save
38+
else
39+
author = creator.first
40+
if !author.repositories.where(id: selfRepo.id).any?
41+
author.repositories << selfRepo
42+
end
43+
author.save
44+
end
45+
commit.github_sha = c.sha
46+
commit.message = cTemp.commit.message.to_s
47+
commit.additions = cTemp.stats.additions.to_i
48+
commit.deletions = cTemp.stats.deletions.to_i
49+
commit.files_changed = cTemp.files.count.to_i
50+
commit.created = cTemp.commit.author.date
51+
commit.author_username = Author.where(username: cTemp.commit.author.email.to_s).first.username
52+
commit.repository = selfRepo
53+
new_commits << commit
54+
end
55+
new_commits.each(&:save)
56+
end
57+
58+
else
59+
name_repo = "nil"
60+
end
61+
end
62+
63+
end
64+
end

app/models/repository.rb

-53
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,4 @@ class Repository < ApplicationRecord
99
includes(:commits).where("commits.created > ? AND commits.created < ?",
1010
start_date, end_date).references(:commits).uniq
1111
end)
12-
13-
def deliver(user_id)
14-
user = User.find(user_id)
15-
github = Octokit::Client.new access_token: user.oauth_token
16-
remote_repo = github.repo self.full_name
17-
if remote_repo
18-
#the request fails if the repo is empty so is request by try catch block
19-
name_repo = remote_repo.full_name
20-
begin
21-
commits = github.commits name_repo
22-
rescue
23-
commits = nil
24-
end
25-
26-
if commits
27-
new_commits = []
28-
commits.each do |c|
29-
cTemp = github.commit name_repo, c.sha
30-
commit = Commit.new
31-
#if Author not exists in that repository
32-
creator = Author.where(username: cTemp.commit.author.email.to_s)
33-
if !creator.any?
34-
author = Author.new
35-
author.username = cTemp.commit.author.email.to_s
36-
author.name = cTemp.commit.author.name.to_s
37-
author.repositories << self
38-
author.save
39-
else
40-
author = creator.first
41-
if !author.repositories.where(id: self.id).any?
42-
author.repositories << self
43-
end
44-
author.save
45-
end
46-
commit.github_sha = c.sha
47-
commit.message = cTemp.commit.message.to_s
48-
commit.additions = cTemp.stats.additions.to_i
49-
commit.deletions = cTemp.stats.deletions.to_i
50-
commit.files_changed = cTemp.files.count.to_i
51-
commit.created = cTemp.commit.author.date
52-
commit.author_username = Author.where(username: cTemp.commit.author.email.to_s).first.username
53-
commit.repository = self
54-
new_commits << commit
55-
end
56-
new_commits.each(&:save)
57-
end
58-
59-
else
60-
name_repo = "nil"
61-
end
62-
63-
end
64-
6512
end

app/views/repositories/_repo.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<em><%= repo.url %></em>
1313
</div>
1414
<div class="col-4 ">
15-
<%= link_to 'Delete', repo, class:"float-right", method: :delete, data: { confirm: 'Are you sure?' } %>
15+
<%= link_to 'Delete', repo, class:"float-right delete_button", method: :delete, data: { confirm: 'Are you sure?' } %>
1616
</div>
1717
</div>
1818
</li>
@@ -37,7 +37,7 @@
3737
<%= f.hidden_field :description, value: repo2.description %>
3838
<%= f.hidden_field :size, value: repo2.size %>
3939
<%= f.hidden_field :collaborator, value: repo2.collaborators_url %>
40-
<%= button_tag 'Add', class: 'button is-success float-right' %>
40+
<%= button_tag 'Add', class: 'button is-success float-right add_button ' %>
4141
<% end %>
4242
</div>
4343
</div>

app/views/repositories/index.html.erb

+21-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
<% end %>
5050

51-
<div id="spinner_loading_ajax" class="col-12 py-5 px-5">
51+
<div class="col-12 py-5 px-5 spinner_loading_ajax">
5252
<div class="d-flex justify-content-center">
5353
<div class="spinner-border" role="status">
5454
<span class="sr-only">Loading...</span>
@@ -77,7 +77,15 @@
7777
function show_repos(event, data){
7878
//data.repos contains partial rendered into string
7979
$("#list_repos").html(data.repos);
80+
$('.delete_button').on("click", function(){
81+
$(this).hide();
82+
$(this).parent().append("<button class='btn btn-primary float-right' type='button' disabled><span class='spinner-border spinner-border-sm' role='status' aria-hidden='true'></span><span class='sr-only'>Loading...</span> </button>");
8083

84+
});
85+
$('.add_button').on("click", function(){
86+
$(this).hide();
87+
$(this).parent().append("<button class='btn btn-primary float-right' type='button' disabled><span class='spinner-border spinner-border-sm' role='status' aria-hidden='true'></span><span class='sr-only'>Loading...</span> </button>");
88+
});
8189

8290
}
8391

@@ -86,17 +94,25 @@
8694
console.log(data);
8795
}
8896

97+
function show_general_spinner(button){
98+
99+
}
100+
89101
$(document).ready(function() {
90102

91103

92104

93-
$("#spinner_loading_ajax").hide();
105+
$(".spinner_loading_ajax").hide();
106+
94107
$(document).ajaxStart(function(){
95-
$('#list_repos').html("");
96-
$('#spinner_loading_ajax').show();
108+
if( $("#org_name option:selected").val() != ""){
109+
$('#list_repos').html("");
110+
$('.spinner_loading_ajax').show();
111+
}
112+
97113
});
98114
$(document).ajaxStop(function(){
99-
$('#spinner_loading_ajax').hide();
115+
$('.spinner_loading_ajax').hide();
100116
});
101117

102118
$('#find_form').on('ajax:success', show_repos);

app/views/repositories/profile.html.erb

+6-41
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,21 @@
1717
<div class="row">
1818
<% if @from_date && @until_date %>
1919
<div class="col-3 offset-1"><strong>Message:</strong> <%= commit.message %></div>
20-
<div class="col-2"><strong>A:</strong> <%= commit.additions %> </div>
21-
<div class="col-2"><strong>D:</strong> <%= commit.deletions %> </div>
22-
<div class="col-2"><strong>M:</strong> <%= commit.files_changed %> </div>
23-
<div class="col-2"><strong>C:</strong> <%= commit.created %> </div>
20+
<div class="col-2"><strong>Additions:</strong> <%= commit.additions %> </div>
21+
<div class="col-2"><strong>Deletions:</strong> <%= commit.deletions %> </div>
22+
<div class="col-2"><strong>Created:</strong> <%= commit.created %> </div>
2423
<% else %>
2524
<div class="col-3 offset-1"><strong>Message:</strong> <%= commit.message %></div>
26-
<div class="col-2"><strong>A:</strong> <%= commit.additions %> </div>
27-
<div class="col-2"><strong>D:</strong> <%= commit.deletions %> </div>
28-
<div class="col-2"><strong>M:</strong> <%= commit.files_changed %> </div>
29-
<div class="col-2"><strong>C:</strong> <%= commit.created %> </div>
25+
<div class="col-2"><strong>Additions:</strong> <%= commit.additions %> </div>
26+
<div class="col-2"><strong>Deletions:</strong> <%= commit.deletions %> </div>
27+
<div class="col-2"><strong>Created:</strong> <%= commit.created %> </div>
3028
<% end %>
3129
</div>
3230
</li>
3331
<%end%>
3432
</ul>
3533
</div>
3634
</div>
37-
<div class="row" style="padding: 1em 0px 0px 0px">
38-
<div class="col-12" align="right">
39-
<%= form_tag(repository_path, method: :get) do %>
40-
<div class="row">
41-
<div class="col-4 offset-2">
42-
<div class="input-group mb-3">
43-
<div class="input-group-prepend">
44-
<label class="input-group-text" for="start_date">Start date</label>
45-
</div>
46-
47-
<input class="form-control" as="date" type="date" id="from_date" name="from_date">
48-
</div>
49-
50-
</div>
51-
<div class="col-4">
52-
<div class="input-group mb-3">
53-
<div class="input-group-prepend">
54-
<label class="input-group-text" for="end_date">End date</label>
55-
</div>
56-
57-
<input class="form-control" as="date" type="date" id="until_date" name="until_date">
58-
</div>
59-
60-
</div>
61-
<div class="col-1">
62-
<input type="submit" name="commit" class="btn btn-info" value="Filter" data-disabled-with="Filter">
63-
</div>
64-
</div>
65-
</div>
66-
67-
<% end %>
68-
</div>
69-
</div>
7035
</div>
7136
</div>
7237
</div>

0 commit comments

Comments
 (0)