Class: ReleaseMe::BumpTag

Inherits:
Object
  • Object
show all
Includes:
VcsProvider
Defined in:
lib/release_me/bump_tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VcsProvider

#provider_config

Constructor Details

#initialize(options) ⇒ BumpTag

Returns a new instance of BumpTag.



11
12
13
14
15
16
17
18
# File 'lib/release_me/bump_tag.rb', line 11

def initialize(options)
  @options = options
  opts = {
    json: true,
    version: options[:version_type]
  }
  @version_instance = ReleaseMe::BumpVersion.new(opts)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/release_me/bump_tag.rb', line 8

def options
  @options
end

#version_instanceObject (readonly)

Returns the value of attribute version_instance.



8
9
10
# File 'lib/release_me/bump_tag.rb', line 8

def version_instance
  @version_instance
end

Instance Method Details

#bump_version_fileHash

Returns output from bump version.

Returns:

  • (Hash)

    output from bump version



41
42
43
# File 'lib/release_me/bump_tag.rb', line 41

def bump_version_file
  JSON.parse(version_instance.run)
end

#commits_urlObject



24
25
26
# File 'lib/release_me/bump_tag.rb', line 24

def commits_url
  @commit_url ||= URI("#{project_url}/#{vcs_config.commits_path}")
end

#file_update_body_request(file_contents, message) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/release_me/bump_tag.rb', line 45

def file_update_body_request(file_contents, message)
  {
    id: vcs_config.project_id,
    branch_name: vcs_config.branch_name,
    author_email: vcs_config.author_email,
    author_name: vcs_config.author_name,
    commit_message: message,
    actions: [
      {
        action: :update,
        file_path: version_instance.app_version_file.gsub(Dir.pwd + '/', ''),
        content: file_contents
      }
    ]
  }.to_json
end

#private_tokenObject



20
21
22
# File 'lib/release_me/bump_tag.rb', line 20

def private_token
  vcs_config.private_token
end

#project_urlObject



28
29
30
# File 'lib/release_me/bump_tag.rb', line 28

def project_url
  @project_url ||= "#{vcs_config.base_url}/#{vcs_config.project_id}"
end

#runObject



126
127
128
129
130
131
132
133
# File 'lib/release_me/bump_tag.rb', line 126

def run
  version_output = bump_version_file
  unless options[:dry_run]
    commit_id = update_version(version_output)
    tag(version_output['new_version'], commit_id) unless @options[:no_tagging]
  end
  "Tagged: #{version_output['new_version']}"
end

#send_data(uri, body, method = nil) ⇒ String

Returns message body for url call in JSON format.

Parameters:

  • uri (URI)

    of the request

  • Body (Object)

    of request is a hash or array

  • method (:put or :post) (defaults to: nil)

    to use when sending data to gitlab

Returns:

  • (String)

    message body for url call in JSON format



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
122
123
124
# File 'lib/release_me/bump_tag.rb', line 95

def send_data(uri, body, method = nil)
  use_ssl = uri.scheme == 'https'
  conn = Net::HTTP.new(uri.host, uri.port)
  # conn.set_debug_output
  conn.use_ssl = use_ssl
  # Don't verify
  conn.verify_mode = ::OpenSSL::SSL::VERIFY_NONE if vcs_config.no_verify
  request = case method
            when :put
              Net::HTTP::Put.new(uri.path)
            else
              Net::HTTP::Post.new(uri.path)
            end
  request.body = body
  # private token is specific to gitlab and must be set
  request['PRIVATE-TOKEN'] = vcs_config.private_token
  request.content_type = 'application/json'
  if options[:dry_run]
    puts body
  else
    response = conn.request(request)
    if response.is_a? Net::HTTPSuccess
      response.body
    else
      puts response.code
      puts response.body
      exit 1
    end
  end
end

#tag(version, commit_id) ⇒ String

Returns - json encoded result from http call.

Parameters:

  • - (String)

    new version string

  • - (String)

    commit it to tag

Returns:

  • (String)
    • json encoded result from http call



86
87
88
89
# File 'lib/release_me/bump_tag.rb', line 86

def tag(version, commit_id)
  body = tag_body_request(version, commit_id)
  send_data(tags_url, body, :post)
end

#tag_body_request(tag_name, commit_id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/release_me/bump_tag.rb', line 62

def tag_body_request(tag_name, commit_id)
  {
    id: vcs_config.project_id,
    tag_name: tag_name,
    ref: commit_id, # or git rev-parse HEAD
    # messasge:
    # release_description:
  }.to_json
end

#tags_urlObject



32
33
34
# File 'lib/release_me/bump_tag.rb', line 32

def tags_url
  @tags_url ||= URI("#{project_url}/#{vcs_config.tags_path}")
end

#update_version(version_object) ⇒ String

Returns commit id of submitted body.

Returns:

  • (String)

    commit id of submitted body



73
74
75
76
77
78
79
80
81
# File 'lib/release_me/bump_tag.rb', line 73

def update_version(version_object)
  new_version = version_object['new_version']
  file_content = version_object['file_content']
  message = "Auto tagged to #{new_version}"
  body = file_update_body_request(file_content, message)
  response = send_data(commits_url, body)
  # the id may be specific to gitlab api
  JSON.parse(response)['id']
end

#vcs_configObject



36
37
38
# File 'lib/release_me/bump_tag.rb', line 36

def vcs_config
  @vcs_config ||= provider_config(options[:project_path])
end