Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub actions and danger #114

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0
3.2.2
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ bin/rails g
## Generators

* [Github](./lib/generators/rolemodel/github)
* [Semaphore](./lib/generators/rolemodel/semaphore)
* [CI](./lib/generators/rolemodel/ci)
* [Semaphore](./lib/generators/rolemodel/ci/semaphore)
* [Github](./lib/generators/rolemodel/ci/github)
* [Testing](./lib/generators/rolemodel/testing)
* [RSpec](./lib/generators/rolemodel/testing/rspec)
* [Factory Bot](./lib/generators/rolemodel/testing/factory_bot)
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rolemodel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* [Modals](./modals)
* [README](./readme)
* [SaaS](./saas)
* [Semaphore](./semaphore)
* [CI](./ci)
* [SimpleForm](./simple_form)
* [SoftDestroyable](./soft_destroyable)
* [Source Map](./source_map)
Expand Down
3 changes: 2 additions & 1 deletion lib/generators/rolemodel/all_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AllGenerator < Rails::Generators::Base

def run_all_the_generators
generate 'rolemodel:github'
generate 'rolemodel:semaphore'
generate 'rolemodel:ci'
generate 'rolemodel:heroku'
generate 'rolemodel:readme'
generate 'rolemodel:webpacker'
Expand All @@ -15,6 +15,7 @@ def run_all_the_generators
generate 'rolemodel:saas:all'
generate 'rolemodel:editors'
generate 'rolemodel:linters:all'
generate 'rolemodel:danger'
generate 'rolemodel:mailers'
generate 'rolemodel:source_map'
end
Expand Down
7 changes: 7 additions & 0 deletions lib/generators/rolemodel/ci/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Description:
Sets up a standard CI configuration

Example:
rails generate rolemodel:ci

This will prompt to either run 'rolemodel:ci:semaphore' or 'rolemodel:ci:github'
14 changes: 14 additions & 0 deletions lib/generators/rolemodel/ci/ci_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Rolemodel
class CiGenerator < Rails::Generators::Base
def select_ci_platform
ci_platform = ask('Which CI platform are you using? (semaphore/github)', limited_to: %w[semaphore github])

case ci_platform
when 'semaphore'
generate 'rolemodel:ci:semaphore'
when 'github'
generate 'rolemodel:ci:github'
end
end
end
end
13 changes: 13 additions & 0 deletions lib/generators/rolemodel/ci/github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Github Actions Generator

## What you get

* rspec.yml file to run the relevant CI commands
* jest.yml file to run the relevant CI commands

This is the basic config needed to use github actions.

## Note
This config assumes
- `main` is the base branch
- `yarn test` is the command to run JS tests
9 changes: 9 additions & 0 deletions lib/generators/rolemodel/ci/github/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Description:
Sets up our standard github actions configuration

Example:
rails generate rolemodel:ci:github

This will create:
rspec.yml
jest.yml
12 changes: 12 additions & 0 deletions lib/generators/rolemodel/ci/github/github_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Rolemodel
module Ci
class GithubGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def create_base_github_config
template 'rspec.yml', '.github/workflows/rspec.yml'
template 'rspec.yml', '.github/workflows/jest.yml' if File.exist?(Rails.root.join('package.json'))
end
end
end
end
35 changes: 35 additions & 0 deletions lib/generators/rolemodel/ci/github/templates/rspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "RSpec"
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15-alpine
ports:
- "5432:5432"
env:
POSTGRES_DB: rails_test
POSTGRES_USER: rails
POSTGRES_PASSWORD: password
env:
RAILS_ENV: test
DATABASE_URL: "postgres://rails:password@localhost:5432/rails_test"
Comment on lines +15 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had issues using DATABASE_URL with parallel testing.
Could you change to https://github.com/RoleModel/gimme-credit/blob/main/.github/workflows/ci.yml#L42-L53

