Skip to content

Commit

Permalink
Fix bug #196 && prepare v8.0.0 release (#197)
Browse files Browse the repository at this point in the history
* Fix bug #196

The call to `empty?` will fail if the key GITHUB_HEAD_REF does not exist.

Before fixing this problem, I extracted the logic to its own testable class. Then I added tests, which also showed that `GITHUB_REF_NAME` would never be returned either (a previously unknown bug) because `!@env["GITHUB_HEAD_REF"]&.empty?` will always be truthy unless the key exists and it's empty.

This would (possibly) be cleaner with pattern matching. Ruby has pattern matching but we can't use it yet due to minimum supported Ruby version https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rdoc.html.

Close #196

* v8.0.0
  • Loading branch information
schneems authored Feb 27, 2023
1 parent d0a96b0 commit 411b462
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD

## 8.0.0

- Breaking change: Delete apps on teardown. Previously hatchet would delete apps lazily to help with debugging. This behavior allowed developers to inspect logs and `heroku run bash` in the event of an unexpected failure. In practice, it is rarely needed and causes accounts to retain apps indefinitely. Previously there was no cost to retaining applications, but now `basic` applications incur a charge. Change details:
- The application teardown process now deletes applications directly.
- To skip destroying applications on teardown, set `HEROKU_DEBUG_EXPENSIVE=1`. This env var will cause `App#teardown!` to skip deletion so you can introspect why one failed.
Expand Down
40 changes: 27 additions & 13 deletions lib/hatchet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,43 @@ module Hatchet
require 'hatchet/init_project'
require 'hatchet/heroku_run'

module Hatchet
RETRIES = Integer(ENV['HATCHET_RETRIES'] || 1)
Runner = Hatchet::GitApp
class DefaultCIBranch
def initialize(env: ENV)
@env = env
end

def self.git_branch
def call
# https://circleci.com/docs/variables
return ENV['CIRCLE_BRANCH'] if ENV['CIRCLE_BRANCH']
return @env['CIRCLE_BRANCH'] if @env['CIRCLE_BRANCH']
# https://docs.github.com/en/actions/learn-github-actions/environment-variables
# GITHUB_HEAD_REF is provided for PRs, but blank for branch actions.
return ENV['GITHUB_HEAD_REF'] if !ENV['GITHUB_HEAD_REF'].empty?
return @env['GITHUB_HEAD_REF'] if @env['GITHUB_HEAD_REF'] && !@env['GITHUB_HEAD_REF']&.empty?
# GITHUB_REF_NAME is incorrect on PRs (`1371/merge`), but correct for branch actions.
return ENV['GITHUB_REF_NAME'] if ENV['GITHUB_REF_NAME']
return @env['GITHUB_REF_NAME'] if @env['GITHUB_REF_NAME']
# https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-variables
return ENV['HEROKU_TEST_RUN_BRANCH'] if ENV['HEROKU_TEST_RUN_BRANCH']
return @env['HEROKU_TEST_RUN_BRANCH'] if @env['HEROKU_TEST_RUN_BRANCH']
# TRAVIS_BRANCH works fine unless the build is a pull-request. In that case, it will contain the target branch
# not the actual pull-request branch! TRAVIS_PULL_REQUEST_BRANCH contains the correct branch but will be empty
# for push builds. See: https://docs.travis-ci.com/user/environment-variables/
return ENV['TRAVIS_PULL_REQUEST_BRANCH'] if ENV['TRAVIS_PULL_REQUEST_BRANCH'] && !ENV['TRAVIS_PULL_REQUEST_BRANCH'].empty?
return ENV['TRAVIS_BRANCH'] if ENV['TRAVIS_BRANCH']
return @env['TRAVIS_PULL_REQUEST_BRANCH'] if @env['TRAVIS_PULL_REQUEST_BRANCH'] && !@env['TRAVIS_PULL_REQUEST_BRANCH']&.empty?
return @env['TRAVIS_BRANCH'] if @env['TRAVIS_BRANCH']
end
end

out = `git rev-parse --abbrev-ref HEAD`.strip
raise "Attempting to find current branch name. Error: Cannot describe git: #{out}" unless $?.success?
out
module Hatchet
RETRIES = Integer(ENV['HATCHET_RETRIES'] || 1)
Runner = Hatchet::GitApp

def self.git_branch
branch = DefaultCIBranch.new.call

if branch
branch
else
out = `git rev-parse --abbrev-ref HEAD`.strip
raise "Attempting to find current branch name. Error: Cannot describe git: #{out}" unless $?.success?
out
end
end

if ENV["HATCHET_DEBUG_DEADLOCK"]
Expand Down
2 changes: 1 addition & 1 deletion lib/hatchet/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Hatchet
VERSION = "7.4.0"
VERSION = "8.0.0"
end
27 changes: 27 additions & 0 deletions spec/unit/default_ci_branch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

require "spec_helper"

describe "DefaultCIBranch" do
it "doesn't error on empty env" do
out = DefaultCIBranch.new(env: {}).call
expect(out).to be_nil
end

it "GitHub PRs" do
out = DefaultCIBranch.new(env: {"GITHUB_HEAD_REF" => "iAmaPR"}).call
expect(out).to eq("iAmaPR")

out = DefaultCIBranch.new(env: {"GITHUB_HEAD_REF" => ""}).call
expect(out).to be_nil
end

it "GitHub branches" do
out = DefaultCIBranch.new(env: {"GITHUB_REF_NAME" => "iAmaBranch"}).call
expect(out).to eq("iAmaBranch")
end

it "heroku" do
out = DefaultCIBranch.new(env: {"HEROKU_TEST_RUN_BRANCH" => "iAmaBranch"}).call
expect(out).to eq("iAmaBranch")
end
end

0 comments on commit 411b462

Please sign in to comment.