-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.rb
executable file
·121 lines (100 loc) · 3.19 KB
/
action.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
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
# frozen_string_literal: true
require 'date'
require 'pry'
require 'slack-notifier'
require 'yaml'
def pretty_print(hash)
Pry::ColorPrinter.pp(hash)
end
def parse_event
event_path = ENV.fetch('GITHUB_EVENT_PATH')
file = File.open(event_path)
parsed_event = YAML.safe_load(file)
puts 'GitHub event is:'
pretty_print(parsed_event)
parsed_event
end
def meaningful_commits(parsed_event)
parsed_event.fetch('commits').reject do |commit|
commit.fetch('message').start_with?('Merge pull request')
end.reject do |commit|
commit.dig('author', 'name') == 'Travis CI'
end
end
def section(text)
{
type: :section,
text: {
type: :mrkdwn,
text: text
}
}
end
def markdown_to_slack(text)
regexp = Regexp.new(/([^\[]+)\[([^\[]+)\]\(([^)]+)\)(.*)/)
if regexp =~ text
"#{Regexp.last_match(1)}<#{Regexp.last_match(3)}|#{Regexp.last_match(2)}>#{Regexp.last_match(4)}"
else
text
end
end
webhook = ENV.fetch('SLACK_WEBHOOK')
if webhook.nil?
warn 'Error: SLACK_WEBHOOK is not configured.'
return
end
parsed_event = parse_event
commits = meaningful_commits(parsed_event)
if commits.empty?
puts 'Nothing to do, there are no commits!'
return
else
puts 'Commits are:'
pretty_print(commits)
end
pretty_date = DateTime.now.strftime('%A %B %d, %Y')
repository_name = parsed_event.dig('repository', 'name')
repository_url = parsed_event.dig('repository', 'html_url')
compare_url = parsed_event.fetch('compare')
header = "*A new version of <#{repository_url}|#{repository_name}> has been released!* <#{compare_url}|See commits :octocat:>"
changelog_intro = if commits.one?
'The only change is:'
else
"The #{commits.size} individual changes are:"
end
blocks = []
blocks << section(pretty_date)
blocks << section(header)
blocks << section(changelog_intro)
commits.each do |commit|
message_lines = commit.fetch('message')
.gsub(/\[no *ticket\]/i, '')
.gsub(/\[travis *skip\]/i, '')
.gsub(/\[ci *skip\]/i, '')
.lines
.map(&:strip)
summary = message_lines.shift
message_lines.shift # Discard the blank line between summary and details
author = commit.dig('author', 'name')
change = nil
if ['dependabot-preview[bot]', 'dependabot[bot]'].include?(author)
summary = markdown_to_slack(message_lines.first)
summary.sub!(/Bump /, ':hammer_and_wrench: Upgrades library ')
change = "#{summary} _(:robot_face: Dependabot)_\n"
release_notes = message_lines.select { |line| line.match?('Release notes') }.first
change += "#{markdown_to_slack(release_notes)}\n" if release_notes
change_log = message_lines.select { |line| line.match?('Changelog') }.first
change += "#{markdown_to_slack(change_log)}\n" if change_log
else
summary.gsub!(/#(\d+)/, "<#{repository_url}/issues/\\1|#\\1>")
change = "#{summary} _(#{author})_\n"
message_lines.each { |line| change += "> #{line}\n" }
end
blocks << section(change)
end
puts "\n\nBlocks:\n"
pretty_print(blocks)
puts 'Notifying Slack'
notifier = Slack::Notifier.new(webhook)
notifier.post(blocks: blocks)
puts 'Done'