forked from jonnyfairbanks/slackup-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_with_images.rb
executable file
·104 lines (84 loc) · 2.44 KB
/
template_with_images.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
# frozen_string_literal: true
require "json"
require "net/http"
require "uri"
class SlackUp
AUTHOR_NAME = "YOUR-NAME"
AUTHORIZATION_TOKEN = "YOUR-GITHUB-API-AUTH-TOKEN"
GITHUB_API_BASE = "https://api.github.com/repos/acima-credit/merchant_portal/pulls"
GITHUB_WEB_BASE = "https://github.com/acima-credit/merchant_portal/pull"
JIRA_BASE = "https://jira.smpl.ch/browse"
JIRA_ID_REGEX = /Ticket.*jira.smpl.ch\/browse\/([\w-]+).*## Description/m
SLACK_WEB_HOOK = "https://hooks.slack.com/services/YOUR-SLACK-WEBHOOK"
def self.run
prompt_for_input
format_output
confirm_post
end
def self.prompt_for_input
print "PR ID: "
@pr_id = gets.chomp
print "Deploy: "
@deploy_message = gets.chomp
print "Update: "
@update_message = gets.chomp
end
def self.format_output
url = "#{GITHUB_API_BASE}/#{@pr_id}"
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new uri
request["Authorization"] = "token #{AUTHORIZATION_TOKEN}"
request["content-type"] = "application/json"
response = http.request(request)
parsed_response = JSON.parse(response.body)
title = parsed_response['title']
body = parsed_response['body']
jira_id = body[JIRA_ID_REGEX, 1]
@slackup_post = <<~END
*Ticket*: #{JIRA_BASE}/#{jira_id}
*PR*: #{GITHUB_WEB_BASE}/#{@pr_id}
*Title*: #{title}
*Deploy*: #{@deploy_message}
*Update*: #{@update_message}
END
end
def self.confirm_post
puts "\n----\n"
puts @slackup_post
print "Would you like to post to slack? (y/n): "
response = gets.chomp
if response.downcase == "y"
image = image_list.sample
uri = URI.parse(SLACK_WEB_HOOK)
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = JSON.dump(
"attachments": [
{
author_name: AUTHOR_NAME,
text: @slackup_post,
thumb_url: image,
}
],
)
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
elsif response.downcase == "n"
puts "Aborted"
else
puts "Invalid entry aborted."
end
end
def self.image_list
%w(
LINKS_TO_YOUR_IMAGES
)
end
end
SlackUp.run