capistrano-deploybot 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bf754872b2f6cde26e645b0448f48a87fc571d37
4
- data.tar.gz: 2fa2c733485283b7bc04b3428f04d010c91875cd
2
+ SHA256:
3
+ metadata.gz: 25431135f57c0aee8ff3ee5ef54b004c52091854e085200b7bf89ae48e06c6d0
4
+ data.tar.gz: 2e959245f4d1c7efeff6cd7506818c0ad3b98c5238cbf7c3dca78b6894fa0424
5
5
  SHA512:
6
- metadata.gz: 16ee73ad1c4b0e5c8a43f15d3ac82321b00c3ebadc5c3ee0b20cedde627f6e6a851d86cffe84aa6ec152069d52858f42c50cf497fa55b3c11dfb836f7fcf9444
7
- data.tar.gz: 8df28709ee648204afcbdb54ebe1676fb4f35b0f67a76ded42c07736f477c89d3dfb4c7badd199159450adc066816902e221875d024021b308a78c94fbd170de
6
+ metadata.gz: ce5932e0b2928274558fa9cb2889d5767b4c7af8bd8bb36d9ca014e91c81aeef7b1fa38b4b41c62df7cb041d66a56649cf3b53991ddaef32b967565b093bfb3f
7
+ data.tar.gz: 2d6544c283bf76a93b09d421d19e22bb67f9ed37b2d98319d5d52d193b8f889c1615fa8b3e168944f2588c46422458d2972df9afa5a098bfb8f65fc67744ef71
@@ -1,3 +1,4 @@
1
+ require 'date'
1
2
  require File.expand_path('../lib/capistrano-deploybot/version', __FILE__)
2
3
 
3
4
  Gem::Specification.new do |s|
@@ -7,9 +7,16 @@ module CapistranoDeploybot
7
7
  class Capistrano
8
8
  DEFAULT_OPTIONS = {
9
9
  username: :autodeploy,
10
- environments: %w(staging)
10
+ environments: {
11
+ staging: {
12
+ slack_webhooks: [],
13
+ jira_webhooks: []
14
+ }
15
+ }
11
16
  }
12
17
 
18
+ JIRA_TICKET_ID_REGEXP = /[A-Z]{2,}-\d+/
19
+
13
20
  def initialize(env)
14
21
  @env = env
15
22
  @opts = DEFAULT_OPTIONS.merge(fetch(:deploybot, {}))
@@ -18,53 +25,89 @@ module CapistranoDeploybot
18
25
  def run
19
26
  current_revision = fetch(:current_revision)
20
27
  previous_revision = fetch(:previous_revision)
21
- rails_env = fetch(:rails_env)
28
+ rails_env = fetch(:rails_env).to_sym
29
+
30
+ return if !@opts[:environments].keys.include?(rails_env) || current_revision == previous_revision
31
+
32
+ @opts[:environments].fetch(rails_env).fetch(:slack_webhooks).tap do |slack_webhooks|
33
+ notify_slack(slack_webhooks, current_revision, previous_revision)
34
+ end
22
35
 
23
- return if !@opts[:environments].include?(rails_env.to_s) || current_revision == previous_revision
36
+ @opts[:environments].fetch(rails_env).fetch(:jira_webhooks).tap do |jira_webhooks|
37
+ notify_jira(jira_webhooks, current_revision, previous_revision)
38
+ end
39
+ end
40
+
41
+ private
24
42
 
43
+ def notify_slack(webhooks, current_revision, previous_revision)
25
44
  application_name = @opts[:application_name]
26
- deploy_target = deploy_target(rails_env, application_name)
45
+ deploy_target = deploy_target(fetch(:rails_env), application_name)
46
+
27
47
  payload = {
28
48
  username: @opts[:username],
29
49
  text: payload_text(current_revision, previous_revision, deploy_target)
30
50
  }
31
51
 
32
- @opts[:webhooks].each do |webhook|
33
- post_to_webhook(payload.merge(webhook: webhook))
52
+ webhooks.each do |webhook|
53
+ post_to_webhook(webhook, payload)
34
54
  end
55
+
56
+ @env.info('[deploybot] Notified Slack webhooks.')
35
57
  end
