Class: Vtasks::Release

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Utils::Git, Utils::Output, Utils::Semver
Defined in:
lib/vtasks/release.rb

Overview

Release tasks

Constant Summary

Constants included from Utils::Semver

Utils::Semver::SEM_LEVELS

Constants included from Utils::Git

Utils::Git::GITHUB_TOKEN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Semver

#bump, #gitver, #semver

Methods included from Utils::Output

#debug, #error, #info, #warn

Methods included from Utils::Git

#git_branch, #git_ci_status, #git_clean_repo, #git_commit, #git_url

Constructor Details

#initialize(options = {}) ⇒ Release

Returns a new instance of Release.



19
20
21
22
23
24
25
# File 'lib/vtasks/release.rb', line 19

def initialize(options = {})
  @write_changelog = options.fetch(:write_changelog, false)
  @wait_for_ci_success = options.fetch(:wait_for_ci_success, false)
  @bug_labels = options.fetch(:bug_labels, 'bug')
  @enhancement_labels = options.fetch(:enhancement_labels, 'enhancement')
  define_tasks
end

Instance Attribute Details

#bug_labelsObject (readonly)

Returns the value of attribute bug_labels.



14
15
16
# File 'lib/vtasks/release.rb', line 14

def bug_labels
  @bug_labels
end

#enhancement_labelsObject (readonly)

Returns the value of attribute enhancement_labels.



14
15
16
# File 'lib/vtasks/release.rb', line 14

def enhancement_labels
  @enhancement_labels
end

#wait_for_ci_successObject (readonly)

Returns the value of attribute wait_for_ci_success.



14
15
16
# File 'lib/vtasks/release.rb', line 14

def wait_for_ci_success
  @wait_for_ci_success
end

#write_changelogObject (readonly)

Returns the value of attribute write_changelog.



14
15
16
# File 'lib/vtasks/release.rb', line 14

def write_changelog
  @write_changelog
end

Instance Method Details

#changelog(config, release: nil) ⇒ Object

Configure the github_changelog_generator/task



28
29
30
31
32
# File 'lib/vtasks/release.rb', line 28

def changelog(config, release: nil)
  config.bug_labels         = bug_labels #'Type: Bug'
  config.enhancement_labels = enhancement_labels #'Type: Enhancement'
  config.future_release     = "v#{release}" if release
end

#define_tasksObject



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
# File 'lib/vtasks/release.rb', line 34

def define_tasks
  desc "Release patch version"
  task release: ['release:patch']

  namespace :release do
    begin
      require 'github_changelog_generator/task'

      # Create release:changes task
      ::GitHubChangelogGenerator::RakeTask.new(:changes) do |config|
          changelog(config)
      end
    rescue LoadError
      nil # Might be in a group that is not installed
    end

    SEM_LEVELS.each do |level|
      desc "Release #{level} version"
      task level.to_sym do
        new_version = bump(level)
        release = "#{new_version[:major]}.#{new_version[:minor]}.#{new_version[:patch]}"
        release_branch = "release_v#{release.gsub(/[^0-9A-Za-z]/, '_')}"
        initial_branch = git_branch

        info 'Check if the repository is clean'
        git_clean_repo

        # Write changelog
        # Create a separate release branch (works with  protected branches as well)
        if write_changelog == true
          info 'Generate new changelog'
          ::GitHubChangelogGenerator::RakeTask.new(:latest_release) do |config|
            changelog(config, release: release)
          end
          task('latest_release').invoke

          if system 'git diff --quiet HEAD'
            info 'CHANGELOG has not changed. Skipping...'
          else
            info 'Create a new release branch'
            sh "git checkout -b #{release_branch}"

            info 'Commit the new changes'
            sh "git commit --gpg-sign --message 'Update change log for v#{release}' CHANGELOG.md"

            info 'Push the new changes'
            sh "git push --set-upstream origin #{release_branch}"

            if wait_for_ci_success == true
              info 'Waiting for CI to finish'
              sleep 5 until git_ci_status(release_branch) == 'success'
            end

            info 'Merge release branch'
            sh "git checkout #{initial_branch}"
            sh "git merge --gpg-sign --no-ff --message 'Release v#{release}' #{release_branch}"
          end
        end

        info "Tag #{release}"
        sh "git tag --sign v#{release} --message 'Release v#{release}'"
        sh 'git push --follow-tags'
      end # task
    end # LEVELS
  end # namespace :release
end