Class: Decidim::GitBackportManager

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/git_backport_manager.rb

Overview

Handles the backport of a given pull request to a branch Uses the git commnad line client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pull_request_id:, release_branch:, backport_branch:, working_dir: Dir.pwd, exit_with_unstaged_changes: false) ⇒ GitBackportManager

Returns a new instance of GitBackportManager.

Parameters:

  • pull_request_id (String)

    the ID of the pull request that we want to backport

  • release_branch (String)

    the name of the branch that we want to backport to

  • backport_branch (String)

    the name of the branch that we want to create

  • working_dir (String) (defaults to: Dir.pwd)

    current working directory. Useful for testing purposes

  • exit_with_unstaged_changes (Boolean) (defaults to: false)

    wheter we should exit cowardly if there is any unstaged change



14
15
16
17
18
19
20
# File 'lib/decidim/git_backport_manager.rb', line 14

def initialize(pull_request_id:, release_branch:, backport_branch:, working_dir: Dir.pwd, exit_with_unstaged_changes: false)
  @pull_request_id = pull_request_id
  @release_branch = release_branch
  @backport_branch = backport_branch
  @working_dir = working_dir
  @exit_with_unstaged_changes = exit_with_unstaged_changes
end

Class Method Details

.checkout_developvoid

This method returns an undefined value.

Switch to the develop branch In case that it can’t do that, exits



52
53
54
55
56
57
58
59
60
# File 'lib/decidim/git_backport_manager.rb', line 52

def self.checkout_develop
  `git checkout develop`

  error_message = <<-EOERROR
  Could not checkout the develop branch.
  Please make sure you don't have any uncommitted changes in the current branch.
  EOERROR
  exit_with_errors(error_message) unless $CHILD_STATUS.exitstatus.zero?
end

Instance Method Details

#callvoid

This method returns an undefined value.

Handles all the different tasks involved on a backport with the git command line utility It does the following tasks:

  • Creates a branch based on a release branch

  • Apply a commit to this branch

  • Push it to the remote repository



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/decidim/git_backport_manager.rb', line 29

def call
  Dir.chdir(working_dir) do
    exit_if_unstaged_changes if @exit_with_unstaged_changes
    self.class.checkout_develop
    sha_commit = sha_commit_to_backport

    error_message = <<-EOERROR
    Could not find commit for pull request #{pull_request_id}.
    Please make sure you have pulled the latest changes.
    EOERROR
    exit_with_errors(error_message) unless sha_commit

    create_backport_branch!
    cherrypick_commit!(sha_commit)
    clean_commit_message!
    push_backport_branch!
  end
end