Commit 6e467428 by Bui Minh Duc

clone github api to local database

parent b821a634
...@@ -35,6 +35,8 @@ gem 'octokit', '~> 4.6', '>= 4.6.2' ...@@ -35,6 +35,8 @@ gem 'octokit', '~> 4.6', '>= 4.6.2'
gem 'will_paginate', '~> 3.1', '>= 3.1.5' gem 'will_paginate', '~> 3.1', '>= 3.1.5'
gem 'bootstrap-select-rails', '~> 1.6', '>= 1.6.3' gem 'bootstrap-select-rails', '~> 1.6', '>= 1.6.3'
gem 'bootstrap-table-rails', '~> 1.11' gem 'bootstrap-table-rails', '~> 1.11'
gem 'momentjs-rails', '>= 2.9.0'
gem 'bootstrap3-datetimepicker-rails', '~> 4.14.30'
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console
......
...@@ -48,6 +48,8 @@ GEM ...@@ -48,6 +48,8 @@ GEM
sass (>= 3.3.4) sass (>= 3.3.4)
bootstrap-select-rails (1.6.3) bootstrap-select-rails (1.6.3)
bootstrap-table-rails (1.11.0) bootstrap-table-rails (1.11.0)
bootstrap3-datetimepicker-rails (4.14.30)
momentjs-rails (>= 2.8.1)
builder (3.2.2) builder (3.2.2)
byebug (9.0.6) byebug (9.0.6)
coffee-rails (4.2.1) coffee-rails (4.2.1)
...@@ -87,6 +89,8 @@ GEM ...@@ -87,6 +89,8 @@ GEM
mime-types-data (3.2016.0521) mime-types-data (3.2016.0521)
mini_portile2 (2.1.0) mini_portile2 (2.1.0)
minitest (5.9.1) minitest (5.9.1)
momentjs-rails (2.15.1)
railties (>= 3.1)
multi_json (1.12.1) multi_json (1.12.1)
multipart-post (2.0.0) multipart-post (2.0.0)
nio4r (1.2.1) nio4r (1.2.1)
...@@ -176,11 +180,13 @@ DEPENDENCIES ...@@ -176,11 +180,13 @@ DEPENDENCIES
bootstrap-sass (~> 3.3.6) bootstrap-sass (~> 3.3.6)
bootstrap-select-rails (~> 1.6, >= 1.6.3) bootstrap-select-rails (~> 1.6, >= 1.6.3)
bootstrap-table-rails (~> 1.11) bootstrap-table-rails (~> 1.11)
bootstrap3-datetimepicker-rails (~> 4.14.30)
byebug byebug
coffee-rails (~> 4.2) coffee-rails (~> 4.2)
jbuilder (~> 2.5) jbuilder (~> 2.5)
jquery-rails jquery-rails
listen (~> 3.0.5) listen (~> 3.0.5)
momentjs-rails (>= 2.9.0)
octokit (~> 4.6, >= 4.6.2) octokit (~> 4.6, >= 4.6.2)
puma (~> 3.0) puma (~> 3.0)
rails (~> 5.0.0, >= 5.0.0.1) rails (~> 5.0.0, >= 5.0.0.1)
......
...@@ -19,3 +19,5 @@ ...@@ -19,3 +19,5 @@
//= require bootstrap/alert //= require bootstrap/alert
//= require bootstrap/dropdown //= require bootstrap/dropdown
//= require bootstrap-table //= require bootstrap-table
//= require moment
//= require bootstrap-datetimepicker
...@@ -5,3 +5,4 @@ ...@@ -5,3 +5,4 @@
@import "bootstrap-sprockets"; @import "bootstrap-sprockets";
@import "bootstrap"; @import "bootstrap";
@import "bootstrap-select"; @import "bootstrap-select";
@import "bootstrap-datetimepicker";
class MainController < ApplicationController class MainController < ApplicationController
def index def index
@repos = Repository.all @repos = @repos || Repository.all
@selected_repos = Repository.where(selected: true)
@selected_repos_obj = []
@selected_repos.each do |repo|
repo_obj = Hash.new
repo_obj[:name] = repo.name
repo_obj[:issues] = $client.list_issues("ZIGExN/" + repo.name)
@selected_repos_obj.append(repo_obj)
end
@labels = @label || Label.all
end
def update_repo_selected
repos_picked = Repository.where(name: params[:selected])
repos_picked.each do |repo|
repo.selected = true
repo.save
end
repos_not_picked = Repository.where.not(name: params[:selected])
repos_not_picked.each do |repo|
repo.selected = false
repo.save
end
@repos = @repos || Repository.all
@selected_repos = repos_picked
@labels = @label || Label.all
render "index"
end
def load_repo_selected
# @repos = Repository.find()
end end
end end
class Issue < ApplicationRecord
belongs_to :repository
belongs_to :user, optional: true #owner
has_and_belongs_to_many :labels
has_and_belongs_to_many :users # assignees
end
class Label < ApplicationRecord
belongs_to :repository
has_and_belongs_to_many :issues
end
class User < ApplicationRecord
has_and_belongs_to_many :issues
end
...@@ -46,14 +46,103 @@ ...@@ -46,14 +46,103 @@
<div class="container"> <div class="container">
<!-- content --> <!-- content -->
<select class="selectpicker" multiple data-live-search="true" multiple data-selected-text-format="count > 3"> <div class="form-group">
<select class="selectpicker" id="selectpicker1" multiple data-live-search="true" multiple data-selected-text-format="count > 3">
<% @repos.each do |repo| %> <% @repos.each do |repo| %>
<option><%= repo.name %></option> <option><%= repo.name %></option>
<% end %> <% end %>
</select> </select>
<button>Apply</button> <button id="repo_apply" class="btn btn-default">Apply</button>
</div>
<form class="form-inline">
<div class="form-group">
<label for="from">From</label>
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<label for="to">To</label>
<div class='input-group date' id='datetimepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<select class="selectpicker" multiple data-live-search="true">
<% @labels.each do |repo| %>
<option><%= repo.name %></option>
<% end %>
</select>
</div>
<input type="submit" class="btn btn-default">
</form>
<div>
<table class="table">
<thead class="thead-inverse">
<tr>
<th></th>
<% @labels.each do |label| %>
<th><%= label.name %></th>
<% end %>
</tr>
</thead>
<tbody>
<% @selected_repos.each do |selected_repo| %>
<tr>
<td><%= link_to selected_repo.name, selected_repo.html_url %></td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
var selected_repos = $('.selectpicker').selectpicker('val'); $(document).ready(function() {
$('#datetimepicker1').datetimepicker({
format: "DD/MM/YYYY"
});
$('#datetimepicker2').datetimepicker({
format: "DD/MM/YYYY"
});
});
$("#repo_apply").click(function() {
var selected_repos = $('#selectpicker1').selectpicker('val');
$.ajax({
method: "POST",
url: "<%= update_repo_selected_path %>",
data: { selected: selected_repos },
success: function(res) {
console.log(res);
location.reload();
}
});
});
// function load_repos(list_name) {
// var result;
// $.ajax({
// method: "POST",
// url: "<%= load_repo_selected_path %>",
// data: { name: list_name },
// success: function(res) {
// console.log(res);
// result = res;
// }
// });
// return result;
// }
</script> </script>
</div> </div>
$client = Octokit::Client.new(access_token: Rails.application.secrets.GITHUB_TOKEN)
...@@ -2,6 +2,8 @@ Rails.application.routes.draw do ...@@ -2,6 +2,8 @@ Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root "main#index" root "main#index"
get "/repo", to: "repository#index" get "/repo", to: "repository#index"
post "/update_repo_selected", to: "main#update_repo_selected"
post "/load_repo_selected", to: "main#load_repo_selected"
# resource :repository # resource :repository
end end
class CreateRepositories < ActiveRecord::Migration[5.0] class CreateRepositories < ActiveRecord::Migration[5.0]
def change def change
create_table :repositories do |t| create_table :repositories do |t|
t.integer :repo_id
t.string :name t.string :name
t.string :full_name t.string :full_name
t.string :html_url
t.string :description t.string :description
t.boolean :private
t.string :url t.string :url
t.string :html_url
t.boolean :selected t.boolean :selected
t.date :created_at
t.date :updated_at
t.timestamps t.timestamps
end end
......
class CreateLabels < ActiveRecord::Migration[5.0]
def change
create_table :labels do |t|
t.string :url
t.string :name
t.string :color
t.boolean :default
t.references :repository, foreign_key: true
t.date :created_at
t.date :updated_at
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :login
t.string :url
t.string :html_url
t.date :created_at
t.date :updated_at
t.timestamps
end
end
end
class CreateIssues < ActiveRecord::Migration[5.0]
def change
create_table :issues do |t|
t.string :url
t.string :repository_url
t.integer :number
t.string :title
t.text :body
t.references :repository, foreign_key: true
t.references :user, foreign_key: true
t.date :created_at
t.date :updated_at
t.timestamps
end
end
end
class IssuesLabels < ActiveRecord::Migration[5.0]
def change
create_join_table :issues, :labels do |t|
t.index [:issue_id, :label_id]
t.index [:label_id, :issue_id]
end
end
end
class IssuesAssignees < ActiveRecord::Migration[5.0]
def change
create_join_table :issues, :users do |t|
t.index [:issue_id, :user_id]
t.index [:user_id, :issue_id]
end
end
end
...@@ -10,18 +10,65 @@ ...@@ -10,18 +10,65 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161122095355) do ActiveRecord::Schema.define(version: 20161125072404) do
create_table "issues", force: :cascade do |t|
t.string "url"
t.string "repository_url"
t.integer "number"
t.string "title"
t.text "body"
t.integer "repository_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["repository_id"], name: "index_issues_on_repository_id"
t.index ["user_id"], name: "index_issues_on_user_id"
end
create_table "issues_labels", id: false, force: :cascade do |t|
t.integer "issue_id", null: false
t.integer "label_id", null: false
t.index ["issue_id", "label_id"], name: "index_issues_labels_on_issue_id_and_label_id"
t.index ["label_id", "issue_id"], name: "index_issues_labels_on_label_id_and_issue_id"
end
create_table "issues_users", id: false, force: :cascade do |t|
t.integer "issue_id", null: false
t.integer "user_id", null: false
t.index ["issue_id", "user_id"], name: "index_issues_users_on_issue_id_and_user_id"
t.index ["user_id", "issue_id"], name: "index_issues_users_on_user_id_and_issue_id"
end
create_table "labels", force: :cascade do |t|
t.string "url"
t.string "name"
t.string "color"
t.boolean "default"
t.integer "repository_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["repository_id"], name: "index_labels_on_repository_id"
end
create_table "repositories", force: :cascade do |t| create_table "repositories", force: :cascade do |t|
t.integer "repo_id"
t.string "name" t.string "name"
t.string "full_name" t.string "full_name"
t.string "html_url"
t.string "description" t.string "description"
t.boolean "private"
t.string "url" t.string "url"
t.string "html_url"
t.boolean "selected" t.boolean "selected"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "users", force: :cascade do |t|
t.string "login"
t.string "url"
t.string "html_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end end
File added
File added
design.png

