docbrown/app/models/github_issue.rb
rhymes e588fa7ece Code cleanups (#659)
* Initial automatic cleanup with rubocop

* Fix syntax error introduced by rubocop

* Cleanup seeds file

* Cleanup lib folder

* Exclude bin folder because it contains auto generated files

* Make Rubocop a little bit more chatty

* Block length should not include comments in the count

* Cleanup config folder

* Cleanup specs

* Updated Rubocop version and generated a todo file

* Fix broken ArticlesApi spec

* Fix tests

* Restored rubocop pre-commit hook
2018-08-07 11:00:13 -04:00

59 lines
1.6 KiB
Ruby

class GithubIssue < ApplicationRecord
serialize :issue_serialized, Hash
validates :category, inclusion: { in: %w(issue issue_comment) }
def self.find_or_fetch(url)
find_by_url(url) || fetch(url)
end
def self.fetch(url)
try_to_get_issue(url)
rescue StandardError => e
if e.message.include?("404 - Not Found")
raise StandardError, "A GitHub issue 404'ed and could not be found!"
else
raise StandardError, e.message
end
end
def self.try_to_get_issue(url)
client = Octokit::Client.new(access_token: random_token)
issue = GithubIssue.new(url: url)
if !!(url !~ /\/issues\/comments/)
repo, issue_id = url.gsub(/.*github\.com\/repos\//, "").split("/issues/")
issue.issue_serialized = get_issue_serialized(client, repo, issue_id)
issue.category = "issue"
else
repo, issue_id = url.gsub(/.*github\.com\/repos\//, "").split("/issues/comments/")
issue.issue_serialized = client.issue_comment(repo, issue_id).to_hash
issue.category = "issue_comment"
end
issue.processed_html = get_html(client, issue)
issue.save!
issue
end
def self.get_html(client, issue)
client.markdown(issue.issue_serialized[:body])
end
def self.get_issue_serialized(client, repo, issue_id)
client.issue(repo, issue_id).to_hash
end
def self.random_token
if Rails.env.test?
"ddsddsdsdssdsds"
else
random_identity.token
end
end
def self.random_identity
if Rails.env.production?
Identity.where(provider: "github").last(250).sample
else
Identity.where(provider: "github").last
end
end
end