-
Notifications
You must be signed in to change notification settings - Fork 12
/
importer.rb
240 lines (194 loc) · 6.43 KB
/
importer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# frozen_string_literal: true
module DiscourseCodeReview
class Importer
attr_reader :github_repo
def initialize(github_repo)
@github_repo = github_repo
end
def self.sync_commit(sha)
client = DiscourseCodeReview.octokit_client
GithubCategorySyncer.each_repo_name do |repo_name|
repo = GithubRepo.new(repo_name, client)
importer = Importer.new(repo)
if commit = repo.commit(sha)
importer.sync_commit(commit)
return repo_name
end
end
nil
end
def category_id
@category_id ||=
GithubCategorySyncer.ensure_category(
repo_name: github_repo.name
).id
end
def sync_merged_commits
last_commit = nil
github_repo.commits_since.each do |commit|
sync_commit(commit)
github_repo.last_commit = commit[:hash]
end
end
def auto_link_commits(text, doc = nil)
linked_commits = find_linked_commits(text)
if (linked_commits.length > 0)
doc ||= Nokogiri::HTML::fragment(PrettyText.cook(text))
skip_tags = ["a", "code"]
linked_commits.each do |hash, topic|
doc.traverse do |node|
if node.text? && !skip_tags.include?(node.parent&.name)
node.replace node.content.gsub(hash, "<a href='#{topic.url}'>#{hash}</a>")
end
end
text = HtmlToMarkdown.new(doc.to_html).to_markdown
end
end
[text, linked_commits, doc]
end
def detect_shas(text)
text.scan(/(?:[^a-zA-Z0-9]|^)([a-f0-9]{8,})(?:[^a-zA-Z0-9]|$)/).flatten
end
def find_linked_commits(text)
result = {}
shas = detect_shas(text)
if shas.length > 0
like_clause = shas.map { |sha| "f.value LIKE '#{sha}%'" }.join(' OR ')
topics = Topic.select("topics.*, value AS sha")
.joins("JOIN topic_custom_fields f ON topics.id = topic_id AND f.name = '#{DiscourseCodeReview::CommitHash}'")
.where(like_clause)
topics.each do |topic|
lookup_shas = shas.select { |sha| topic.sha.start_with? sha }
lookup_shas.each do |sha|
result[sha] = topic
end
end
end
result
end
def sync_commit_sha(commit_sha)
commit = github_repo.commit(commit_sha)
sync_commit(commit)
end
def sync_commit(commit)
topic_id = import_commit(commit)
import_comments(topic_id, commit[:hash])
topic_id
end
def import_commit(commit)
merged = github_repo.master_contains?(commit[:hash])
link = <<~LINK
[<small>GitHub</small>](https://github.com/#{github_repo.name}/commit/#{commit[:hash]})
LINK
title = commit[:subject]
# we add a unicode zero width joiner so code block is not corrupted
diff = commit[:diff].gsub('```', "`\u200d``")
truncated_message =
if commit[:diff_truncated]
"\n[... diff too long, it was truncated ...]\n"
end
body, linked_topics = auto_link_commits(commit[:body])
linked_topics.merge! find_linked_commits(title)
short_hash = "<small>sha: #{commit[:hash][0...8]}</small>"
raw = "[excerpt]\n#{body}\n[/excerpt]\n\n```diff\n#{diff}\n#{truncated_message}```\n#{link} #{short_hash}"
user = DiscourseCodeReview.github_user_syncer.ensure_user(
email: commit[:email],
name: commit[:name],
github_login: commit[:author_login],
github_id: commit[:author_id]
)
topic_id =
TopicCustomField
.where(
name: DiscourseCodeReview::CommitHash,
value: commit[:hash]
)
.limit(1)
.pluck(:topic_id)
.first
if topic_id.present?
if merged
topic = Topic.find(topic_id)
tags = topic.tags.pluck(:name)
merged_tags = [
SiteSetting.code_review_pending_tag,
SiteSetting.code_review_approved_tag,
SiteSetting.code_review_followup_tag
]
if (tags & merged_tags).empty?
tags << SiteSetting.code_review_pending_tag
tags -= [SiteSetting.code_review_unmerged_tag]
DiscourseTagging.tag_topic_by_names(
topic,
Discourse.system_user.guardian,
tags
)
end
end
else
tags =
if merged
[SiteSetting.code_review_pending_tag]
else
[SiteSetting.code_review_unmerged_tag]
end
post = PostCreator.create!(
user,
raw: raw,
title: title,
created_at: commit[:date],
category: category_id,
tags: tags,
skip_validations: true,
)
TopicCustomField.create!(
topic_id: post.topic_id,
name: DiscourseCodeReview::CommitHash,
value: commit[:hash]
)
linked_topics.values.each do |topik|
topik.add_moderator_post(
user,
" #{post.topic.url}",
bump: false,
post_type: Post.types[:small_action],
action_code: "followed_up"
)
end
topic_id = post.topic_id
end
topic_id
end
def import_comments(topic_id, commit_sha)
github_repo.commit_comments(commit_sha).each do |comment|
# skip if we already have the comment
unless PostCustomField.exists?(name: DiscourseCodeReview::GithubId, value: comment[:id])
login = comment[:login] || "unknown"
user = DiscourseCodeReview.github_user_syncer.ensure_user(name: login, github_login: login)
context = ""
if comment[:line_content]
context = <<~MD
[quote]
#{comment[:path]}
```diff
#{comment[:line_content]}
```
[/quote]
MD
end
custom_fields = { DiscourseCodeReview::GithubId => comment[:id] }
custom_fields[DiscourseCodeReview::CommentPath] = comment[:path] if comment[:path].present?
custom_fields[DiscourseCodeReview::CommentPosition] = comment[:position] if comment[:position].present?
PostCreator.create!(
user,
raw: context + comment[:body],
skip_validations: true,
created_at: comment[:created_at],
topic_id: topic_id,
custom_fields: custom_fields
)
end
end
end
end
end