8.83 KB

namespace :github do namespace :github do
$org = "ZIGExN"
desc "Insert Zigexn repository" desc "Insert Zigexn Github data"
task insert_repos: :environment do task insert_github_data: :environment do
org = "ZIGExN" insert_repos($client)
client = Octokit::Client.new(access_token: Rails.application.secrets.GITHUB_TOKEN) insert_users($client)
repos = client.org_repos(org)
db_repos = Repository.all
# db_repos.each do |db_repo|
# insert_labels(db_repo, $client)
# end
db_repos.each do |db_repo|
insert_issues(db_repo, $client)
end
end
def insert_repos(client)
repos = client.org_repos($org)
last_page = 1
unless client.last_response.headers[:link].nil?
last_page = client.last_response.headers[:link].split(' ')[2][-3].to_i last_page = client.last_response.headers[:link].split(' ')[2][-3].to_i
end
for i in (1..last_page) for i in (1..last_page)
repos = client.org_repos(org, {type: 'all', page: i}) repos = client.org_repos($org, {type: 'all', page: i})
repos.each do |repo| repos.each do |repo|
db_repo = Repository.where(id: repo.id).first
new_flag = false
if db_repo.nil?
db_repo = Repository.new db_repo = Repository.new
db_repo.repo_id = repo.id new_flag = true
end
db_repo.id = repo.id
db_repo.name = repo.name db_repo.name = repo.name
db_repo.full_name = repo.full_name db_repo.full_name = repo.full_name
db_repo.html_url = repo.html_url db_repo.private = repo.private
db_repo.description = repo.description db_repo.description = repo.description
db_repo.url = repo.url db_repo.url = repo.url
db_repo.html_url = repo.html_url
db_repo.created_at = repo.created_at db_repo.created_at = repo.created_at
db_repo.updated_at = repo.updated_at db_repo.updated_at = repo.updated_at
if db_repo.save if db_repo.save
puts "Insert repo success" if new_flag
puts "Insert repo success: " + repo.name
else
puts "Update repo success: " + repo.name
end
else
puts "Repo >> An error occured: " + repo.name
end
end
end
end
def insert_labels(repo, client)
labels = client.labels($org + "/" + repo.name)
last_page = 1
unless client.last_response.headers[:link].nil?
last_page = client.last_response.headers[:link].split(' ')[2][-3].to_i
end
for i in (1..last_page)
labels = client.labels($org + "/" + repo.name, {page: i})
labels.each do |label|
db_label = Label.where(id: label.id).first
new_flag = false
if db_label.nil?
new_flag = true
db_label = Label.new
end
db_label.id = label.id
db_label.url = label.url
db_label.name = label.name
db_label.color = label.color
db_label.default = label.default
db_label.repository_id = repo.id
if db_label.save
if new_flag
puts "Insert label success: " + label.name
else
puts "Update label success: " + label.name
end
else
puts "Label >> An error occured: " + label.name
end
end
end
end
def insert_users(client)
users = client.org_members($org)
last_page = 1
unless client.last_response.headers[:link].nil?
last_page = client.last_response.headers[:link].split(' ')[2][-3].to_i
end
for i in (1..last_page)
users = client.org_members($org, {page: i})
users.each do |user|
db_user = User.where(id: user.id).first
new_flag = false
if db_user.nil?
new_flag = true
db_user = User.new
end
db_user.id = user.id
db_user.login = user.login
db_user.url = user.url
db_user.html_url = user.html_url
if db_user.save
if new_flag
puts "Insert user success: " + user.login
else
puts "Update user success: " + user.login
end
else
puts "User >> An error occured: " + user.login
end
end
end
end
def insert_issues(repo, client)
issues = client.list_issues($org + "/" + repo.name)
last_page = 1
unless client.last_response.headers[:link].nil?
last_page = client.last_response.headers[:link].split(' ')[2][-3].to_i
end
for i in (1..last_page)
issues = client.list_issues($org + "/" + repo.name, {page: i})
issues.each do |issue|
db_issue = Issue.where(id: issue.id).first
new_flag = false
if db_issue.nil?
new_flag = true
db_issue = Issue.new
end
db_issue.id = issue.id
db_issue.url = issue.url
db_issue.repository_url = issue.repository_url
db_issue.number = issue.number
db_issue.title = issue.title
db_issue.body = issue.body
db_issue.repository_id = repo.id
db_issue.user_id = issue.user.id
issue.labels.each do |label|
db_label = Label.where(id: label.id).first
db_issue.labels.append(db_label)
end
# TODO implement assignees relation
if db_issue.save
if new_flag
puts "Insert issue success: " + issue.title
else
puts "Update issue success: " + issue.title
end
else else
puts "Repo exist" byebug
puts "Issue >> an error occured: " + issue.title
end end
end end
end end
......
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
url: MyString
repository_url: MyString
number: 1
title: MyString
body: MyText
repository: one
user: one
created_at: 2016-11-25
updated_at: 2016-11-25
two:
url: MyString
repository_url: MyString
number: 1
title: MyString
body: MyText
repository: two
user: two
created_at: 2016-11-25
updated_at: 2016-11-25
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
url: MyString
name: MyString
color: MyString
default: false
repository: one
created_at: 2016-11-25
updated_at: 2016-11-25
two:
url: MyString
name: MyString
color: MyString
default: false
repository: two
created_at: 2016-11-25
updated_at: 2016-11-25
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one: one:
repo_id: 1
name: MyString name: MyString
full_name: MyString full_name: MyString
html_url: MyString
description: MyString description: MyString
private: false
url: MyString url: MyString
html_url: MyString
selected: false selected: false
created_at: 2016-11-25
updated_at: 2016-11-25
two: two:
repo_id: 1
name: MyString name: MyString
full_name: MyString full_name: MyString
html_url: MyString
description: MyString description: MyString
private: false
url: MyString url: MyString
html_url: MyString
selected: false selected: false
created_at: 2016-11-25
updated_at: 2016-11-25
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
login: MyString
url: MyString
html_url: MyString
created_at: 2016-11-25
updated_at: 2016-11-25
two:
login: MyString
url: MyString
html_url: MyString
created_at: 2016-11-25
updated_at: 2016-11-25
require 'test_helper'
class IssueTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class LabelTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment