-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy path_this-week-in-rails.rb
More file actions
executable file
·154 lines (122 loc) · 3.74 KB
/
_this-week-in-rails.rb
File metadata and controls
executable file
·154 lines (122 loc) · 3.74 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# Generate a new "This Week in Rails" blog post.
#
# Start date is exactly 7 days from today.
#
# Usage:
#
# _this-week-in-rails.rb [AUTHOR] [AUTHOR_URL]
#
# * AUTHOR: (optional) Should be your GitHub username, or profile name, default to $USER
# * AUTHOR_URL: (optional) a URL to link to your profile, default to github/author
#
# Example:
#
# _this-week-in-rails.rb zzak https://ruby.social/@zzak
#
author = ARGV[0] || ENV["USER"]
author_url = ARGV[1] || "https://github.com/#{author}"
days_ago = ENV["DAYS_AGO"]&.to_i || 7
require 'uri'
require 'open-uri'
require 'json'
require 'date'
require 'nokogiri'
end_date = Date.today
start_date = end_date - days_ago
class Contributors
attr_accessor :body, :start_date, :end_date, :total
def initialize(start_date, end_date)
@start_date = start_date.strftime("%Y%m%d")
@end_date = end_date.strftime("%Y%m%d")
@body = fetch
@total = extract_total
end
def url
"https://contributors.rubyonrails.org/contributors/in-time-window/#{@start_date}-#{@end_date}"
end
def fetch
uri = URI.parse(url)
body = uri.open.read
return Nokogiri::HTML(body)
end
def extract_total
xpath = "//span[@class=\"listing-total\"][1]"
text = @body.xpath(xpath.to_s).first.content
return text.match(/\d+/)
end
end
def fetch_merged_prs(start_date, end_date, page)
url = "https://api.github.com/search/issues?q=is:pr+repo:rails/rails+merged:#{start_date}..#{end_date}&page=#{page}"
body = URI.parse(url).open.read
JSON.parse(body)
end
def extract_intro_markdown(body)
text = body.to_s.dup
text = text.gsub("\r\n", "\n").gsub("\r", "\n")
text = text.gsub(/<!--.*?-->/m, "")
parts = text.split(/^\s*#+\s*Details?\b.*$/i, 2)
text = parts[1] || parts[0]
if text
text = text.gsub(/^\s*#+\s*Motivation \/ Background\s*\n+/i, "")
text = text.gsub(/^\s*#+\s*Details?\s*\n+/i, "")
text = text.split(/^\s*#+\s*Checklist\b.*$/i, 2)[0]
text = text&.strip
text = text&.gsub(/\n{3,}/, "\n\n")
end
text
end
def format_for_post(markdown)
cleaned = markdown.to_s.strip
return "" if cleaned.empty?
lines = cleaned.split("\n")
lines.map(&:rstrip).join("\n")
end
post_content = []
contributors = Contributors.new(start_date, end_date)
total_count = fetch_merged_prs(start_date, end_date, 1)["total_count"].to_f
total_pages = total_count / 30.0
current_page = 1
while total_pages >= 0.0
data = fetch_merged_prs(start_date, end_date, current_page)
data["items"].each do |item|
summary = extract_intro_markdown(item["body"])
# The two spaces before line-breaks creates a soft-break in the Rails website.
post_content << <<~POST
[#{item["title"]}](#{item["html_url"]})#{" "}
#{format_for_post(summary)}
POST
end
current_page += 1
total_pages -= 1.0
end
title_date = end_date.strftime("%B %-d, %Y")
meta = %(---
layout: post
title: "This Week in Rails: #{title_date}"
categories: news
author: #{author}
og_image: assets/images/this-week-in-rails.png
published: true
date: #{end_date}
---
)
header = %(
Hi, it's [#{author}](#{author_url}). Let's explore this week's changes in the Rails codebase.
)
footer = %(
_You can view the whole list of changes [here](https://github.com/rails/rails/compare/@%7B#{start_date}%7D...main@%7B#{end_date}%7D)._#{" "}
_We had [#{contributors.total} contributors](#{contributors.url}) to the Rails codebase this past week!_
Until next time!#{" "}
_[Subscribe](https://world.hey.com/this.week.in.rails) to get these updates mailed to you._
)
post_path = "_posts/#{end_date}-this-week-in-rails.markdown"
File.open(post_path, 'w') do |f|
f.write meta
f.write header
f.write post_content.join("")
f.write footer
end
#system "#{ENV['EDITOR'] || 'open'} #{post_path}"