58
+
59
+ def notify_jira(webhooks, current_revision, previous_revision)
60
+ jira_issues = extract_jira_ids_from_commits(current_revision, previous_revision)
61
+ return if jira_issues.empty?
36
62
 
37
- private
63
+ payload = {
64
+ issues: jira_issues
65
+ }
66
+
67
+ webhooks.each do |webhook|
68
+ post_to_webhook(webhook, payload)
69
+ end
70
+
71
+ @env.info("[deploybot] Notified JIRA webhooks with tickets: #{jira_issues.join(', ')}")
72
+ end
38
73
 
39
74
  def deploy_target(rails_env, application_name)
40
75
  application_name.nil? ? rails_env : "#{application_name} (#{rails_env})"
41
76
  end
42
77
 
78
+ def extract_jira_ids_from_commits(current_revision, previous_revision)
79
+ commits = `git show --pretty=format:%s -s #{previous_revision}..#{current_revision}`
80
+ commits.split("\n").map { |s| s.scan(JIRA_TICKET_ID_REGEXP) }.flatten.uniq
81
+ end
82
+
43
83
  def payload_text(current_revision, previous_revision, deploy_target)
44
84
  "Deployed to #{deploy_target}:\n" + `git shortlog #{previous_revision}..#{current_revision}`
45
85
  end
46
86
 
47
- def post_to_webhook(payload)
87
+ def post_to_webhook(url, payload)
48
88
  begin
49
- response = post_to_slack_as_webhook(payload)
89
+ response = perform_http_request(url, payload)
50
90
  rescue => e
51
- backend.warn("[deploybot] Error notifying Slack!")
52
- backend.warn("[deploybot] Error: #{e.inspect}")
91
+ @env.warn("[deploybot] Error sending request to webhook #{url}")
92
+ @env.warn("[deploybot] Error: #{e.inspect}")
53
93
  end
54
94
 
55
95
  if response && response.code !~ /^2/
56
- warn("[deploybot] Slack API Failure!")
57
- warn("[deploybot] URI: #{response.uri}")
58
- warn("[deploybot] Code: #{response.code}")
59
- warn("[deploybot] Message: #{response.message}")
60
- warn("[deploybot] Body: #{response.body}") if response.message != response.body && response.body !~ /<html/
96
+ @env.warn("[deploybot] Webhook API Failure!")
97
+ @env.warn("[deploybot] URI: #{response.uri}")
98
+ @env.warn("[deploybot] Code: #{response.code}")
99
+ @env.warn("[deploybot] Message: #{response.message}")
100
+ @env.warn("[deploybot] Body: #{response.body}") if response.message != response.body && response.body !~ /<html/
61
101
  end
62
102
  end
63
103
 
64
- def post_to_slack_as_webhook(payload = {})
65
- params = { payload: payload.to_json }
66
- uri = URI(payload[:webhook])
67
- Net::HTTP.post_form(uri, params)
104
+ def perform_http_request(url, payload)
105
+ uri = URI(url)
106
+ http = Net::HTTP.new(uri.host, uri.port)
107
+ http.use_ssl = true
108
+ req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
109
+ req.body = payload.to_json
110
+ http.request(req)
68
111
  end
69
112
  end
70
113
  end
@@ -2,10 +2,13 @@ namespace :slack do
2
2
  namespace :deploy do
3
3
  desc 'Notify about updated deploy'
4
4
  task :updated do
5
- CapistranoDeploybot::Capistrano.new(self).run
5
+ on roles(:web) do
6
+ within release_path do
7
+ CapistranoDeploybot::Capistrano.new(self).run
8
+ end
9
+ end
6
10
  end
7
11
  end
8
12
  end
9
13
 
10
- # before 'deploy', 'slack:deploy:updated'
11
14
  after 'deploy:finishing', 'slack:deploy:updated'
@@ -1,3 +1,3 @@
1
1
  module CapistranoDeploybot
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-deploybot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kamil Baćkowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-10 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano
@@ -54,8 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubyforge_project:
58
- rubygems_version: 2.4.6
57
+ rubygems_version: 3.1.2
59
58
  signing_key:
60
59
  specification_version: 4
61
60
  summary: Capistrano deploy integration for slack