Skip to content

Instantly share code, notes, and snippets.

@skatkov
Last active April 10, 2020 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skatkov/8c4e4bc40c208ebbd6ad6004f2a60dc7 to your computer and use it in GitHub Desktop.
Save skatkov/8c4e4bc40c208ebbd6ad6004f2a60dc7 to your computer and use it in GitHub Desktop.
lefthook.yml
#!/bin/bash
BRANCH_CHANGE=$3
[[ $BRANCH_CHANGE -eq 0 ]] && exit
PREV_HEAD=$1
CURR_HEAD=$2
[ $PREV_HEAD == $CURR_HEAD ] && exit
# Don't run bundler if there were no changes in gems
git diff --quiet --exit-code $PREV_HEAD $CURR_HEAD -- Gemfile.lock && exit;
bundle check || bundle install
#!/usr/bin/env ruby
# frozen_string_literal: true
prev_head, curr_head, branch_change, * = ARGV
exit if branch_change == '0' # Don't run on file checkouts
exit if prev_head == curr_head # Don't run if we're still on the same commit
app_dir = File.expand_path('../..', __dir__)
ENV['BUNDLE_GEMFILE'] ||= File.join(app_dir, 'Gemfile')
require 'bundler/setup'
require 'git'
git = Git.open(app_dir)
# Don't run if there were no migrations added between revisions
diff = git.diff(prev_head, curr_head).path('db/migrate')
exit if diff.size.zero?
# Load rails application with rake tasks
require File.expand_path('config/boot', app_dir)
require File.expand_path('config/application', app_dir)
require 'rake'
Rails.application.load_tasks
Rake::Task['db:load_config'].invoke
# migrations added in prev_head (deleted in curr_head) that we need to rollback
rollback_migration_files = diff.select { |file| file.type == 'deleted' }
if rollback_migration_files.any?
require 'tmpdir'
versions = []
Dir.mktmpdir do |directory|
rollback_migration_files.each do |diff_file|
filename = File.basename(diff_file.path)
contents = git.gblob("#{prev_head}:#{diff_file.path}").contents
File.write(File.join(directory, filename), contents)
version = filename.scan(ActiveRecord::Migration::MigrationFilenameRegexp).first&.first
versions.push(version) if version
end
begin
old_migration_paths = ActiveRecord::Migrator.migrations_paths
ActiveRecord::Migrator.migrations_paths.push(directory)
versions.sort.reverse_each do |version|
ENV['VERSION'] = version
Rake::Task['db:migrate:down'].execute
end
ensure
ENV.delete('VERSION')
ActiveRecord::Migrator.migrations_paths = old_migration_paths
end
end
end
# We don't have to do any magic with +up+ migrations
# because we already have them checked out in the working directory
Rake::Task['db:migrate'].invoke
pre-commit:
parallel: true
commands:
eslint:
glob: "*.{js}"
run: yarn prettier --write {staged_files} && yarn eslint {staged_files} && git add {staged_files}
css:
glob: "*.{css}"
run: yarn prettier --write {staged_files} && yarn stylelint --fix {staged_files} && git add {staged_files}
rubocop:
glob: "*.rb"
exclude: "application.rb|routes.rb"
run: bundle exec rubocop -a --fail-level E && git add {staged_files}
pre-push:
parallel: true
commands:
minitest:
tags: backend
run: bundle exec rake test
post-checkout:
piped: true
scripts:
bundle-checkinstall:
tags: backend
db-migrate:
tags: backend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment