Skip to content

Commit

Permalink
add support for multiple versioning strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
kitallis committed Dec 1, 2023
1 parent 4e7a0cc commit 654dd48
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 77 deletions.
1 change: 1 addition & 0 deletions app/controllers/trains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def train_params
:minor_version_seed,
:patch_version_seed,
:branching_strategy,
:versioning_strategy,
:release_backmerge_branch,
:release_branch,
:kickoff_at,
Expand Down
35 changes: 29 additions & 6 deletions app/libs/versioning_strategies/semverish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@ class VersioningStrategies::Semverish

TEMPLATES = {
"Positive Number" => :pn,
"Current Year" => :yyyy
"Calendar Year And Next Week" => :yy0w1
}

INCREMENTS = {
TEMPLATES["Positive Number"] => proc { |v| (!v.nil?) ? v.abs + 1 : nil },
TEMPLATES["Current Year"] => proc { |_v| Time.current.year }
TEMPLATES["Calendar Year And Next Week"] => proc { |_v|
now = Time.current
Integer("#{now.year.to_s[2..3]}#{now.strftime("%U").to_i + 1}")
}
}

DEFAULT_TEMPLATE = TEMPLATES["Positive Number"]
STRATEGIES = {
semver: {
major: TEMPLATES["Positive Number"],
minor: TEMPLATES["Positive Number"],
patch: TEMPLATES["Positive Number"],
update_minor_on_major_bump: false
},

year_and_next_week: {
major: TEMPLATES["Positive Number"],
minor: TEMPLATES["Calendar Year And Next Week"],
patch: TEMPLATES["Positive Number"],
update_minor_on_major_bump: true
}
}

DEFAULT_STRATEGY = :semver

# adapted from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
# makes the patch version optional
Expand All @@ -36,12 +55,16 @@ def initialize(version_str)
@version = version_str
end

def bump!(term, template_type: DEFAULT_TEMPLATE)
def bump!(term, strategy: DEFAULT_STRATEGY)
term = term.to_sym
new_version = clone
new_value = INCREMENTS[template_type].call(public_send(term))
strategy_config = STRATEGIES[strategy.to_sym]
new_value = INCREMENTS[strategy_config[term]].call(public_send(term))
new_version.public_send("#{term}=", new_value)
new_version.minor = 0 if term == :major
if term == :major
new_version.minor = 0 unless strategy_config[:update_minor_on_major_bump]
new_version.minor = INCREMENTS[strategy_config[:minor]].call(public_send(:minor)) if strategy_config[:update_minor_on_major_bump]
end
new_version.patch = 0 if proper? && (term == :major || term == :minor)
new_version
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/versionable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def next_version(major_only: false, patch_only: false)
major_only ? :major : :minor
end

version_current.ver_bump(bump_term)
version_current.ver_bump(bump_term, strategy: versioning_strategy)
end
end
1 change: 1 addition & 0 deletions app/models/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Release < ApplicationRecord

attr_accessor :has_major_bump, :force_finalize, :hotfix_platform

delegate :versioning_strategy, to: :train
delegate :app, :pre_release_prs?, :vcs_provider, :release_platforms, :notify!, :continuous_backmerge?, to: :train
delegate :platform, to: :app

Expand Down
5 changes: 3 additions & 2 deletions app/models/release_platform_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ReleasePlatformRun < ApplicationRecord

scope :pending_release, -> { where.not(status: [:finished, :stopped]) }

delegate :versioning_strategy, to: :release
delegate :all_commits, :original_release_version, :hotfix?, to: :release
delegate :steps, :train, :app, :platform, to: :release_platform

Expand Down Expand Up @@ -115,8 +116,8 @@ def bump_version!

semverish = newest_release_version.to_semverish

self.release_version = semverish.bump!(:patch).to_s if semverish.proper?
self.release_version = semverish.bump!(:minor).to_s if semverish.partial?
self.release_version = semverish.bump!(:patch, strategy: versioning_strategy).to_s if semverish.proper?
self.release_version = semverish.bump!(:minor, strategy: versioning_strategy).to_s if semverish.partial?

save!

Expand Down
9 changes: 3 additions & 6 deletions app/models/train.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#
class Train < ApplicationRecord
has_paper_trail
using RefinedArray
using RefinedString
extend FriendlyId
include Rails.application.routes.url_helpers
Expand Down Expand Up @@ -66,13 +67,9 @@ class Train < ApplicationRecord
delegate :ready?, :config, to: :app
delegate :vcs_provider, :ci_cd_provider, :notification_provider, :monitoring_provider, to: :integrations

enum status: {
draft: "draft",
active: "active",
inactive: "inactive"
}

enum status: {draft: "draft", active: "active", inactive: "inactive"}
enum backmerge_strategy: {continuous: "continuous", on_finalize: "on_finalize"}
enum versioning_strategy: {semver: "Semver", year_and_next_week: "Year and Next Week"}

friendly_id :name, use: :slugged
auto_strip_attributes :name, squish: true
Expand Down
4 changes: 2 additions & 2 deletions app/refinements/refined_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def to_semverish
VersioningStrategies::Semverish.new(to_s)
end

def ver_bump(term)
to_semverish.bump!(term).to_s
def ver_bump(term, strategy:)
to_semverish.bump!(term, strategy:).to_s
end

def better_titleize
Expand Down
8 changes: 5 additions & 3 deletions app/views/trains/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<%= render partial: "notifications_config_form", locals: { form: } %>
<% end %>

<div class="text-xl text-slate-600 font-medium mt-2 mb-4">Branching Strategy</div>
<div class="text-xl text-slate-600 font-medium mt-6 mb-4">Branching Strategy</div>

<div class="grid gap-x-5 md:grid-cols-2">
<div>
Expand Down Expand Up @@ -163,6 +163,10 @@
to learn more about our supported branching strategies.
</div>
<% end %>

<%= if current_organization.fixed_build_number? %>
<%= render partial: "trains/versioning_strategy", locals: { form:, train: } %>
<% end %>
</section>

<section>
Expand All @@ -178,8 +182,6 @@
<%= render partial: "store_tags_form", locals: { form: } %>
<% end %>
</section>


</article>

<div class="mt-12">
Expand Down
10 changes: 10 additions & 0 deletions app/views/trains/_versioning_strategy.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="text-xl text-slate-600 font-medium mt-6 mb-4">Versioning Strategy</div>

<div>
<%= form.label "Strategy", class: "block text-sm font-medium mb-1" %>
<%= form.select :versioning_strategy,
options_for_select(Train.versioning_strategies.invert, train.versioning_strategy),
{ required: true, },
{ class: text_field_classes(is_disabled: train.persisted?),
disabled: train.persisted? } %>
</div>
5 changes: 5 additions & 0 deletions db/migrate/20231201191245_add_versioning_strategy_to_train.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddVersioningStrategyToTrain < ActiveRecord::Migration[7.0]
def change
add_column :trains, :versioning_strategy, :string, default: "semver"
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 654dd48

Please sign in to comment.