It does require some updates to the database.yml for some reason though https://github.com/RoleModel/gimme-credit/blob/main/config/database.yml#L60-L65

steps:
- name: Checkout code
uses: actions/checkout@v3
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
# Add or replace database setup steps here
- name: Set up database schema
run: bin/rails db:schema:load
# Add or replace test runners here
- name: Run tests
run: bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Description:
Sets up our standard semaphore configuration

Example:
rails generate rolemodel:semaphore
rails generate rolemodel:ci:semaphore

This will create:
semaphore.yml
Expand Down
27 changes: 27 additions & 0 deletions lib/generators/rolemodel/ci/semaphore/semaphore_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Rolemodel
module Ci
class SemaphoreGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def create_base_semaphore_config
@project_name = Rails.application.class.try(:parent_name) || Rails.application.class.module_parent_name
template 'semaphore.yml.erb', '.semaphore/semaphore.yml'

if yes?('Does your project have JavaScript tests and/or eslint?')
uncomment_lines('.semaphore/semaphore.yml', '- yarn test')
uncomment_lines('.semaphore/semaphore.yml', '- yarn run eslint')
end
end

def create_deplyment_commands
default_heroku_prefix = (Rails.application.class.try(:parent_name) || Rails.application.class.module_parent_name).underscore.dasherize

@heroku_prefix = ask('Enter the heroku project prefix', default: default_heroku_prefix)

template 'heroku-deployment-commands.sh', '.semaphore/heroku-deployment-commands.sh'
template 'staging-deploy.yml.erb', '.semaphore/staging-deploy.yml'
template 'production-deploy.yml.erb', '.semaphore/production-deploy.yml'
end
end
end
end
2 changes: 1 addition & 1 deletion lib/generators/rolemodel/css/base/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def use_webpacker_styles_in_layout
layouts = [ 'erb', 'slim' ].each do |template_language|
layout_file = "app/views/layouts/application.html.#{template_language}"

next unless File.exists? layout_file
next unless File.exist? layout_file
gsub_file layout_file, "stylesheet_link_tag 'application', media: 'all'", "stylesheet_pack_tag 'stylesheets'"
end
end
Expand Down
16 changes: 16 additions & 0 deletions lib/generators/rolemodel/danger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Danger Generator

## What you get

* danger.yml file to run danger itself
* .danger/base.rb file to get basic review PR comments
* .danger/rubocop.rb file to get rubocop PR comments
* .danger/eslint.rb file to get eslint PR comments
* .danger/brakeman.rb file to get brakeman PR comments

This is the basic config needed to use semaphore.

## Note
This config assumes
- `main` is the base branch
- `yarn test` is the command to run JS tests
12 changes: 12 additions & 0 deletions lib/generators/rolemodel/danger/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Description:
Sets up our standard danger configurations

Example:
rails generate rolemodel:danger

This will create:
Dangerfile
.danger/base.rb
.danger/brakeman.rb
.danger/eslint.rb
.danger/rubocop.rb
38 changes: 38 additions & 0 deletions lib/generators/rolemodel/danger/danger_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Rolemodel
class DangerGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def create_base_danger_config
copy_file 'Dangerfile', 'Dangerfile'
copy_file 'base.rb', '.danger/base.rb'
gem_group :development, :test do
gem 'danger'
end
end

def create_github_action
copy_file 'danger.yml', '.github/workflows/danger.yml'
end

def create_rubocop_config
unless File.exist?(Rails.root.join('.rubocop.yml'))
return unless yes?('Do you want to install rubocop?')
generate 'rolemodel:linters:rubocop'
end
gem_group :development, :test do
gem 'danger-rubocop'
end
copy_file 'rubocop.rb', '.danger/rubocop.rb'
end

def create_brakeman_config
return unless yes?('Do you want to install brakeman?')

