Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
6 changes: 6 additions & 0 deletions plugins/github_relation/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
gem "octokit"
group :development, :test do
gem 'rspec-rails'
gem 'debugger'
gem 'hashie'
end
87 changes: 87 additions & 0 deletions plugins/github_relation/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
GEM
specs:
actionpack (3.2.14)
activemodel (= 3.2.14)
activesupport (= 3.2.14)
builder (~> 3.0.0)
erubis (~> 2.7.0)
journey (~> 1.0.4)
rack (~> 1.4.5)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.2.1)
activemodel (3.2.14)
activesupport (= 3.2.14)
builder (~> 3.0.0)
activesupport (3.2.14)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
addressable (2.3.5)
builder (3.0.4)
columnize (0.3.6)
debugger (1.6.3)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.2.4)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.4)
diff-lcs (1.2.5)
erubis (2.7.0)
faraday (0.8.8)
multipart-post (~> 1.2.0)
hashie (2.0.5)
hike (1.2.3)
i18n (0.6.4)
journey (1.0.4)
json (1.8.1)
multi_json (1.8.2)
multipart-post (1.2.0)
octokit (2.6.3)
sawyer (~> 0.5.1)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
railties (3.2.14)
actionpack (= 3.2.14)
activesupport (= 3.2.14)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.1.0)
rdoc (3.12.2)
json (~> 1.4)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
rspec-rails (2.14.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
sawyer (0.5.1)
addressable (~> 2.3.5)
faraday (~> 0.8, < 0.10)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
thor (0.18.1)
tilt (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
debugger
hashie
octokit
rspec-rails
3 changes: 3 additions & 0 deletions plugins/github_relation/README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= github_relation

Description goes here
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class GithubProjectController < ApplicationController
unloadable

before_filter :find_project, :authorize

def index
if @github_project.present?
redirect_to :action => :show, id: @github_project.id, project_id: @project.id
else
redirect_to action: :new, project_id: @project.id
end
end

def show
end

def new
@github_project = GithubProject.new()
end

def edit
end

def create
@github_project = GithubProject.new(params[:github_project])
@github_project.project_id = @project.id

unless @github_project.save
render :action => :new
return
end
redirect_to :action => :show, id: @github_project.id, project_id: @project.id
end

def update
@github_project.attributes = params[:github_project]
@github_project.project_id = @project.id

unless @github_project.save
render :action => :edit
return
end
redirect_to :action => :show, id: @github_project.id, project_id: @project.id
end

def get_data
@github_project.get_from_github(params[:login], params[:password])
redirect_to :action => :show, id: @github_project.id, project_id: @project.id
end

private
def find_project
@project = Project.find(params[:project_id])
@github_project = GithubProject.where(project_id: @project.id).first
end

end
95 changes: 95 additions & 0 deletions plugins/github_relation/app/models/github_issue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class GithubIssue < ActiveRecord::Base
attr_accessible :issue_number

has_many :github_issue_comments
has_many :relation_to, :class_name => 'GithubIssueRelation', :foreign_key => 'issue_from_id'

belongs_to :issue

def self.create_issues(project, list_issues)
created_data = list_issues.select do |issue_from_github|
github_issue = GithubIssue.where(issue_number: issue_from_github.number).first_or_create()
github_issue.update_from_github(project, issue_from_github)
end

GithubIssue.where(issue_number: created_data.map{|issue| issue.number})
end

def update_from_github(project, issue_from_github, status = :open)
self.build_issue if issue.nil?
return if issue.updated_on.present? && issue.updated_on > issue_from_github.updated_at

issue.subject = issue_from_github.title
issue.description = issue_from_github.body
issue.project = project
issue.tracker = Tracker.first
issue.author = GithubUser.user_by_github_login(issue_from_github.user)
if issue_from_github.assignee.present?
issue.assigned_to = GithubUser.user_by_github_login(issue_from_github.assignee)
end
issue.created_on = issue_from_github.created_at
issue.updated_on = issue_from_github.updated_at
case status
when :open
issue.closed_on = nil
issue.status = IssueStatus.where(is_closed: false).first
when :close
issue.closed_on = issue_from_github.closed_at
issue.status = IssueStatus.where(is_closed: true).first
else
end

Issue.skip_callback(:save, :before, :force_updated_on_change)
Issue.skip_callback(:save, :before, :update_closed_on)
issue.save!
Issue.set_callback(:save, :before, :force_updated_on_change)
Issue.set_callback(:save, :before, :update_closed_on)
self.save!
true
end

def self.closed_issues(list_issues)
github_issue_number = list_issues.map{|issue_from_github| issue_from_github.number}
GithubIssue.where(issue_id: self.project.issues.open.map{|issue| issue.id}).
where( GithubIssue.arel_table[:issue_number].not_in(github_issue_number))
end

def set_issue_comment_from_github(issue_comments)
issue_comments.each do |issue_comment_from_github|
issue_comment = github_issue_comments.where(issue_comment_number: issue_comment_from_github.id.to_s).first_or_create
issue_comment.github_issue = self
issue_comment.update_from_github(issue_comment_from_github)
issue_comment.save!
end

issue_comment_number = issue_comments.map {|issue_comment_from_github| issue_comment_from_github.id.to_s}
closed_issue_comments = github_issue_comments.where( GithubIssueComment.arel_table[:issue_comment_number].not_in(issue_comment_number))
closed_issue_comments.each do |issue_comment|
issue_comment.destroy
end
end

def create_and_delete_relation_issues
relation_to_issues.reject{|issue| relation_issues.any? {|relation| relation == issue}}.each do |issue|
relation_to.where(issue_to_id: issue.id).each do |to_issue|
to_issue.destroy
end
end

relation_issues.reject{|issue| relation_to_issues.any? {|relation| relation == issue}}.each do |issue|
relation_to.create issue_to_id: issue.id
end
end

private
def relation_issues
issue_numbers = github_issue_comments(true).inject([]) do |numbers, github_issue|
numbers + github_issue.relation_issues
end
GithubIssue.where(issue_number: issue_numbers.uniq)
end

def relation_to_issues
relation_to.map {|relation| relation.issue_to}
end
end
32 changes: 32 additions & 0 deletions plugins/github_relation/app/models/github_issue_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class GithubIssueComment < ActiveRecord::Base
attr_accessible :issue_comment_number

belongs_to :github_issue
belongs_to :journal

after_destroy :destroy_issue_comment

def update_from_github(issue_comment)
new_journal = self.journal || Journal.new(:journalized => github_issue.issue, :user => User.current)
new_journal.notes = issue_comment.body
new_journal.save!
self.journal = new_journal
end

def relation_issues
match_data = journal.notes.match /#(\d*)/

issue_ids = []
while match_data.present?
issue_ids << match_data[1].to_i
match_data = match_data.post_match.match /#(\d*)/
end

issue_ids
end

def destroy_issue_comment
self.journal.destroy
end

end
24 changes: 24 additions & 0 deletions plugins/github_relation/app/models/github_issue_relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class GithubIssueRelation < ActiveRecord::Base
attr_accessible :issue_from_id, :issue_to_id

belongs_to :issue_from, :class_name => 'GithubIssue', :foreign_key => 'issue_from_id'
belongs_to :issue_to, :class_name => 'GithubIssue', :foreign_key => 'issue_to_id'
belongs_to :issue_relation

after_create :create_issue_relation
after_destroy :destroy_issue_relation

private
def create_issue_relation
issue_relation = IssueRelation.new
issue_relation.issue_from = issue_from.issue
issue_relation.issue_to = issue_to.issue
issue_relation.save!
self.issue_relation = issue_relation
self.save!
end

def destroy_issue_relation
self.issue_relation.destroy
end
end
28 changes: 28 additions & 0 deletions plugins/github_relation/app/models/github_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class GithubProject < ActiveRecord::Base
attr_accessible :organization, :project_name

validates :organization, presence: true
validates :project_name, presence: true

belongs_to :project

def get_from_github(login, password)
github_relation = Github::Relation.new(login, password)

list_issues = github_relation.issues(self.organization, self.project_name)

updated_issues = GithubIssue.create_issues(self.project, list_issues)

closed_issues = GithubIssue.closed_issues(list_issues)
closed_issues.each do |issue|
github_issue = github_relation.issue(self.organization, self.project_name, issue.issue_number)
issue.update_from_github(self.project, github_issue, true)
end

(updated_issues + closed_issues).each do |issue|
issue_comments = github_relation.issue_comments(self.organization, self.project_name, issue.issue_number)
issue.set_issue_comment_from_github issue_comments
issue.create_and_delete_relation_issues
end
end
end
33 changes: 33 additions & 0 deletions plugins/github_relation/app/models/github_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class GithubUser < ActiveRecord::Base
attr_accessible :login

belongs_to :user

after_create :create_user

def self.create_users(users)
users.each do |user_from_github|
next if GithubUser.exists?(login: user_from_github.login)

GithubUser.create(login: user_from_github.login)
end
end

def self.user_by_github_login(user_by_github)
github_user = self.where(login: user_by_github.login).first_or_create
github_user.user
end

private
def create_user
return if User.exists?(login: self.login)

user = self.build_user
user.login = self.login
user.firstname = self.login
user.lastname = self.login
user.mail = "#{self.login}@piyo.hoge"
user.save!
self.save!
end
end
12 changes: 12 additions & 0 deletions plugins/github_relation/app/views/github_project/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= hidden_field_tag :project_id, @project.id %>
<table>
<tr>
<th><%= f.label :organization %></th>
<td><%= f.text_field :organization %></td>
</tr>
<tr>
<th><%= f.label :project_name %></th>
<td><%= f.text_field :project_name %></td>
</tr>
</table>
<%= f.submit %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= form_for(@github_project,:url => {:action => :update} ) do |f| %>
<%= render 'form', f: f %>
<% end %>
Loading