/
plugin.rb
134 lines (106 loc) · 4.2 KB
/
plugin.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
# name: discourse-code-review
# about: use discourse for after the fact code reviews
# version: 0.1
# authors: Sam Saffron
# url: https://github.com/discourse/discourse-code-review
# match version in discourse dev
begin
require 'octokit'
rescue LoadError
gem 'octokit', '4.9.0'
end
enabled_site_setting :code_review_enabled
require_dependency 'auth/github_authenticator'
module HackGithubAuthenticator
def after_authenticate(auth_token, existing_account: nil)
result = super(auth_token, existing_account: existing_account)
if SiteSetting.code_review_enabled
if user_id = result.user&.id
token = auth_token.credentials.token
user = result.user
user.custom_fields[DiscourseCodeReview::UserToken] = token
user.custom_fields[DiscourseCodeReview::GithubId] = auth_token[:uid]
user.custom_fields[DiscourseCodeReview::GithubLogin] = auth_token.info.nickname
user.save_custom_fields
end
end
result
end
def register_middleware(omniauth)
scope = "user:email"
if SiteSetting.code_review_enabled
scope = "user:email,repo"
end
scope = "user:email,repo"
omniauth.provider :github,
setup: lambda { |env|
strategy = env["omniauth.strategy"]
strategy.options[:client_id] = SiteSetting.github_client_id
strategy.options[:client_secret] = SiteSetting.github_client_secret
},
scope: scope
end
end
class ::Auth::GithubAuthenticator
prepend HackGithubAuthenticator
end
after_initialize do
if !SiteSetting.tagging_enabled
Rails.logger.warn("The code review plugin requires tagging, enabling it!")
SiteSetting.tagging_enabled = true
end
module ::DiscourseCodeReview
PluginName = 'discourse-code-review'
class Engine < ::Rails::Engine
engine_name 'code-review'
isolate_namespace DiscourseCodeReview
end
UserToken = 'github user token'
CommitHash = 'commit hash'
GithubId = 'github id'
GithubLogin = 'github login'
CommentPath = 'comment path'
CommentPosition = 'comment position'
def self.octokit_client
client = Octokit::Client.new
if username = SiteSetting.code_review_api_username.presence
username = username.downcase
id = User.where(username_lower: username).pluck(:id).first
if id && (token = UserCustomField.where(user_id: id, name: DiscourseCodeReview::UserToken).pluck(:value).first)
client = Octokit::Client.new(access_token: token)
end
end
client
end
end
require File.expand_path("../app/controllers/discourse_code_review/code_review_controller.rb", __FILE__)
require File.expand_path("../lib/discourse_code_review/importer.rb", __FILE__)
require File.expand_path("../lib/discourse_code_review/github_repo.rb", __FILE__)
DiscourseCodeReview::Engine.routes.draw do
post '/approve' => 'code_review#approve'
post '/followup' => 'code_review#followup'
post '/webhook' => 'code_review#webhook'
end
Discourse::Application.routes.append do
mount ::DiscourseCodeReview::Engine, at: '/code-review'
end
on(:post_process_cooked) do |doc, post|
if post.post_number > 1 && !post.whisper? && post.raw.present? && (topic = post.topic) && (hash = topic.custom_fields[DiscourseCodeReview::CommitHash])
if !post.custom_fields[DiscourseCodeReview::GithubId] && post.user
if token = post.user.custom_fields[DiscourseCodeReview::UserToken]
client = Octokit::Client.new(access_token: token)
fields = post.reply_to_post&.custom_fields || {}
path = fields[DiscourseCodeReview::CommentPath]
position = fields[DiscourseCodeReview::CommentPosition]
if repo = post.topic.category.custom_fields[DiscourseCodeReview::Importer::GithubRepoName]
comment = client.create_commit_comment(repo, hash, post.raw, path, nil, position)
post.custom_fields[DiscourseCodeReview::GithubId] = comment.id
post.custom_fields[DiscourseCodeReview::CommentPath] = path if path.present?
post.custom_fields[DiscourseCodeReview::CommentPosition] = position if position.present?
post.save_custom_fields
end
end
end
end
end
end