forked from cpicot/gyro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
90 lines (80 loc) · 2.06 KB
/
Rakefile
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
namespace :pr do
desc 'Check incoming PRs on CI'
task :check do
if ENV['CIRCLE_PULL_REQUEST'].nil? || ENV['CIRCLE_PULL_REQUEST'].empty?
warn 'Not part of a Pull Request, so nothing to check in this task'
next
end
if ENV['CI']
sh 'git fetch origin +master:master 2>/dev/null'
else
sh 'git fetch origin master 2>/dev/null'
end
modified_files = `git diff --name-only HEAD master`.split("\n")
puts '---'
if modified_files.include?('CHANGELOG.md')
info 'CHANGELOG.md modified.'
Rake::Task['changelog:check'].invoke
else
warn 'Please include an entry in CHANGELOG.md to describe the changes and credit yourself!'
warn 'Note: use 2 spaces after the last line describing your changes, ' \
'then add links to your GitHub account & the PR for attribution'
exit 1
end
end
end
namespace :changelog do
desc 'Validates the CHANGELOG format'
task :check do
previous_type = :empty
failures = []
puts 'Linting CHANGELOG.md...'
File.open('CHANGELOG.md').each_line.with_index do |line, idx|
line.chomp!
type = line_type(line)
case type
when :li
failures << [line, idx, 'Please end description lines with a period and 2 spaces'] unless line.end_with?('. ')
when :link
if %i[li link].include?(previous_type)
failures << [line, idx, 'Please indent links with 2 spaces'] unless line.start_with?(' ')
end
end
previous_type = type
end
if failures.empty?
info 'CHANGELOG.md OK.'
else
warn 'Offenses detected:'
failures.each do |f|
error ' > ' + f[0]
error " Line #{f[1]}: #{f[2]}"
puts ''
end
exit 1
end
end
end
## Helper functions ##
def line_type(line)
return :empty if line.strip.empty?
case line
when /^\#* /
:header
when /^\* /
:li
when /\[.*\]\(.*\)/
:link
else
:other
end
end
def info(str)
puts 'ℹ️' + str
end
def warn(str)
puts '⚠️ ' + str
end
def error(str)
puts '🛑 ' + str
end