forked from cucumber/cucumber-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-changelog
executable file
·81 lines (64 loc) · 1.96 KB
/
update-changelog
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
#!/usr/bin/env ruby
require 'octokit'
class PullRequest
attr_reader :gh, :num
def initialize(gh, num)
@gh = gh
@num = num
end
def entry
pr = gh.pull_request('cucumber/cucumber-ruby', num)
username = pr['user']['login']
url = pr['html_url']
title = pr['title']
"#{title} ([##{num}](#{url}) [@#{username}](https://github.com/#{username}))"
end
def labels
gh.labels_for_issue('cucumber/cucumber-ruby', num).map { |label| label['name'] }
end
def type_label
types = labels.select { |label| label =~ /^type: / }
raise('This pull request does not have a \'type\' label on it. Please add one.') if types.empty?
raise('This pull request has more than one \'type\' label on it. Please choose just one.') if types.length > 1
types.first
end
end
class Changelog
def self.open(path, &block)
full_path = File.expand_path("#{File.dirname(__FILE__)}/#{path}/CHANGELOG.md")
log = new(File.read(full_path))
log.instance_exec(&block)
File.write(full_path, log.body)
end
attr_reader :lines
def initialize(body)
@lines = body.lines
end
def insert(label, entry)
section_index = lines.index { |line| line.strip == heading_for(label) }
raise("Unable to find matching heading in CHANGELOG.md for '#{label}' (#{heading_for(label)})") unless section_index
lines.insert(section_index + 2, "* #{entry}\n")
end
def heading_for(label)
text = {
'type: new feature' => 'Added',
'type: bug' => 'Fixed',
'type: refactoring / developer experience' => 'Changed'
}.fetch(label)
"### #{text}"
end
def body
lines.join
end
end
begin
token = ENV['GITHUB_TOKEN'] || raise('You need to set GITHUB_TOKEN')
num = (ARGV[0] || raise("syntax: #{$PROGRAM_NAME} <PULL-REQUEST-NUMBER>")).to_i
gh = Octokit::Client.new(access_token: token)
pr = PullRequest.new(gh, num)
Changelog.open('..') do
insert(pr.type_label, pr.entry)
end
rescue StandardError => e
abort e.message
end