gem_group :development, :test do
gem 'brakeman'
gem 'danger-brakeman'
end
copy_file 'brakeman.rb', '.danger/brakeman.rb'
end
end
end
22 changes: 22 additions & 0 deletions lib/generators/rolemodel/danger/templates/Dangerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

# https://evilmartians.com/chronicles/danger-on-rails-make-robots-do-some-code-review-for-you

# Shared consts
CHANGED_FILES = (git.added_files + git.modified_files).freeze
ADDED_FILES = git.added_files.freeze

Dir[File.join(__dir__, '.danger/*.rb')].each do |danger_rule_file|
danger_rule = danger_rule_file.gsub(%r{(^./.danger/|.rb)}, '')
$stdout.print "- #{danger_rule} "
# execute each check using `eval`
eval File.read(danger_rule_file), binding, File.expand_path(danger_rule_file) # rubocop:disable Security/Eval
$stdout.puts '✅'
# allow a single check to fail without breaking others
rescue Exception => e # rubocop:disable Lint/RescueException
$stdout.puts '💥'

# make sure the result is a failure if some check failed to execute
raise "Danger rule :#{danger_rule} failed with exception: #{e.message}\n" \
"Backtrace: \n#{e.backtrace.join("\n")}"
end
17 changes: 17 additions & 0 deletions lib/generators/rolemodel/danger/templates/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Danger uses 'fail' as part of their DSL, so we need to disable this cop
# rubocop:disable Style/SignalException

declared_trivial = github.pr_title.include? '#trivial' # rubocop:disable Lint/UselessAssignment

# Make it more obvious that a PR is a work in progress and shouldn't be merged yet
warn('PR is classed as Work in Progress') if github.pr_title.include? '[WIP]'

# Warn when there is a big PR
warn('Big PR') if git.lines_of_code > 500

# Don't let testing shortcuts get into master by accident
fail('fdescribe left in tests') if `grep -r fdescribe spec/**/*_spec.rb `.length > 1
fail('fit left in tests') if `grep -r fit spec/**/*_spec.rb `.length > 1
fail('focus left in tests') if `grep -r focus spec/**/*_spec.rb `.length > 1

# rubocop:enable Style/SignalException
1 change: 1 addition & 0 deletions lib/generators/rolemodel/danger/templates/brakeman.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
brakeman.lint
22 changes: 22 additions & 0 deletions lib/generators/rolemodel/danger/templates/danger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: "Danger"
on:
push:
branches: ["master"]
pull_request:
branches: ["master"]
jobs:
danger:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' # if only run pull request when multiple trigger workflow
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Running the Dangerfile for this repo
run: bundle exec danger --verbose
env:
DANGER_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUNNING_IN_ACTIONS: true
19 changes: 19 additions & 0 deletions lib/generators/rolemodel/danger/templates/rubocop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rubocop.lint(
# Comment on individual files on the PR
inline_comment: true,

# New option to use the severity reported by Rubocop, e.g. errors
# will fail the build.
# https://github.com/ashfurrow/danger-rubocop/pull/61
report_severity: true,

# Allowed Danger-Rubocop to fail the build, but it reported all offenses
# as failures if true, or all as warnings if fails / not provided. The
# option above allows for better specificity.
# file_on_inline_comment: true,

# Empty string will report on all files, even those not directly touched
# in the current PR. This is useful in case a change impacts other files,
# such as modifying the Rubocop config.
files: '',
)
2 changes: 1 addition & 1 deletion lib/generators/rolemodel/modals/modals_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ModalsGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)

def install_icons
generate 'rolemodel:css:base' unless File.exists?(Rails.root.join('app/javascript/packs/stylesheets.scss'))
generate 'rolemodel:css:base' unless File.exist?(Rails.root.join('app/javascript/packs/stylesheets.scss'))
generate 'rolemodel:css:icons'
end

Expand Down
25 changes: 0 additions & 25 deletions lib/generators/rolemodel/semaphore/semaphore_generator.rb

This file was deleted.