* Rubocop enabled style/alias * Enabled Style/ArrayJoin Cop * Enabled Style/Attr * Enabled Case Equality * Enabled CharacterLiteral * Enabled ColonMethodCall Cop * Enabled CommentAnnotation cop * Enabled PreferredHashMethods Cop * Enabled DoubleNegation Cop * Enabled EachWithObject Cop * Enabled EmptyLiteral Cop * Enabled EvenOdd Cop * Enabled IfWithSemicolon Cop * Enabled Lambda and LambdaCall Cop * Enabled LineEndConcatenation Cop * Enabled ModuleFunction Cop; * Enable NegatedIf and NegatedWhile Cop * Enabled NilComparison Cop * Enabled Not Cop * Enabled NumericLiterals Cop * Enabled OneLineConditional Cop * Enabled PercentLiteralDelimiters Cop * Excluded internal/users_controller from negated_if cop * Reverted the double negation change from github_issue_tag and github_issue.rb" * Enabled PerlBackrefs Cop * Changed Regexp.last_match(1) to Regexp.last_match(0) * Enabled proc cop * Enabled RaiseArgs Cop * Reverted Regexp.last_match(0) to Regexp.last_match(1) * Enabled SelfAssignment Cop * Enabled SingleLineMethods Cop * Enabled SpecialGlobalVars Cop * Enabled VariableInterpolation Cop * Enabled WhenThen Cop * Enabled WhileUntilModifier Cop * Enabled WordArray Cop * Enabled IfUnlessModifier Cop * Enabled GuardClause Cop
65 lines
1.7 KiB
Ruby
65 lines
1.7 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
|
|
raise StandardError, "A GitHub issue 404'ed and could not be found!" if e.message.include?("404 - Not Found")
|
|
|
|
raise StandardError, e.message
|
|
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(issue_or_pull(url))
|
|
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
|
|
|
|
def self.issue_or_pull(url)
|
|
if !url.include?("pull")
|
|
"/issues/"
|
|
else
|
|
"/pull/"
|
|
end
|
|
end
|
|
end
|