forked from rubygems/rubygems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_changelog.rb
67 lines (57 loc) · 1.75 KB
/
update_changelog.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
#!/usr/bin/env ruby
require 'json'
require 'net/http'
def access_token_query_string
@access_token_query_string ||= begin
token = ENV['GITHUB_API_TOKEN']
token ? "?access_token=#{token.strip}" : ''
end
end
def github_api(path)
base = 'https://api.github.com'
url = path.start_with?(base) ? path : base + path
url += access_token_query_string
uri = URI.parse(url)
return unless response = Net::HTTP.get_response(uri)
JSON.load(response.body)
end
def wrap(text, length, indent)
result = []
work = text.dup
while work.length > length
if work =~ /^(.{0,#{length}})[ \n]/o
result << $1
work.slice!(0, $&.length)
else
result << work.slice!(0, length)
end
end
result << work unless work.empty?
result = result.reduce(String.new) do |acc, elem|
acc << "\n" << ' ' * indent unless acc.empty?
acc << elem
end
result += "\n" unless result.end_with?("\n")
result
end
def sentence(text)
text = text[0].upcase + text[1..-1]
text.end_with?('.') ? text : text << '.'
end
branch = ARGV.shift || "HEAD"
history = File.read(File.expand_path('../../History.txt', __FILE__))
File.open(File.expand_path('../../ChangeLog', __FILE__), 'w') do |changelog|
commits = `git log --oneline v#{Gem::VERSION}..#{branch}`.split("\n")
prs = commits.reverse_each.map { |c| c =~ /(Auto merge of|Merge pull request) #(\d+)/ && $2 }.compact
prs.each do |pr|
next if history =~ /Pull\srequest\s##{pr}/m
details = github_api "/repos/rubygems/rubygems/pulls/#{pr}"
title, user = details.values_at('title', 'user')
user = github_api(user['url'])
name = user['name'] || user['login']
changelog << wrap(
['*', sentence(title), sentence("Pull request ##{pr} by #{name}")].join(' '),
74, 2
)
end
end