-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-posts-prs
executable file
·237 lines (204 loc) · 6.21 KB
/
generate-posts-prs
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
#!/usr/bin/ruby
# encoding: utf-8
#
# Copyright 2021 Marc Dequènes (Duck)
#
# generate_post_{,frontmatter} came from planet_gen_posts.rb
# originally:
# Copyright 2014-2020 Gerald Bauer
# Released under CC0 1.0 Universal
# on https://github.com/feedreader/planet.rb
# Adapted by Red Hat OSPO
# to match post fields with minima theme template
# to return the post content without writing a file
# to move sanity checks in the posts loop
# Copyright 2021 Marc Dequènes (Duck)
require 'pluto/models'
require 'octokit'
require 'nokogiri'
require 'pp'
def generate_pr_branch_name(item)
item.published.strftime("%Y%m%dT%H%M%S") +
"_" +
item.title.parameterize
end
def generate_pr_title(item)
"[feed-moderation] #{item.title}"
end
def generate_post_path(item)
item.published.strftime(@post_path)
.sub('{title}', item.title.parameterize)
end
def generate_post_frontmatter(data)
frontmatter = ''
data.each do |key, value|
spaces = ' ' * (data.keys.map(&:length).max + 1 - key.length) unless value.is_a?(Array)
output = value
output = "\"#{value}\"" if value.is_a?(String)
output = "\n - \"#{value.join("\"\n - \"")}\"" if value.is_a?(Array)
frontmatter += "#{key}:#{spaces}#{output}\n"
end
frontmatter
end
def generate_post(item)
item.published = item.updated if item.published.nil?
content = item.content ? item.content : item.summary
data = {}
data["title"] = item.title.gsub('"','\"') unless item.title.empty?
#data["created_at"] = item.published if item.published
#data["updated_at"] = item.updated if item.updated
data["date"] = item.published if item.published
data["modified_date"] = item.updated if item.updated and data["modified_date"] != data["date"]
#data["guid"] = item.guid unless item.guid.empty?
data["author"] = item.feed.title unless item.feed.title.empty?
#data["avatar"] = item.feed.avatar if item.feed.avatar
#data["link"] = item.feed.link unless item.feed.link.empty?
#data["rss"] = item.feed.feed unless item.feed.feed.empty?
#data["tags"] = [ item.feed.location ? item.feed.location : "en" ]
#data["original_link"] = item.url if item.url
data["url"] = item.url if item.url
item.feed.author.split.each do |contact|
if contact.include?(':')
part = contact.split(':')
data[part.shift] = part.join(':')
else
data[contact] = true
end
end if item.feed.author
#data["original_link"] == data["link"] + data["original_link"] unless data["original_link"].include?('//')
data["url"] = data["link"] + data["url"] unless data["url"].include?('//')
post = "---\n"
post += generate_post_frontmatter(data)
post += "---\n"
# There were a few issues of incomplete html documents, nokogiri fixes that
html = Nokogiri::HTML::DocumentFragment.parse(content).to_html
# Liquid complains about curly braces
html.gsub!("{", "{")
html.gsub!("{", "}")
html.gsub!(/(?<=src=[\"\'])\/(?!\/)/, "#{/\/\/.*?(?=\/|$)/.match(item.feed.link)[0]}/")
html.gsub!(/(?<=src=[\"\'])https?:/, "")
post += html
post
end
if ARGV.count() != 2
puts "ERROR: Parameter(s) missing"
exit 1
end
@post_path = ARGV[0]
label_name = ARGV[1]
last_commit_sha = ENV.fetch('GITHUB_SHA')
puts "Last Commit SHA: #{last_commit_sha}"
unless ENV.has_key?('GITHUB_TOKEN')
puts "ERROR: Please provide GITHUB_TOKEN as environment variable"
exit 1
end
gh_client = Octokit::Client.new(
access_token: ENV.fetch("GITHUB_TOKEN"))
# get all paged results and concatenate automagically
gh_client.auto_paginate = true
# GITHUB_EVENT_PATH is unfortunately almost empty in schedule mode
repo_name = ENV.fetch("GITHUB_REPOSITORY")
default_branch = ENV.fetch("GITHUB_REF").split('/')[-1]
puts "Repo Name: #{repo_name}"
puts "Default Branch: #{default_branch}"
begin
Octokit.label(repo_name, label_name)
rescue
puts "Please create label #{label_name} with the color of your choice (a different name can be selected)"
end
# PRs are special issues in GH
prs = gh_client.list_issues(repo_name,
:labels => label_name,
:state => 'all',
:pulls => true
)
#puts "PRs:"
#pp prs
pr_titles_ids = Hash[prs.collect {|pr| [pr[:title], pr[:number]] }]
#puts "Found PRs:"
#pp pr_titles_ids
db_config = {
adapter: 'sqlite3',
database: './planet.db'
}
unless File.exists?(db_config[:database])
puts "ERROR: Database file '#{db_config[:database]}' is missing"
exit 1
end
Pluto.connect(db_config)
pr_gen_err = false
Pluto::Model::Item.latest.each_with_index do |item, i|
puts "[#{i+1}] {#{item.feed.title}} #{item.title}"
unless item.title &&
item.published &&
item.url &&
(item.content || item.summary)
puts " post missing basic info"
pr_gen_err = true
next
end
post = generate_post(item)
pr_title = generate_pr_title(item)
if pr_titles_ids.has_key? pr_title
puts " PR (#{pr_titles_ids[pr_title]}) already exist"
next
end
pr_branch_name = generate_pr_branch_name(item)
puts " Creating branch #{pr_branch_name}"
begin
gh_client.create_ref(
repo_name,
"heads/#{pr_branch_name}",
last_commit_sha
)
rescue
puts " ERROR: Cannot create branch, skipping"
pr_gen_err = true
next
end
post_path = generate_post_path(item)
puts " Creating commit with post into #{post_path}"
begin
commit = gh_client.create_contents(
repo_name,
post_path,
"Add post: #{item.title}\n\nfrom #{item.url}",
post,
:branch => pr_branch_name
)
rescue
puts " ERROR: Cannot create commit, skipping"
pr_gen_err = true
next
end
commit_sha = commit[:commit][:sha]
puts " Commit #{commit_sha} created"
puts " Creating PR with title: #{pr_title}"
begin
pr = gh_client.create_pull_request(
repo_name,
default_branch,
pr_branch_name,
pr_title
)
rescue
puts " ERROR: Cannot create PR, skipping"
pr_gen_err = true
next
end
pr_id = pr[:number]
puts " PR #{pr_id} was created"
puts " Add label #{label_name} to PR"
begin
gh_client.add_labels_to_an_issue(
repo_name,
pr_id,
[label_name]
)
rescue
puts " ERROR: Cannot add label to PR, skipping"
pr_gen_err = true
next
end
end
exit pr_gen_err ? 2 : 0