Skip to content

Commit

Permalink
Add support for default branches other than "master" (e.g. "main") (c…
Browse files Browse the repository at this point in the history
…loses #225)
  • Loading branch information
codener committed Nov 22, 2024
1 parent ab79265 commit b447e90
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 57 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
### Compatible changes
### Breaking changes


## 11.2.0 2024-11-22

### Compatible changes
* Add support for default branches other than "master" (e.g. "main"). Will read
this information from `origin`.

### Breaking changes

## 11.1.0 2024-11-20

### Compatible changes

* Skip `yarn install` for other package managers:
* Before: Check for a `package.json`
* After: Check for a `yarn.lock`
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
geordi (11.1.0)
geordi (11.2.0)
thor (~> 1)

GEM
Expand Down Expand Up @@ -119,4 +119,4 @@ DEPENDENCIES
rspec

BUNDLED WITH
2.3.25
2.3.26
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ On the first execution we ask for your Linear API token. It will be
stored in `~/.config/geordi/global.yml`.

**Options**
- `-m, [--from-master], [--no-from-master]`: Branch from master instead of the current branch
- `-m, --from-main, [--from-master], [--no-from-master]`: Branch from master instead of the current branch


### `geordi capistrano COMMAND`
Expand Down Expand Up @@ -142,7 +142,8 @@ before it does it.** There are different scenarios where this command is handy:
be skipped.

Calling the command without arguments will infer the target stage from the
current branch and fall back to master/staging.
current branch and fall back to master/staging. (Will use the actual main branch
of the repository, e.g. "main" instead of "master".)

Finds available Capistrano stages by their prefix, e.g. `geordi deploy p` will
deploy production, `geordi deploy mak` will deploy a `makandra` stage if there
Expand Down Expand Up @@ -267,29 +268,20 @@ variable like this: `PARALLEL_TEST_PROCESSORS=6 geordi rspec`
Support for performing security updates.

Preparation for security update: `geordi security-update`. Checks out production
and pulls.
and pulls, and will tell each step before performing it.

After performing the update: `geordi security-update finish`. Switches branches,
pulls, pushes and deploys as required by our workflow.

This command tells what it will do before it does it. In detail:
Part two after performing the update: `geordi security-update finish`. Switches
branches, pulls, pushes and deploys as required by our workflow. This as well
will tell each step before performing it. In detail:

1. Ask user if tests are green

2. Push production

3. Check out master and pull

4. Merge production and push in master

5. Deploy staging, if there is a staging environment

6. Ask user if deployment log is okay and staging application is still running

7. Deploy other stages

8. Ask user if deployment log is okay and application is still running on all stages

9. Inform user about the next (manual) steps


Expand Down
15 changes: 12 additions & 3 deletions features/branch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ Feature: Check out a feature branch based on an issue from Linear
Then the output should contain "Util.run! git, checkout, -b, testuser/team-123-test-issue"


Scenario: Checkout a new branch with geordi branch from master
Scenario: Checkout a new branch from master
Given my local git branches are: master

When I run `geordi branch --from-master`
Then the output should contain "Util.run! git, checkout, master"
And the output should contain "Util.run! git, checkout, -b, testuser/team-123-test-issue"


Scenario: If the target branch already exists, do not attempt to create a new one but simply switch to the existing branch
Scenario: Checkout a new branch from main
Given my local git branches are: main
And my default branch is "main"

When I run `geordi branch --from-main`
Then the output should contain "Util.run! git, checkout, main"
And the output should contain "Util.run! git, checkout, -b, testuser/team-123-test-issue"


Scenario: If the target branch already exists, just check it out
Given my local git branches are: master, testuser/team-123-test-issue

When I run `geordi branch`
Then the output should contain "Util.run! git, checkout, testuser/team-123-test-issue"


Scenario: The interaction fails if the local git branches could not be determined
Scenario: The command fails if local Git branches can not be determined
When I run `geordi branch`
Then the output should contain "Could not determine local Git branches"
16 changes: 16 additions & 0 deletions features/deploy.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ Feature: The deploy command
And I type ""
And I type "no"
Then the output should not contain "Deployment stage: [staging]"


Scenario: Deploy with a default branch of "main"
Given my default branch is "main"

When I run `geordi deploy` interactively
# Answer three prompts
And I type ""
And I type ""
And I type ""
And I type "cancel"

Then the output should contain:
"""
Deployment stage: [staging] Source branch: [main] Deploy branch: [main]
"""
30 changes: 30 additions & 0 deletions features/security_update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,33 @@ Feature: The security-update command
> Now send an email to customer and project lead, informing them about the update.
> Do not forget to make a joblog on a security budget, if available.
"""

Scenario: Finishing the security-update with a "main" branch instead of "master"
Given my default branch is "main"

When I run `geordi security-update finish` interactively
And I type "yes"
And I type "yes"

Then the output should contain:
"""
Util.run! git status --porcelain
"""
Then the output should contain:
"""
> About to: push production, checkout & pull main, merge production, push main.
Continue? [n]
"""
And the output should contain:
"""
> git push
Util.run! git push
> git checkout main
Util.run! git checkout main
> git pull
Util.run! git pull
> git merge production
Util.run! git merge production
> git push
Util.run! git push
"""
5 changes: 5 additions & 0 deletions features/step_definitions/miscellaneous_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
ENV['GEORDI_TESTING_GIT_BRANCHES'] = branches.split(", ").join("\n") + "\n"
end

Given /^my default branch is "(.*)"$/ do |default_branch|
ENV['GEORDI_TESTING_DEFAULT_BRANCH'] = default_branch
end

Given 'there are no Linear issues' do
ENV['GEORDI_TESTING_NO_LINEAR_ISSUES'] = 'true'
end
Expand All @@ -28,4 +32,5 @@
ENV['GEORDI_TESTING_NO_LINEAR_ISSUES'] = nil
ENV['GEORDI_TESTING_IRB_VERSION'] = nil
ENV['GEORDI_TESTING_RUBY_VERSION'] = nil
ENV['GEORDI_TESTING_DEFAULT_BRANCH'] = nil
end
2 changes: 1 addition & 1 deletion lib/geordi/commands/branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
stored in `~/.config/geordi/global.yml`.
LONGDESC

option :from_master, aliases: '-m', type: :boolean, desc: 'Branch from master instead of the current branch'
option :from_master, aliases: %w[-m --from-main], type: :boolean, desc: 'Branch from master instead of the current branch'

def branch
require 'geordi/gitlinear'
Expand Down
10 changes: 7 additions & 3 deletions lib/geordi/commands/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
be skipped.
Calling the command without arguments will infer the target stage from the
current branch and fall back to master/staging.
current branch and fall back to master/staging. (Will use the actual main branch
of the repository, e.g. "main" instead of "master".)
Finds available Capistrano stages by their prefix, e.g. `geordi deploy p` will
deploy production, `geordi deploy mak` will deploy a `makandra` stage if there
Expand All @@ -37,7 +38,7 @@

def deploy(target_stage = nil)
# Set/Infer default values
branch_stage_map = { 'master' => 'staging', 'production' => 'production' }
branch_stage_map = { 'master' => 'staging', 'main' => 'staging', 'production' => 'production' }
if target_stage && !Util.deploy_targets.include?(target_stage)
# Target stage autocompletion from available stages
target_stage = Util.deploy_targets.find { |t| t.start_with? target_stage }
Expand All @@ -58,7 +59,10 @@ def deploy(target_stage = nil)
source_branch = target_branch = Util.current_branch
else
source_branch = Interaction.prompt 'Source branch:', Util.current_branch
target_branch = Interaction.prompt 'Deploy branch:', branch_stage_map.invert.fetch(target_stage, 'master')

deploy_branch = 'production' if target_stage == 'production'
deploy_branch ||= Util.git_default_branch
target_branch = Interaction.prompt 'Deploy branch:', deploy_branch
end

merge_needed = (source_branch != target_branch)
Expand Down
35 changes: 10 additions & 25 deletions lib/geordi/commands/security_update.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
desc 'security-update [STEP]', 'Support for performing security updates'
long_desc <<-LONGDESC
Preparation for security update: `geordi security-update`. Checks out production
and pulls.
and pulls, and will tell each step before performing it.
After performing the update: `geordi security-update finish`. Switches branches,
pulls, pushes and deploys as required by our workflow.
This command tells what it will do before it does it. In detail:
1. Ask user if tests are green
2. Push production
3. Check out master and pull
4. Merge production and push in master
5. Deploy staging, if there is a staging environment
6. Ask user if deployment log is okay and staging application is still running
7. Deploy other stages
8. Ask user if deployment log is okay and application is still running on all stages
9. Inform user about the next (manual) steps
Part two after performing the update: `geordi security-update finish`. Switches
branches, pulls, pushes and deploys as required by our workflow. This as well
will tell each step before performing it.
LONGDESC

def security_update(step = 'prepare')
master = Util.git_default_branch

case step
when 'prepare'
Interaction.announce 'Preparing for security update'
Expand Down Expand Up @@ -56,11 +39,11 @@ def security_update(step = 'prepare')
Interaction.note 'Working directory clean.'
Interaction.prompt('Have you successfully run all tests?', 'n', /y|yes/) || Interaction.fail('Please run tests first.')

Interaction.note 'About to: push production, checkout & pull master, merge production, push master.'
Interaction.note "About to: push production, checkout & pull #{master}, merge production, push #{master}."
Interaction.prompt('Continue?', 'n', /y|yes/) || Interaction.fail('Cancelled.')

Util.run!('git push', show_cmd: true)
Util.run!('git checkout master', show_cmd: true)
Util.run!("git checkout #{master}", show_cmd: true)
Util.run!('git pull', show_cmd: true)
Util.run!('git merge production', show_cmd: true)
Util.run!('git push', show_cmd: true)
Expand Down Expand Up @@ -106,5 +89,7 @@ def security_update(step = 'prepare')
puts
Interaction.note 'Now send an email to customer and project lead, informing them about the update.'
Interaction.note 'Do not forget to make a joblog on a security budget, if available.'
else
Interaction.fail "Unsupported step #{step.inspect}"
end
end
9 changes: 5 additions & 4 deletions lib/geordi/gitlinear.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ def branch(from_master: false)
matching_local_branch = local_branches.find { |branch_name| branch_name == issue['branchName'] }
matching_local_branch ||= local_branches.find { |branch_name| branch_name.include? issue['identifier'].to_s }

if matching_local_branch.nil?
Util.run! ['git', 'checkout', 'master'] if from_master
Util.run! ['git', 'checkout', '-b', issue['branchName']]
else
if matching_local_branch
Util.run! ['git', 'checkout', matching_local_branch]
else
default_branch = Util.git_default_branch
Util.run! ['git', 'checkout', default_branch] if from_master
Util.run! ['git', 'checkout', '-b', issue['branchName']]
end
end

Expand Down
14 changes: 13 additions & 1 deletion lib/geordi/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def server_command

def current_branch
if testing?
'master'
git_default_branch
else
`git rev-parse --abbrev-ref HEAD`.strip
end
Expand Down Expand Up @@ -217,6 +217,18 @@ def cucumber_path?(path)
def rspec_path?(path)
%r{(^|\/)spec|_spec\.rb($|:)}.match?(path)
end

def git_default_branch
default_branch = if testing?
ENV['GEORDI_TESTING_DEFAULT_BRANCH']
else
head_symref = `git ls-remote --symref origin HEAD`
head_symref[%r{\Aref: refs/heads/(\S+)\sHEAD}, 1]
end

default_branch || 'master'
end

end
end
end
2 changes: 1 addition & 1 deletion lib/geordi/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Geordi
VERSION = '11.1.0'.freeze
VERSION = '11.2.0'.freeze
end

0 comments on commit b447e90

Please sign in to comment.