capistrano-deploybot 0.0.1 → 1.1.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: 05b4947014385caab09399ef8ddc9c99746d6ace
4
- data.tar.gz: 2b2b36cf0cb891aac9f4cf38a94afbabfc4cbce9
2
+ SHA256:
3
+ metadata.gz: e7681a63cd918d0df99e11d05c571104614061fe9bd9a24d77909b0ff8c76891
4
+ data.tar.gz: 64367a40e53aedbcaf9a51708b2ea52925460e2744a4757608ba5bd3451b9861
5
5
  SHA512:
6
- metadata.gz: 788b096095f3bcf20efa822fbf41218748811e702e6ece93e92d7b9babafe5246751a0a5e61e24fab0c0c4e63652ead94be6e33351a12ffa478cc74cc4a860f8
7
- data.tar.gz: 6a429f9b1b5e5a39be80dd0b5b563abc542868979316a162382c1d0d2b42eae1f2e6f4975390192e8fe3f4761b76ec6e57581673e474de12967e72afe70bd6d8
6
+ metadata.gz: 688b536e9c4b7fa738a8fa35bf8798b2727386138b40ea88f79db9a56d4ea87107779dfcfb529ca7108f62bf9e1a0451f66e64d0d17caa856d33c7606a3cd092
7
+ data.tar.gz: 9eb32b67d54606a4f5faaf71020f46538ba6d25313a37847eaafac9d797c9b377656faa8a78915deea5433a7501698b18086820d510e548b7cfa037f16279558
@@ -1,14 +1,17 @@
1
+ require 'date'
2
+ require File.expand_path('../lib/capistrano-deploybot/version', __FILE__)
3
+
1
4
  Gem::Specification.new do |s|
2
5
  s.name = 'capistrano-deploybot'
3
- s.version = '0.0.1'
6
+ s.version = CapistranoDeploybot::VERSION
4
7
  s.homepage = 'https://github.com/kbackowski/capistrano-deploybot'
5
8
  s.description = 'Notify slack channel with list of deployed commits'
6
- s.date = '2017-03-07'
9
+ s.date = Date.today.to_s
7
10
  s.summary = 'Capistrano deploy integration for slack'
8
11
  s.authors = ['Kamil Baćkowski']
9
12
  s.email = 'kbackowski@gmail.com'
10
13
  s.files = `git ls-files`.split($/)
11
14
  s.license = 'MIT'
12
15
 
13
- s.add_dependency 'capistrano', '>= 3.5.0'
16
+ s.add_dependency 'capistrano', '~> 3.5'
14
17
  end
@@ -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,47 +25,95 @@ 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
22
31
 
23
- return if !@opts[:environments].include?(rails_env.to_s) || current_revision == previous_revision
32
+ @opts[:environments].fetch(rails_env).fetch(:slack_webhooks).tap do |slack_webhooks|
33
+ notify_slack(slack_webhooks, current_revision, previous_revision)
34
+ end
24
35
 
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
42
+
43
+ def notify_slack(webhooks, current_revision, previous_revision)
44
+ application_name = @opts[:application_name]
45
+ deploy_target = deploy_target(fetch(:rails_env), application_name)
46
+
25
47
  payload = {
26
48
  username: @opts[:username],
27
- text: payload_text(current_revision, previous_revision, rails_env)
49
+ text: payload_text(current_revision, previous_revision, deploy_target)
28
50
  }
29
51
 
30
- @opts[:webhooks].each do |webhook|
31
- post_to_webhook(payload.merge(webhook: webhook))
52
+ webhooks.each do |webhook|
53
+ post_to_webhook(webhook, payload)
32
54
  end
55
+
56
+ @env.info('[deploybot] Notified Slack webhooks.')
33
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?
34
62
 
35
- private
63
+ release_tag = ENV['CI_COMMIT_TAG']
64
+
65
+ payload = {
66
+ issues: jira_issues,
67
+ data: { releaseVersion: release_tag }
68
+ }
69
+
70
+ webhooks.each do |webhook|
71
+ post_to_webhook(webhook, payload)
72
+ end
73
+
74
+ message = "[deploybot] Notified JIRA webhooks with tickets: #{jira_issues.join(', ')}"
75
+ message << " and release: #{release_tag}" if release_tag
76
+
77
+ @env.info(message)
78
+ end
79
+
80
+ def deploy_target(rails_env, application_name)
81
+ application_name.nil? ? rails_env : "#{application_name} (#{rails_env})"
82
+ end
83
+
84
+ def extract_jira_ids_from_commits(current_revision, previous_revision)
85
+ commits = `git show --pretty=format:%s -s #{previous_revision}..#{current_revision}`
86
+ commits.split("\n").map { |s| s.scan(JIRA_TICKET_ID_REGEXP) }.flatten.uniq
87
+ end
36
88
 
37
- def payload_text(current_revision, previous_revision, rails_env)
38
- "Deployed to #{rails_env}:\n" + `git shortlog #{previous_revision}..#{current_revision}`
89
+ def payload_text(current_revision, previous_revision, deploy_target)
90
+ "Deployed to #{deploy_target}:\n" + `git shortlog #{previous_revision}..#{current_revision}`
39
91
  end
40
92
 
41
- def post_to_webhook(payload)
93
+ def post_to_webhook(url, payload)
42
94
  begin
43
- response = post_to_slack_as_webhook(payload)
95
+ response = perform_http_request(url, payload)
44
96
  rescue => e
45
- backend.warn("[deploybot] Error notifying Slack!")
46
- backend.warn("[deploybot] Error: #{e.inspect}")
97
+ @env.warn("[deploybot] Error sending request to webhook #{url}")
98
+ @env.warn("[deploybot] Error: #{e.inspect}")
47
99
  end
48
100
 
49
101
  if response && response.code !~ /^2/
50
- warn("[deploybot] Slack API Failure!")
51
- warn("[deploybot] URI: #{response.uri}")
52
- warn("[deploybot] Code: #{response.code}")
53
- warn("[deploybot] Message: #{response.message}")
54
- warn("[deploybot] Body: #{response.body}") if response.message != response.body && response.body !~ /<html/
102
+ @env.warn("[deploybot] Webhook API Failure!")
103
+ @env.warn("[deploybot] URI: #{response.uri}")
104
+ @env.warn("[deploybot] Code: #{response.code}")
105
+ @env.warn("[deploybot] Message: #{response.message}")
106
+ @env.warn("[deploybot] Body: #{response.body}") if response.message != response.body && response.body !~ /<html/
55
107
  end
56
108
  end
57
109
 
58
- def post_to_slack_as_webhook(payload = {})
59
- params = { payload: payload.to_json }
60
- uri = URI(payload[:webhook])
61
- Net::HTTP.post_form(uri, params)
110
+ def perform_http_request(url, payload)
111
+ uri = URI(url)
112
+ http = Net::HTTP.new(uri.host, uri.port)
113
+ http.use_ssl = true
114
+ req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
115
+ req.body = payload.to_json
116
+ http.request(req)
62
117
  end
63
118
  end
64
119
  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.1'
2
+ VERSION = '1.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-deploybot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.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-03-07 00:00:00.000000000 Z
11
+ date: 2022-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 3.5.0
19
+ version: '3.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 3.5.0
26
+ version: '3.5'
27
27
  description: Notify slack channel with list of deployed commits
28
28
  email: kbackowski@gmail.com
29
29
  executables: []
@@ -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