This repository was archived by the owner on Apr 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
243 lines (203 loc) · 8.12 KB
/
main.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
241
242
243
# frozen_string_literal: true
require 'base64'
require 'bundler/setup'
require 'faraday'
require 'json'
require 'logger'
require 'pry'
require 'sinatra'
require 'sinatra/reloader' if settings.development?
require 'yaml'
Bundler.require
Dotenv.load
class QiitaItemNotFoundError < StandardError; end
class CannotGetGitHubContentError < StandardError; end
post '/payload' do
verify_signature(request.body.read)
if !JSON.parse(params['payload'])['commits'] && JSON.parse(params['payload'])['zen']
log.debug('GitHub Webhook successfully added!')
log.debug("GitHub zen: #{JSON.parse(params['payload'])['zen']}")
return
end
JSON.parse(params['payload'])['commits'].each do |commit|
commit['added'].each do |new_file_path|
next unless ENV['INCLUDED_DIR']&.split(',')&.include?(File.dirname(new_file_path))
content, description = retrieve_content_and_description(new_file_path)
response_body = publish_to_qiita(content, YAML.safe_load(description), new_file_path, mode: :add)
map_filepath_with_qiita_item_id(new_file_path, response_body['id'])
end
commit['modified'].each do |new_file_path|
next unless ENV['INCLUDED_DIR']&.split(',')&.include?(File.dirname(new_file_path))
content, description = retrieve_content_and_description(new_file_path)
publish_to_qiita(content, YAML.safe_load(description), new_file_path, mode: :edit)
end
end
# TODO: implement deletion?
status 200
end
def retrieve_content_and_description(new_file_path)
connection = Faraday.new('https://api.github.com')
connection.headers['Accept'] = 'application/vnd.github.VERSION.raw'
connection.headers['Authorization'] = "token #{ENV['GITHUB_PERSONAL_ACCESS_TOKEN']}"
response = connection.get("/repos/#{ENV['GITHUB_REPOS']}/contents/#{new_file_path}")
unless response.status.to_s[0] == '2'
log.fatal("GitHub server returns status #{response.status}")
log.fatal("Reason: #{response.body}")
raise CannotGetGitHubContentError, "GitHub server returns status #{response.status}"
end
yaml_description = ''
yaml_to_be_trimmed = ''
if response.body.lines(chomp: true).first == '---'
response.body.each_line.with_index do |line, index|
if index.zero?
yaml_to_be_trimmed += line
next
end
if line =~ /^---/
yaml_to_be_trimmed += '---'
break
end
yaml_description += line
yaml_to_be_trimmed += line
end
end
content = response.body.gsub(/\A#{Regexp.escape(yaml_to_be_trimmed)}/, '').gsub(/\A\n+/, '')
[content, yaml_description]
end
def publish_to_qiita(content, description, new_file_path, mode: nil)
if mode.nil?
log.fatal("Argument `mode' is not specified")
raise ArgumentError, "Argument `mode' is not specified"
end
unless %i[add edit].include?(mode)
log.fatal("Argument `mode' must be :add or :edit")
raise ArgumentError, "Argument `mode' must be :add or :edit"
end
tags = []
description['topics'].each { |topic| tags.push("name": topic) }
request_url = case mode
when :add
'/api/v2/items'
when :edit
"/api/v2/items/#{qiita_item_id(new_file_path)}"
end
request_body = case mode
when :add
{
body: content.force_encoding('UTF-8'),
coediting: false,
group_url_name: nil,
private: private?(published: description['published']),
tags: tags,
title: description['title'],
tweet: !private?(published: description['published']) # only mode :add
}.to_json
when :edit
{
body: content.force_encoding('UTF-8'),
coediting: false,
group_url_name: nil,
private: private?(published: description['published']),
tags: tags,
title: description['title']
}.to_json
end
# TODO: write more smartly!
case mode
when :add
connection = Faraday.new('https://qiita.com')
response = connection.post do |request|
request.url(request_url)
request.headers['Authorization'] = "Bearer #{ENV['QIITA_ACCESS_TOKEN']}"
request.headers['Content-Type'] = 'application/json'
request.body = request_body
end
log.debug('Published article(s) to Qiita successfully!')
log.debug("Qiita item id: #{JSON.parse(response.body)['id']}")
when :edit
connection = Faraday.new('https://qiita.com')
response = connection.patch do |request|
request.url(request_url)
request.headers['Authorization'] = "Bearer #{ENV['QIITA_ACCESS_TOKEN']}"
request.headers['Content-Type'] = 'application/json'
request.body = request_body
end
log.debug('Modified article(s) on Qiita successfully!')
log.debug("Qiita item id: #{JSON.parse(response.body)['id']}")
end
JSON.parse(response.body)
end
def private?(published: false)
return true if settings.development?
!published
end
# Returns Hash
# {
# content: String or Nil: the content of file decoded Base64,
# sha: String or Nil: the SHA of file,
# exist: Boolean: if mapping file exists
# }
def retrieve_mapping_file
connection = Faraday.new('https://api.github.com')
connection.headers['Accept'] = 'application/vnd.github.v3+json'
connection.headers['Authorization'] = "token #{ENV['GITHUB_PERSONAL_ACCESS_TOKEN']}"
response = connection.get("/repos/#{ENV['GITHUB_REPOS']}/contents/#{ENV['MAPPING_FILEPATH']}")
mapping_file = if response.status == 404
{
content: nil,
sha: nil,
exist: false
}
else
{
content: Base64.decode64(JSON.parse(response.body)['content']),
sha: JSON.parse(response.body)['sha'],
exist: true
}
end
# GitHub will return response status 404 if a mapping file does not exist
# In that case, it will add a mapping file as a new file
# This will probably happen only the first time
if response.status.to_s[0] != '2' && response.status != 404
log.fatal("GitHub server returns status #{response.status}")
log.fatal("Reason: #{response.body}")
raise CannotGetGitHubContentError, "GitHub server returns status #{response.status}"
end
mapping_file
end
def map_filepath_with_qiita_item_id(filepath, item_id)
mapping_file = retrieve_mapping_file
connection = Faraday.new('https://api.github.com')
response = connection.put do |request|
request.url("/repos/#{ENV['GITHUB_REPOS']}/contents/#{ENV['MAPPING_FILEPATH']}")
request.headers['Accept'] = 'application/vnd.github.v3+json'
request.headers['Authorization'] = "token #{ENV['GITHUB_PERSONAL_ACCESS_TOKEN']}"
request.headers['Content-Type'] = 'application/json'
request_body = {
message: 'Update mapping file',
content: Base64.encode64("#{filepath}, #{item_id}\n#{mapping_file[:content]}")
}
request_body.merge!(sha: mapping_file[:sha]) if mapping_file[:sha]
request.body = request_body.to_json
end
return if response.status.to_s[0] == '2'
log.fatal("GitHub server returns status #{response.status}")
log.fatal("Reason: #{response.body}")
raise CannotGetGitHubContentError, "GitHub server returns status #{response.status}"
end
def qiita_item_id(filepath)
retrieve_mapping_file[:content]&.each_line do |line|
return line.split(',').last.gsub(/[\s\r\n]/, '') if line.include?(filepath)
end
log.fatal('Qiita item not found')
log.fatal(filepath)
raise QiitaItemNotFoundError, 'Qiita item not found'
end
def verify_signature(payload_body)
signature = "sha256=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), ENV['GITHUB_WEBHOOK_SECRET_TOKEN'], payload_body)}"
return halt 500, "Signatures didn't match!" unless Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE_256'])
end
def log
FileUtils.mkdir_p('logs') unless FileTest.exist?('logs')
Logger.new('logs/error.log', 'monthly')
end