Skip to content

Commit c34bb97

Browse files
committed
Added core relational logic
1 parent 8d02753 commit c34bb97

17 files changed

+204
-143
lines changed

app/controllers/repositories_controller.rb

+125-129
Original file line numberDiff line numberDiff line change
@@ -5,121 +5,84 @@ class RepositoriesController < ApplicationController
55
# GET /repositories
66
# GET /repositories.json
77
def index
8-
@username = current_user.username
8+
@username = current_user.username
99
set_initial_variables
1010
end
1111

1212
# GET /repositories/1
1313
# GET /repositories/1.json
1414
def show
15-
github = Octokit::Client.new access_token: current_user.oauth_token
16-
@id = @repository.id
17-
repop = github.repo @repository.full_name
18-
total = 0
19-
if repop
20-
@name_repo = repop.full_name
21-
commits = github.commits @name_repo
22-
@data=Hash.new
23-
24-
commits.each do |c|
25-
cTemp = github.commit @name_repo, c.sha
26-
if @data.has_key? cTemp.commit.author.email
27-
@data[cTemp.commit.author.email]["name"] =cTemp.commit.author.name.to_s
28-
@data[cTemp.commit.author.email]["additions"] = @data[cTemp.commit.author.email]["additions"].to_i + cTemp.stats.additions.to_i
29-
@data[cTemp.commit.author.email]["deletions"] = @data[cTemp.commit.author.email]["deletions"].to_i + cTemp.stats.deletions.to_i
30-
@data[cTemp.commit.author.email]["modified"] = @data[cTemp.commit.author.email]["modified"].to_i + cTemp.stats.modifiedw.to_i
31-
else
32-
@data[cTemp.commit.author.email] = {
33-
name: cTemp.commit.author.name.to_s,
34-
additions: cTemp.stats.additions.to_i,
35-
deletions: cTemp.stats.deletions.to_i,
36-
modified: cTemp.stats.modified.to_i
37-
}
38-
end
39-
40-
end
41-
# @name_repo = repop.full_name
42-
else
43-
@name_repo = "nil"
44-
end
45-
@username = current_user.username
46-
47-
48-
@chart = LazyHighCharts::HighChart.new('pie') do |f|
49-
f.chart({:defaultSeriesType=>"pie" ,
15+
@authors = @repository.authors
16+
@username = current_user.username
17+
@chart = LazyHighCharts::HighChart.new('pie') do |f|
18+
f.chart({:defaultSeriesType=>"pie" ,
5019
:margin=> [50, 200, 60, 170]},
5120

52-
)
53-
54-
f.series(
55-
:type=> 'pie',
56-
:name=> 'percentage contribution',
57-
:data=> [
58-
['Sam', 45.0],
59-
['Pedro', 15.0],
60-
['Juan', 30.0],
61-
['Thomas', 5.0],
62-
['Jeff', 5.0]
63-
])
64-
f.legend(:layout=> 'vertical',:style=> {:left=> 'auto', :bottom=> 'auto',:right=> '50px',:top=> '100px'})
65-
f.plot_options(:pie=>{
66-
:allowPointSelect=>true,
67-
:cursor=>"pointer" ,
68-
:dataLabels=>{
21+
)
22+
23+
f.series(
24+
:type=> 'pie',
25+
:name=> 'percentage contribution',
26+
:data=> [
27+
['Sam', 45.0],
28+
['Pedro', 15.0],
29+
['Juan', 30.0],
30+
['Thomas', 5.0],
31+
['Jeff', 5.0]
32+
])
33+
f.legend(:layout=> 'vertical',:style=> {:left=> 'auto', :bottom=> 'auto',:right=> '50px',:top=> '100px'})
34+
f.plot_options(:pie=>{
35+
:allowPointSelect=>true,
36+
:cursor=>"pointer" ,
37+
:dataLabels=>{
6938
:enabled=>true,
7039
:color=>"black",
7140
:style=>{
72-
:font=>"13px Trebuchet MS, Verdana, sans-serif"
41+
:font=>"13px Trebuchet MS, Verdana, sans-serif"
7342
}
74-
}
75-
})
76-
end
77-
@data_in_series = []
78-
@data.each do |key,value|
79-
@data_in_series.push([value["name"],value["additions"] + value["modified"]])
80-
81-
end
82-
83-
84-
@chart2 = LazyHighCharts::HighChart.new('pie') do |c|
85-
c.chart(
86-
plotBackgroundColor: nil,
87-
plotBorderWidth: nil,
88-
plotShadow: false,
89-
type: 'pie'
90-
)
91-
c.title(
92-
text: 'Contributions to repo'
93-
)
94-
c.tooltip(
95-
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
96-
)
97-
#c.options[:chart][:height] = 800
98-
#c.options[:chart][:width] = 800
99-
c.plotOptions(
100-
pie: {
101-
allowPointSelect: true,
102-
cursor: 'pointer',
103-
dataLabels: {
104-
enabled: true,
105-
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
106-
style: {
107-
color: 'black'
108-
}
109-
}
110-
}
111-
)
112-
c.series(
113-
:type=> 'pie',
114-
:name=> 'percentage contribution',
115-
:data=> @data_in_series
116-
)
43+
}
44+
})
45+
end
46+
@data_in_series = []
47+
@authors.each do |author|
48+
@data_in_series.push([author.name, @repository.commits.where(author_username: author.username).sum(&:additions) + @repository.commits.where(author_username: author.username).sum(&:files_changed) ])
11749
end
118-
end
119-
120-
121-
12250

51+
@chart2 = LazyHighCharts::HighChart.new('pie') do |c|
52+
c.chart(
53+
plotBackgroundColor: nil,
54+
plotBorderWidth: nil,
55+
plotShadow: false,
56+
type: 'pie'
57+
)
58+
c.title(
59+
text: 'Contributions to repo'
60+
)
61+
c.tooltip(
62+
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
63+
)
64+
#c.options[:chart][:height] = 800
65+
#c.options[:chart][:width] = 800
66+
c.plotOptions(
67+
pie: {
68+
allowPointSelect: true,
69+
cursor: 'pointer',
70+
dataLabels: {
71+
enabled: true,
72+
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
73+
style: {
74+
color: 'black'
75+
}
76+
}
77+
}
78+
)
79+
c.series(
80+
:type=> 'pie',
81+
:name=> 'percentage contribution',
82+
:data=> @data_in_series
83+
)
84+
end
85+
end
12386

12487
# GET /repositories/1/edit
12588
def edit
@@ -128,7 +91,40 @@ def edit
12891
# POST /repositories
12992
# POST /repositories.json
13093
def create
94+
github = Octokit::Client.new access_token: current_user.oauth_token
13195
@repository = Repository.new(repository_params)
96+
repop = github.repo @repository.full_name
97+
if repop
98+
@name_repo = repop.full_name
99+
commits = github.commits @name_repo
100+
commits.each do |c|
101+
cTemp = github.commit @name_repo, c.sha
102+
commit = Commit.new
103+
#if Author not exists in that repository
104+
if !Author.where(username: cTemp.commit.author.email.to_s).any?
105+
author = Author.new
106+
author.username = cTemp.commit.author.email.to_s
107+
author.name = cTemp.commit.author.name.to_s
108+
author.repositories << @repository
109+
author.save
110+
else
111+
author = Author.where(username: cTemp.commit.author.email.to_s).first
112+
if !author.repositories.where(id: @repository.id).any?
113+
author.repositories << @repository
114+
end
115+
author.save
116+
end
117+
commit.message = cTemp.commit.message.to_s
118+
commit.additions = cTemp.stats.additions.to_i
119+
commit.deletions = cTemp.stats.deletions.to_i
120+
commit.files_changed = cTemp.files.count.to_i
121+
commit.author_username = Author.where(username: cTemp.commit.author.email.to_s).first.username
122+
commit.repository = @repository
123+
commit.save
124+
end
125+
else
126+
@name_repo = "nil"
127+
end
132128

133129
respond_to do |format|
134130
if @repository.save
@@ -160,41 +156,41 @@ def update
160156
def destroy
161157
@repository.destroy
162158
respond_to do |format|
163-
format.html { redirect_to repositories_url, notice: 'Repository was successfully destroyed.' }
159+
format.html { redirect_to repositories_path, notice: 'Repository was successfully destroyed.' }
164160
format.json { head :no_content }
165161
end
166162
end
167163

168164
private
169-
# Use callbacks to share common setup or constraints between actions.
170-
def set_repository
171-
@repository = Repository.find(params[:id])
172-
end
165+
# Use callbacks to share common setup or constraints between actions.
166+
def set_repository
167+
@repository = Repository.find(params[:id])
168+
end
173169

174-
# Never trust parameters from the scary internet, only allow the white list through.
175-
def repository_params
176-
params.require(:repository).permit(:github_id, :url, :name, :full_name, :description, :size, :collaborator)
177-
end
170+
# Never trust parameters from the scary internet, only allow the white list through.
171+
def repository_params
172+
params.require(:repository).permit(:github_id, :url, :name, :full_name, :description, :size, :collaborator)
173+
end
178174

179-
def set_initial_variables
180-
github = Octokit::Client.new access_token: current_user.oauth_token
181-
repos = Repository.all.to_a
182-
github.repos.each do |item|
183-
if !Repository.where(github_id: item.id).any?
184-
repo = Repository.new
185-
repo.github_id = item.id
186-
repo.url = item.html_url
187-
repo.name = item.name
188-
repo.full_name = item.full_name
189-
repo.description = item.description
190-
repo.size = item.size
191-
repo.collaborator = item.collaborator
192-
repos.push(repo)
193-
end
175+
def set_initial_variables
176+
github = Octokit::Client.new access_token: current_user.oauth_token
177+
repos = Repository.all.to_a
178+
github.repos.each do |item|
179+
if !Repository.where(github_id: item.id).any?
180+
repo = Repository.new
181+
repo.github_id = item.id
182+
repo.url = item.html_url
183+
repo.name = item.name
184+
repo.full_name = item.full_name
185+
repo.description = item.description
186+
repo.size = item.size
187+
repo.collaborator = item.collaborator
188+
repos.push(repo)
194189
end
195-
196-
@repositories = repos
197-
@repo = Repository.new
198-
# @site = github.oauth.login
199190
end
191+
192+
@repositories = repos
193+
@repo = Repository.new
194+
# @site = github.oauth.login
195+
end
200196
end

app/controllers/sessions_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class SessionsController < ApplicationController
22
def new
3+
redirect_to repositories_path if current_user
34
end
45

56
def create

app/models/author.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class Author < ApplicationRecord
2+
has_many :repository_authors
3+
has_many :repositories, through: :repository_authors
24
end

app/models/commit.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Commit < ApplicationRecord
2+
belongs_to :repository
23
end

app/models/organization.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class Organization < ApplicationRecord
2+
belongs_to :user
3+
has_many :repositories
24
end

app/models/repository.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
class Repository < ApplicationRecord
2+
has_many :commits, dependent: :destroy
3+
has_many :repository_authors, dependent: :destroy
4+
has_many :authors, through: :repository_authors, dependent: :destroy
25
end

app/models/repository_author.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class RepositoryAuthor < ApplicationRecord
2+
belongs_to :repository
3+
belongs_to :author
4+
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class User < ApplicationRecord
2+
has_many :organizations
3+
24
def self.from_omniauth(auth)
35
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
46
user.email = auth.info.email

app/views/repositories/show.html.erb

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
<div class="card-title text-center "> Colaboradores</div>
2020

2121
<ul class="list-group" data-spy="scroll" style="overflow: scroll; max-height: 300px;">
22-
<% @data.each do |email, value| %>
22+
<% @authors.each do |author| %>
2323
<li class="list-group-item">
24-
Name: <%=value["name"] %>
25-
A: <%=value["additions"] %>
26-
D: <%=value["deletions"] %>
27-
M: <%=value["modified"] %>
28-
24+
Name: <%= author.name %>
25+
A: <%= @repository.commits.where(author_username: author.username).sum(&:additions) %>
26+
D: <%= @repository.commits.where(author_username: author.username).sum(&:deletions) %>
27+
M: <%= @repository.commits.where(author_username: author.username).sum(&:files_changed) %>
2928
</li>
3029
<%end%>
3130

config.ru

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is used by Rack-based servers to start the application.
22

33
require_relative 'config/environment'
4-
4+
Octokit.auto_paginate = true
55
run Rails.application
6+

db/migrate/20190331221631_create_commits.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class CreateCommits < ActiveRecord::Migration[5.2]
22
def change
33
create_table :commits do |t|
4-
t.string :title
5-
t.string :description
4+
t.string :message
5+
t.string :author_username
66
t.integer :additions
77
t.integer :deletions
8-
t.integer :changed_files
8+
t.integer :files_changed
99

1010
t.timestamps
1111
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddUserIdToOrganizations < ActiveRecord::Migration[5.2]
2+
def change
3+
add_reference :organizations, :user, foreign_key: true
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddOrganizationIdToRepositories < ActiveRecord::Migration[5.2]
2+
def change
3+
add_reference :repositories, :organization, foreign_key: true
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddRepositoryIdToCommits < ActiveRecord::Migration[5.2]
2+
def change
3+
add_reference :commits, :repository, foreign_key: true
4+
end
5+
end

0 commit comments

Comments
 (0)