-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.rb
More file actions
189 lines (155 loc) · 4.7 KB
/
insert_data.rb
File metadata and controls
189 lines (155 loc) · 4.7 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'sqlite3'
require 'date'
# Helper script to insert commit and release data into the database
# Usage:
# git -C ../renovate log --pretty=format:"%H|%cd|%s" --date=short | ruby insert_data.rb --major-version 41
# git -C ../renovate tag -l "41.*" --format="%(refname:short)|%(creatordate:short)" | ruby insert_data.rb --tags
DB_FILE = 'release_train_stats.db'
def get_db
SQLite3::Database.new(DB_FILE)
end
def parse_conventional_commit(message)
# Parse conventional commit format: type(scope): message
# or type: message
match = message.match(/^(\w+)(?:\(([^)]+)\))?:\s*(.+)/)
if match
{
type: match[1],
scope: match[2],
subject: match[3]
}
else
{
type: nil,
scope: nil,
subject: message
}
end
end
def is_dependency_update?(message)
# Common patterns for dependency updates
message.match?(/^(chore|build)\(deps\)|update dependency|bump .+ from|^chore: lock file maintenance/i)
end
def parse_version(tag)
# Extract major, minor, patch from semver tag (e.g., "41.2.3" or "v41.2.3")
match = tag.match(/^v?(\d+)\.(\d+)\.(\d+)/)
return nil unless match
{
major: match[1].to_i,
minor: match[2].to_i,
patch: match[3].to_i
}
end
def insert_commit(db, sha:, date:, message:, release_tag: nil, major_version: nil)
parsed = parse_conventional_commit(message)
is_dep = is_dependency_update?(message)
# Auto-extract major version from release_tag if not provided
if major_version.nil? && release_tag
version = parse_version(release_tag)
major_version = version[:major] if version
end
db.execute(
'INSERT OR IGNORE INTO commits (sha, commit_date, message, type, scope, is_dependency_update, release_tag, major_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[sha, date, message, parsed[:type], parsed[:scope], is_dep ? 1 : 0, release_tag, major_version]
)
end
def insert_release(db, tag:, date:, is_minor: false, major_version: nil, minor_version: nil, patch_version: nil)
# Auto-parse version numbers if not provided
if major_version.nil? || minor_version.nil? || patch_version.nil?
version = parse_version(tag)
if version
major_version ||= version[:major]
minor_version ||= version[:minor]
patch_version ||= version[:patch]
end
end
db.execute(
'INSERT OR IGNORE INTO releases (tag, release_date, is_minor, major_version, minor_version, patch_version) VALUES (?, ?, ?, ?, ?, ?)',
[tag, date, is_minor ? 1 : 0, major_version, minor_version, patch_version]
)
end
def insert_commits_batch(db, commits)
# commits should be an array of hashes with keys: sha, date, message, release_tag (optional)
db.transaction do
commits.each do |commit|
insert_commit(db, **commit)
end
end
end
def insert_releases_batch(db, releases)
# releases should be an array of hashes with keys: tag, date, is_minor
db.transaction do
releases.each do |release|
insert_release(db, **release)
end
end
end
def is_minor_release?(tag)
# Check if tag ends with .0 (e.g., 41.0.0, 41.1.0)
parts = tag.split('.')
parts.length >= 3 && parts[2].split('-')[0] == '0'
end
def parse_git_log_line(line)
# Expected format: SHA|DATE|MESSAGE
parts = line.split('|', 3)
return nil if parts.length < 3
{
sha: parts[0],
date: parts[1],
message: parts[2]
}
end
def parse_git_tag_line(line)
# Expected format: TAG|DATE
parts = line.split('|', 2)
return nil if parts.length < 2
{
tag: parts[0],
date: parts[1],
is_minor: is_minor_release?(parts[0])
}
end
# Main script
if __FILE__ == $0
db = get_db
# Extract major version from args
major_version = nil
if idx = ARGV.index('--major-version')
major_version = ARGV[idx + 1]&.to_i
end
if ARGV.include?('--tags')
# Parse tag data from stdin
count = 0
releases = []
STDIN.each_line do |line|
line = line.strip
next if line.empty?
release_data = parse_git_tag_line(line)
next unless release_data
releases << release_data
end
unless releases.empty?
insert_releases_batch(db, releases)
puts "Inserted #{releases.length} releases"
end
else
# Parse commit data from stdin
count = 0
commits = []
STDIN.each_line do |line|
line = line.strip
next if line.empty?
commit_data = parse_git_log_line(line)
next unless commit_data
# Add major_version if provided
commit_data[:major_version] = major_version if major_version
commits << commit_data
end
unless commits.empty?
insert_commits_batch(db, commits)
puts "Inserted #{commits.length} commits"
end
end
end