-
Notifications
You must be signed in to change notification settings - Fork 15
/
pimper.rb
65 lines (52 loc) · 2.23 KB
/
pimper.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
module PimpMyChangelog
class Pimper
SEPARATOR = "<!--- The following link definition list is generated by PimpMyChangelog --->"
attr_reader :user, :project, :changelog
# @param user [String] Github user of this changelog
# @param project [String] Github project of this changelog
# @param changelog [String] The changelog
def initialize(user, project, changelog)
@user = user
@project = project
@changelog = changelog
end
# @return [String] The changelog with contributors and issues as link
def better_changelog
parsed_changelog = Parser.new(changelog)
# If the file doesn't have an extra newline at the end the separator gets rendered
# as part of the changelog. So add an extra newline in that case.
extra_newline_if_required = parsed_changelog.content.match(/\n\n\Z/) ? "" : "\n"
linkify_changelog(parsed_changelog.content) + extra_newline_if_required +
links_definitions(parsed_changelog.issues, parsed_changelog.contributors)
end
protected
# @param [String] changelog
# @return [String] The changelog with users and issues linkified.
#
# Example: "@pcreux closes issue #123"
# # => "[@pcreux][] closes issue [#123][]"
def linkify_changelog(changelog)
changelog.
gsub(ISSUE_NUMBER_REGEXP, '\1[#\2][]\3').
gsub(CONTRIBUTOR_REGEXP, '\1[@\2][]\3')
end
# The following regexp ensure that the issue or contributor is
# not wrapped between brackets (aka: is a link)
ISSUE_NUMBER_REGEXP = /(^|[^\[])#(\d+)($|[^\]])/
CONTRIBUTOR_REGEXP = /(^|[^\[])@([\w-]+)($|[^\]])/
# @param [Array] issues An array of issue numbers
# @param [Array] contributors An array of contributors github ids
#
# @return [String] A list of link definitions
def links_definitions(issues, contributors)
return '' if issues.empty? && contributors.empty?
issues_list = issues.map do |issue|
"[##{issue}]: https://github.com/#{user}/#{project}/issues/#{issue}"
end
contributors_list = contributors.map do |contributor|
"[@#{contributor}]: https://github.com/#{contributor}"
end
([SEPARATOR] + issues_list + contributors_list).join("\n")
end
end
end