capistrano-committed 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -1,4 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
4
- before_install: gem install bundler -v 1.10.6
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0.0
6
+ - rbx-2
7
+ addons:
8
+ code_climate:
9
+ repo_token: c19f4a2a864567e4dbf3e325b4423d9478c3bf025d9e3f85c6e2ffc0c9fb960f
10
+ before_install: gem install bundler -v 1.11.2
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in capistrano-release-log.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'codeclimate-test-reporter', require: nil
8
+ end
data/README.md CHANGED
@@ -1,5 +1,16 @@
1
+ <div align="center">
2
+ <a href="https://github.com/sambauers/capistrano-committed"><img width="136" src="https://github.com/sambauers/capistrano-committed/raw/master/icons/capricorn.png" alt="Capistrano Committed logo" /></a>
3
+ </div>
4
+
1
5
  # Capistrano Committed
2
6
 
7
+ [![Gem Version](https://badge.fury.io/rb/capistrano-committed.svg)](https://badge.fury.io/rb/capistrano-committed)
8
+ [![Build Status](https://travis-ci.org/sambauers/capistrano-committed.svg?branch=master)](https://travis-ci.org/sambauers/capistrano-committed)
9
+ [![Test Coverage](https://codeclimate.com/github/sambauers/capistrano-committed/badges/coverage.svg)](https://codeclimate.com/github/sambauers/capistrano-committed/coverage)
10
+ [![Code Climate](https://codeclimate.com/github/sambauers/capistrano-committed/badges/gpa.svg)](https://codeclimate.com/github/sambauers/capistrano-committed)
11
+ [![Issue Count](https://codeclimate.com/github/sambauers/capistrano-committed/badges/issue_count.svg)](https://codeclimate.com/github/sambauers/capistrano-committed)
12
+ [![Dependency Status](https://gemnasium.com/sambauers/capistrano-committed.svg)](https://gemnasium.com/sambauers/capistrano-committed)
13
+
3
14
  Capistrano Committed is an extension to Capistrano 3 which helps to determine what you are about to deploy.
4
15
 
5
16
  It creates a report, which lets you know which GitHub commits and pull requests are not yet deployed to the target stage (server).
@@ -99,6 +110,9 @@ set :committed_issue_match, '\[\s?([A-Z0-9]+\-[0-9]+)\s?\]'
99
110
  # "%s" will be replaced with the issue number. Setting this to `nil` will also
100
111
  # disable issue matching altogether.
101
112
  set :committed_issue_url, 'https://example.jira.com/browse/%s'
113
+
114
+ # Register deployments in GitHub via the GitHub Deployments API
115
+ set :committed_deployments, false
102
116
  ```
103
117
 
104
118
  Once your required settings are all in place, you can generate a report by running:
@@ -106,3 +120,14 @@ Once your required settings are all in place, you can generate a report by runni
106
120
  ```shell
107
121
  $ cap <stage> committed:generate
108
122
  ```
123
+
124
+ ## What's with the unicorn?
125
+
126
+ <div align="center">
127
+ <a href="https://github.com/sambauers/capistrano-committed"><img width="640" src="https://github.com/sambauers/capistrano-committed/raw/master/icons/capricorn_equation.png" alt="Capistrano + GitHub API gem = Capistrano Committed gem" /></a>
128
+ </div>
129
+
130
+ * [Capistrano](http://capistranorb.com)
131
+ * [GitHub API gem](https://github.com/peter-murach/github)
132
+
133
+
Binary file
Binary file
Binary file
@@ -4,65 +4,122 @@ module Capistrano
4
4
  module Committed
5
5
  class GithubApi
6
6
  def initialize(config_options = {})
7
- raise TypeError, '`initialize` requires a hash to be passed as the first and only argument' unless config_options.is_a?(Hash)
7
+ validate('config_options', config_options, Hash, __callee__)
8
8
 
9
- config_options.merge!({
10
- :adapter => :net_http,
11
- :ssl => {:verify => false},
12
- :per_page => 100,
13
- :user_agent => 'Committed Ruby Gem (via Github API Ruby Gem)'
14
- })
9
+ options = { adapter: :net_http,
10
+ ssl: { verify: false },
11
+ per_page: 100,
12
+ user_agent: 'Committed Ruby Gem (via Github API Ruby Gem)' }
15
13
 
16
- @client = ::Github.new config_options
14
+ options.merge! config_options
15
+
16
+ @client = ::Github.new options
17
+ end
18
+
19
+ def client
20
+ @client
17
21
  end
18
22
 
19
23
  def get_commit(user, repo, sha)
20
- validate_user_and_repo(user, repo)
21
- raise TypeError, sprintf('`%s` requires a valid commit SHA.', __callee__) unless sha.is_a?(String)
24
+ validate_user_and_repo(user, repo, __callee__)
25
+ validate('sha', sha, String, __callee__)
22
26
 
23
- begin
24
- @client.repos.commits.get(:user => user, :repo => repo, :sha => sha)
25
- rescue ::Github::Error::GithubError => e
26
- rescue_github_errors(e)
27
+ api_call do
28
+ @client.repos.commits.get(user: user,
29
+ repo: repo,
30
+ sha: sha)
27
31
  end
28
32
  end
29
33
 
30
34
  def get_commits_since(user, repo, date, branch = 'master')
31
- validate_user_and_repo(user, repo)
35
+ validate_user_and_repo(user, repo, __callee__)
32
36
  date = Time.parse(date) if date.is_a?(String)
33
- raise TypeError, sprintf('`%s` requires a valid date.', __callee__) unless date.is_a?(Time)
34
- raise TypeError, sprintf('`%s` requires a valid branch.', __callee__) unless branch.is_a?(String)
37
+ validate('date', date, Time, __callee__)
38
+ validate('branch', branch, String, __callee__)
35
39
 
36
- begin
37
- @client.repos.commits.list(:user => user, :repo => repo, :sha => branch, :since => date.iso8601)
38
- rescue ::Github::Error::GithubError => e
39
- rescue_github_errors(e)
40
+ api_call do
41
+ @client.repos.commits.list(user: user,
42
+ repo: repo,
43
+ sha: branch,
44
+ since: date.iso8601)
40
45
  end
41
46
  end
42
47
 
43
48
  def get_pull_request(user, repo, number)
44
- validate_user_and_repo(user, repo)
45
- raise TypeError, sprintf('`%s` requires a valid pull request number.', __callee__) unless number.is_a?(Integer)
46
-
47
- begin
48
- info = @client.pull_requests.get(:user => user, :repo => repo, :number => number)
49
- commits = @client.pull_requests.commits(:user => user, :repo => repo, :number => number)
50
- return {:info => info, :commits => commits}
51
- rescue ::Github::Error::GithubError => e
52
- rescue_github_errors(e)
49
+ validate_user_and_repo(user, repo, __callee__)
50
+ validate('number', number, Integer, __callee__)
51
+
52
+ api_call do
53
+ info = @client.pull_requests.get(user: user,
54
+ repo: repo,
55
+ number: number)
56
+
57
+ commits = @client.pull_requests.commits(user: user,
58
+ repo: repo,
59
+ number: number)
60
+
61
+ return { info: info, commits: commits }
62
+ end
63
+ end
64
+
65
+ def register_deployment(user, repo, stage, branch = 'master')
66
+ validate_user_and_repo(user, repo, __callee__)
67
+ validate('stage', stage, String, __callee__)
68
+ validate('branch', branch, String, __callee__)
69
+
70
+ api_call do
71
+ @client.repos.deployments.create(user: user,
72
+ repo: repo,
73
+ environment: stage,
74
+ ref: branch,
75
+ auto_merge: false,
76
+ required_contexts: [])
53
77
  end
54
78
  end
55
79
 
56
- def validate_user_and_repo(user, repo)
57
- raise TypeError, sprintf('`%s` requires a valid GitHub user.', __caller__) unless user.is_a?(String)
58
- raise TypeError, sprintf('`%s` requires a valid GitHub repository.', __caller__) unless repo.is_a?(String)
80
+ def register_deployment_status(user, repo, id, state)
81
+ validate_user_and_repo(user, repo, __callee__)
82
+ validate('id', id, Integer, __callee__)
83
+
84
+ valid_states = %w(pending success error failure)
85
+ state = state.to_s
86
+ unless valid_states.include?(state)
87
+ message = t('committed.error.helpers.valid_param',
88
+ method: __callee__,
89
+ param: 'state')
90
+ fail TypeError, message
91
+ end
92
+
93
+ api_call do
94
+ @client.repos.deployments.create_status(user: user,
95
+ repo: repo,
96
+ id: id,
97
+ state: state)
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def validate(param, value, type, method)
104
+ return if value.is_a?(type)
105
+ message = t('committed.error.helpers.valid_param',
106
+ method: method,
107
+ param: param)
108
+ fail TypeError, message
109
+ end
110
+
111
+ def validate_user_and_repo(user, repo, method)
112
+ validate('GitHub user', user, String, method)
113
+ validate('GitHub repository', repo, String, method)
59
114
  end
60
115
 
61
- def rescue_github_errors(e)
116
+ def api_call
117
+ yield
118
+ rescue ::Github::Error::GithubError => e
62
119
  if e.is_a? ::Github::Error::ServiceError
63
- raise e, 'There seems to be a problem with the GitHub service.'
120
+ raise e, t('committed.error.helpers.github_service_error')
64
121
  elsif e.is_a? ::Github::Error::ClientError
65
- raise e, 'There seems to be a problem with the request that was made to GitHub, check that your settings are correct.'
122
+ raise e, t('committed.error.helpers.github_client_error')
66
123
  end
67
124
  end
68
125
  end
@@ -2,6 +2,11 @@ require 'i18n'
2
2
 
3
3
  en = {
4
4
  error: {
5
+ helpers: {
6
+ valid_param: '`%{method}` requires a valid %{param}.',
7
+ github_service_error: 'There seems to be a problem with the GitHub service.',
8
+ github_client_error: 'There seems to be a problem with the request that was made to GitHub, check that your settings are correct.'
9
+ },
5
10
  prerequisites: {
6
11
  nil: '`:%{variable}` variable is `nil`, it needs to contain the %{name} name.',
7
12
  empty: '`:%{variable}` variable is empty, it needs to contain the %{name} name.',
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module Committed
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -1,22 +1,43 @@
1
- require "capistrano/committed/version"
1
+ require 'capistrano/committed/version'
2
2
  require 'capistrano/committed/i18n'
3
- require "capistrano/committed/github_api"
3
+ require 'capistrano/committed/github_api'
4
4
 
5
5
  module Capistrano
6
6
  module Committed
7
7
  class << self
8
- def scan_for_issues(pattern, string)
9
- raise TypeError, sprintf('`%s` requires a valid pattern.', __callee__) unless pattern.is_a?(String) || pattern.is_a?(Regexp)
10
- raise TypeError, sprintf('`%s` requires a valid string.', __callee__) unless pattern.is_a?(String)
8
+ def get_issue_urls(issue_pattern, url_pattern, message)
9
+ fail TypeError, t('committed.error.helpers.valid_param',
10
+ method: __callee__,
11
+ param: 'issue_pattern') unless
12
+ issue_pattern.is_a?(String) ||
13
+ issue_pattern.is_a?(Regexp)
11
14
 
12
- matches = Regexp.new(pattern).match(string)
13
- return unless matches && matches[1]
14
- matches = matches.to_a
15
- matches.shift
16
- matches
15
+ fail TypeError, t('committed.error.helpers.valid_param',
16
+ method: __callee__,
17
+ param: 'url_pattern') unless
18
+ url_pattern.is_a?(String)
19
+
20
+ fail TypeError, t('committed.error.helpers.valid_param',
21
+ method: __callee__,
22
+ param: 'message') unless
23
+ message.is_a?(String)
24
+
25
+ matches = message.scan(Regexp.new(issue_pattern))
26
+ return [] unless matches
27
+ matches.map { |m| format(url_pattern, m[0]) }
28
+ end
29
+
30
+ def format_issue_urls(urls, pad = '')
31
+ return [] if urls.nil? || urls.empty?
32
+ output = []
33
+ output << format('%s %s', pad, t('committed.output.issue_links'))
34
+ urls.each do |url|
35
+ output << format('%s - %s', pad, url)
36
+ end
37
+ output << format('%s', pad)
17
38
  end
18
39
  end
19
40
  end
20
41
  end
21
42
 
22
- load File.expand_path("../tasks/committed.rake", __FILE__)
43
+ load File.expand_path('../tasks/committed.rake', __FILE__)