-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-release.rb
executable file
·317 lines (273 loc) · 11.5 KB
/
pre-release.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env ruby
# encoding: UTF-8
########################################################################################################################
# The purpose of this tool is to handle several pre release tasks. These tasks are:
# - Update the data in README.md
# - Update the changelog.txt (using JIRA's REST API to get the required information)
# - Print a release file template for the hibernate.org website
########################################################################################################################
require 'rubygems'
require 'bundler/setup'
require 'json'
require 'net/https'
require 'choice'
require 'git'
require 'word_wrap'
require 'word_wrap/core_ext'
$now = Time.now
########################################################################################################################
# Defining the various command line arguments for this script
########################################################################################################################
Choice.options do
header 'Application options:'
separator 'Required:'
option :project, :required => true do
short '-p'
long '--project=<project>'
desc 'The project to process'
valid %w[beanvalidation-tck beanvalidation-api beanvalidation-spec]
end
option :release_version, :required => true do
short '-v'
long '--release-version=<version>'
desc 'The release version to process'
end
separator 'Optional:'
option :release_version_qualifier, :required => false do
short '-q'
long '--release_version_qualifier=<release_version_qualifier>'
desc 'The release version qualifier'
default ''
end
option :update_readme, :required => false do
short '-r'
long '--update-readme=<path to readme>'
desc 'If specified the date in README.md will be updated'
end
option :update_changelog, :required => false do
short '-c'
long '--update-change-log=<path to changelog>'
desc 'If specified changelog.txt will be updated'
end
separator 'Common:'
option :help do
short '-h'
long '--help'
desc 'This scripts handles various pre-release steps like changelog and readme updates.'
end
end
########################################################################################################################
# Defining all required configuration properties
########################################################################################################################
# Project specific configuration
$project=Choice.choices[:project]
if $project == "beanvalidation-tck"
$jira_key='BVTCK'
elsif $project == "beanvalidation-api"
$jira_key='BVAL'
elsif $project == "beanvalidation-spec"
$jira_key='BVAL'
end
# REST URL used to retrieve all release versions of the project - https://docs.atlassian.com/jira/REST/latest/#d2e4023
$jira_versions_url='https://hibernate.atlassian.net/rest/api/latest/project/' + $jira_key + '/versions'
# REST URL used for getting all issues of given release - see https://docs.atlassian.com/jira/REST/latest/#d2e2450
$jira_issues_url='https://hibernate.atlassian.net/rest/api/2/search/?jql=project%20%3D%20' + $jira_key + '%20AND%20fixVersion%20%3D%20#{release_version}%20ORDER%20BY%20issuetype%20ASC'
########################################################################################################################
# Defining all required helper methods
########################################################################################################################
def get_json(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request).body
JSON.parse(response)
end
#######################################################################################################################
# We are dealing with something like this
#
# ...,
# {
# "self": "https://hibernate.atlassian.net/rest/api/latest/version/18754",
# "id": "18754",
# "description": "Bugfixes for MongoDB, Neo4j and CouchDB backends",
# "name": "4.1.2.Final",
# "archived": false,
# "released": true,
# "releaseDate": "2015-02-27",
# "userReleaseDate": "27/Feb/2015",
# "projectId": 10160
# },
# ...
def get_release_info(release_version)
jira_versions = get_json $jira_versions_url
jira_version = jira_versions.find { |version| version['name'] == release_version }
abort "ERROR: Version #{release_version} does not exist in JIRA" if jira_version.nil?
abort "ERROR: Version #{release_version} is not yet released in JIRA" if !jira_version['released']
jira_version
end
#######################################################################################################################
# Creates the required update for changelog.txt. It creates the following:
#
# <version> (<date>)
# -------------------------
#
# ** <issue-type-1>
# * PROJECT-<key> - <component> - <summary>
# ...
#
# ** <issue-type-2>
# * PROJECT-<key> - <component> - <summary>
# ...
#
def create_changelog_update(release_version, release_version_qualifier, date_format, word_wrap)
interpolated_url = eval '"' + $jira_issues_url + '"'
jira_issues = get_json interpolated_url
processed_issues = process_issues(jira_issues)
max_component_length = calculate_max_component_length(processed_issues)
issue_type = ''
change_log_title = release_version + ' (' + ( release_version_qualifier.strip.size > 0 ? release_version_qualifier.strip + ', ' : '' ) + $now.strftime(date_format) + ')'
change_log_update = change_log_title + "\n"
change_log_update << "".ljust(change_log_title.size, '-') + "\n"
processed_issues.each do |issue|
current_issue_type = issue['type']
if issue_type.empty? or !issue_type.eql? current_issue_type
# create issue type entry
issue_type = current_issue_type
change_log_update << "\n** " << issue_type << "\n"
end
# issue key
issue_update = ' * [' << issue['key'] << '] - '
if word_wrap
line_length = 94 - issue_update.size
wrapped_issue_summary = issue['summary'].wrap(line_length).rstrip
wrapped_issue_summary = wrapped_issue_summary.gsub(/\n/, "\n" + " " * issue_update.size)
issue_update << wrapped_issue_summary << "\n"
else
issue_update << issue['summary'] << "\n"
end
change_log_update << issue_update
end
change_log_update << "\n"
end
#######################################################################################################################
# Processing all issues and putting data needed for changelog into an array where each element is a hash with the
# required data
def process_issues(jira_issues)
issues_array = Array.new
jira_issues['issues'].each do |issue|
issue_hash = Hash.new
# type & key
issue_hash['type'] = issue['fields']['issuetype']['name']
issue_hash['key'] = issue['key']
# components
if issue['fields']['components'].empty?
issue_hash['components'] = ''
else
components = ''
last = issue['fields']['components'].pop
issue['fields']['components'].each { |component|
components << component['name'] << ', '
}
components << last['name']
issue_hash['components'] = components
end
# summary
issue_hash['summary'] = issue['fields']['summary']
issues_array << issue_hash
end
return issues_array
end
#######################################################################################################################
def calculate_max_component_length(processed_issues)
max = 0
processed_issues.each do |issue|
if issue['components'].length > max
max = issue['components'].length
end
end
return max
end
#######################################################################################################################
# Updates version and date in README.md
def update_readme(readme_file_name, release_version)
readme = File.read(readme_file_name)
updated_readme = readme.gsub(/^\*?Version: .*\*?$/, "*Version: #{release_version} - #{$now.strftime("%d-%m-%Y")}*")
# To write changes to the file, use:
File.open(readme_file_name, "w") {|file| file.puts updated_readme }
end
#######################################################################################################################
def insert_lines(file_name, at_line, new_lines)
open(file_name, 'r+') do |file|
while (at_line -= 1) > 0 # read up to the line you want to write after
file.readline
end
position = file.pos # save your position in the file
rest = file.read # save the rest of the file
file.seek position # go back to the old position
file.puts [new_lines, rest] # write new data & rest of file
end
end
#######################################################################################################################
# Returns true if the given path represents a root directory (/ or C:/)
def root_directory?(file_path)
# Implementation inspired by http://stackoverflow.com/a/4969416:
# Does file + ".." resolve to the same directory as file_path?
File.directory?(file_path) &&
File.expand_path(file_path) == File.expand_path(File.join(file_path, '..'))
end
# Returns the git root directory given a path inside the repo. Returns nil if
# the path is not in a git repo.
def find_git_repo(start_path = '.')
raise NoSuchPathError unless File.exists?(start_path)
current_path = File.expand_path(start_path)
# for clarity: set to an explicit nil and then just return whatever
# the current value of this variable is (nil or otherwise)
return_path = nil
until root_directory?(current_path)
if File.exists?(File.join(current_path, '.git'))
# done
return_path = current_path
break
else
# go up a directory and try again
current_path = File.dirname(current_path)
end
end
return_path
end
def git_commit(file_name, message)
working_dir = find_git_repo(File.dirname file_name)
git = Git.open(working_dir)
git.add(file_name)
git.commit(message)
end
########################################################################################################################
# Putting it all together
########################################################################################################################
release_version = Choice.choices[:release_version]
release_version_qualifier = Choice.choices[:release_version_qualifier]
jira_release_info = get_release_info release_version
readme_file_name = Choice.choices[:update_readme]
if !readme_file_name.nil? and !readme_file_name.empty?
abort "ERROR: #{readme_file_name} is not a valid file" unless File.exist?(readme_file_name)
update_readme(readme_file_name, release_version)
git_commit(readme_file_name, "[Jenkins release job] README.md updated by release build #{release_version}")
end
change_log_file_name = Choice.choices[:update_changelog]
if !change_log_file_name.nil? and !change_log_file_name.empty?
abort "ERROR: #{change_log_file_name} is not a valid file" unless File.exist?(change_log_file_name)
if $project == "beanvalidation-spec"
date_format = "%Y-%m-%d"
line_number = 18
word_wrap = true
else
date_format = "%d-%m-%Y"
line_number = 6
word_wrap = false
end
change_log_update = create_changelog_update(release_version, release_version_qualifier, date_format, word_wrap)
insert_lines(change_log_file_name, line_number, change_log_update)
git_commit(change_log_file_name, "[Jenkins release job] changelog updated by release build #{release_version}")
end