Commit ba846520 authored by Alexis Reigel's avatar Alexis Reigel

Merge branch 'features/import-rules-from-linkbox' into 'master'

Import rules from linkbox project

See merge request !1
parents fc9917af bcab77a6
Pipeline #31318 passed with stage
in 31 seconds
image: ruby:2.5
variables:
DANGER_GITLAB_HOST: git.panter.ch
DANGER_GITLAB_API_BASE_URL: https://git.panter.ch/api/v4
cache:
key: "$CI_COMMIT_REF_SLUG"
paths:
- vendor/ruby
before_script:
- gem install bundler --no-ri --no-rdoc
- bundle install -j $(nproc) --path vendor
rubocop:
script:
- bundle exec rubocop -D
danger-review:
allow_failure: true
only:
variables:
- $DANGER_GITLAB_API_TOKEN
except:
refs:
- master
script:
- git version
- bundle exec danger --fail-on-errors=true
assert-generated-danger-files:
script:
- git version
- git checkout -f $CI_COMMIT_SHA
- bundle exec rake generate
- git diff --exit-code || (printf "\033[0;31mThe Dangerfile 'all/Dangerfile' has not been regenerated. Run 'rake generate_all' and commit the changes.\n" && exit 1)
AllCops:
TargetRubyVersion: 2.5
# frozen_string_literal: true
# include all //Dangerfile
Dir['src/*/**/Dangerfile'].each do |danger_file|
danger_folder = File.dirname(danger_file)
message "Using '#{danger_file}'"
danger.import_dangerfile(path: danger_folder)
end
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end
gem 'danger-gitlab'
gem 'rake'
gem 'rubocop', require: false
GEM
remote: https://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0)
claide (1.0.2)
claide-plugins (0.9.2)
cork
nap
open4 (~> 1.3)
colored2 (3.1.2)
cork (0.3.0)
colored2 (~> 3.1)
danger (5.8.1)
claide (~> 1.0)
claide-plugins (>= 0.9.2)
colored2 (~> 3.1)
cork (~> 0.1)
faraday (~> 0.9)
faraday-http-cache (~> 1.0)
git (~> 1.5)
kramdown (~> 1.5)
no_proxy_fix
octokit (~> 4.7)
terminal-table (~> 1)
danger-gitlab (6.0.0)
danger (~> 5.0)
gitlab (~> 4.2, >= 4.2.0)
faraday (0.15.3)
multipart-post (>= 1.2, < 3)
faraday-http-cache (1.3.1)
faraday (~> 0.8)
git (1.5.0)
gitlab (4.7.0)
httparty (>= 0.14.0)
terminal-table (>= 1.5.1)
httparty (0.16.3)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
jaro_winkler (1.5.1)
kramdown (1.17.0)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)
multi_xml (0.6.0)
multipart-post (2.0.0)
nap (1.1.0)
no_proxy_fix (0.1.2)
octokit (4.13.0)
sawyer (~> 0.8.0, >= 0.5.3)
open4 (1.3.4)
parallel (1.12.1)
parser (2.5.3.0)
ast (~> 2.4.0)
powerpack (0.1.2)
public_suffix (3.0.3)
rainbow (3.0.0)
rake (12.3.1)
rubocop (0.60.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.4.0)
ruby-progressbar (1.10.0)
sawyer (0.8.1)
addressable (>= 2.3.5, < 2.6)
faraday (~> 0.8, < 1.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
unicode-display_width (1.4.0)
PLATFORMS
ruby
DEPENDENCIES
danger-gitlab
rake
rubocop
BUNDLED WITH
1.17.1
[![pipeline status](https://git.panter.ch/open-source/danger-rules/badges/master/pipeline.svg)](https://git.panter.ch/open-source/danger-rules/commits/master)
# Rules for [Danger](https://danger.systems/ruby/)
Provides rules for [Danger](https://danger.systems/ruby/) that ~~can~~ should
:smile: be included in your project.
## Usage
1. Add a `Dangerfile` with the desired rules.
```ruby
# imports all rules
danger.import_dangerfile(gitlab: 'open-source/danger-rules', path: 'rules/all')
# only import one specific rule
danger.import_dangerfile(gitlab: 'open-source/danger-rules', path: 'rules/git/wip_commit')
```
Available rules:
<!-- rules -->
- rules/all
- rules/git/commit\_message\_too\_long
- rules/git/fixup\_commit
- rules/git/master\_merge\_commit
- rules/git/wip\_commit
- rules/merge\_request/issue\_reference
<!-- /rules -->
1. Add the following to your `.gitlab-ci.yml`
```yaml
include: 'https://git.panter.ch/open-source/danger-rules/raw/master/gitlab-ci-include.yml'
```
1. (Alternative): You may also define the review job directly:
```yaml
# The image needs to have ruby installed
# You may also define a specific image for the `danger-review` job if your
# overall image doesn't contain ruby.
image: ruby:2.5
variables:
DANGER_GITLAB_HOST: git.panter.ch
DANGER_GITLAB_API_BASE_URL: https://git.panter.ch/api/v4
danger-review:
allow_failure: true
before_script:
- gem install danger-gitlab --no-document
only:
variables:
- $DANGER_GITLAB_API_TOKEN
except:
refs:
- master
script:
- git version
- bundle exec danger --fail-on-errors=true
```
The groups `panter` and `open-source` on [git.panter.ch](https://git.panter.ch)
already have the `DANGER_GITLAB_API_TOKEN` variable set. If you need to create
a project within another group you need to add the following variable to either
the group or the project.
Those two groups already allow the `@gitlab-bot` user access, so any projects
within those groups are fine already.
1. Add a [Secret
Variable](https://gitlab.com/help/ci/variables/README#variables)
`DANGER_GITLAB_API_TOKEN` with the value from `pass -c git.panter.ch/DANGER_GITLAB_API_TOKEN`
to your GitLab group or project
1. For **non-public** projects that are not within the group `panter` the user
`@gitlab-bot` needs to be added as `guest`.
**IMPORTANT**: Don't re-use this bot for public projects, as the access
token could be extracted and would give access to the non-public projects.
## Contributing
All `Dangerfile`s in any subdirectory starting from `src` are included. The
`Dangerfile`s in `rules` are the generated ones (by `rake generate`). This
directory includes every single `Dangerfile` as well as `all/Dangerfile` which
includes all other existing ones.
If you want to extract common logic you may place library methods in the `lib`
directory, which will be inlined in the generated `Dangerfile`s. Requiring
anything other from `lib` (e.g. third party gem) is currently not supported.
### Create a new `Dangerfile`
1. Create a new Dangerfile with the desired rules, e.g. `git/commit_message_with_typo/Dangerfile`
1. Execute `rake generate`
1. Commit the new file and the newly generated or updated `rules/all/*/**`
# frozen_string_literal: true
REQUIRE_PATTERN = /^require ["'].+?["']$/.freeze
FROZEN_STRING = '# frozen_string_literal: true'
ALL_DANGER_FILE = 'all/Dangerfile'
desc "Generate the complete #{ALL_DANGER_FILE}"
task 'generate:all' do
contents = [
header,
lib_files_content
]
danger_files.each do |file|
contents << inlined_content(file)
end
write_content("rules/#{ALL_DANGER_FILE}", contents)
end
desc 'Generate every single */**/Dangerfile'
task 'generate:single' do
danger_files.each do |file|
contents = [
header,
lib_files_content,
inlined_content(file)
]
output_file = file.sub(%r{^src/}, 'rules/')
write_content(output_file, contents)
end
end
desc 'Documents all available rules in the README'
task 'generate:readme' do
files = [ALL_DANGER_FILE, *danger_files].map do |file|
file
.sub(%r{^src/}, '')
.sub(%r{/Dangerfile$}, '')
.gsub('_', '\_')
.yield_self { |f| "rules/#{f}" }
end
rules_list = files.map { |file| " - #{file}\n" }.join
readme = read_content('README.md')
readme.gsub!(
%r{(<!-- rules -->\n).+?( +<!-- /rules -->)}m,
"\\1#{rules_list}\\2"
)
File.write('README.md', readme)
end
desc 'Generate all Dangerfiles and update the rules description in the README'
task generate: %i[generate:all generate:single generate:readme]
def files(*patterns)
Dir[*patterns].sort
end
def danger_files
files('src/*/**/Dangerfile')
end
def lib_files
files('lib/*.rb')
end
def read_content(file)
File.open(file, 'r:UTF-8', &:read)
end
def write_content(file, contents)
directory = File.dirname(file)
FileUtils.mkdir_p(directory)
File.write(file, "#{contents.join.strip}\n")
end
def clean_content(content)
content
.gsub(FROZEN_STRING, '')
.gsub(REQUIRE_PATTERN, '')
.gsub(/\n\n+/, '')
end
def header
<<~HEADER
#{FROZEN_STRING}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
HEADER
end
def included_file_header(file, content)
"# --> #{file}:\n#{content}\n"
end
def inlined_content(file)
content = read_content(file)
content = clean_content(content)
included_file_header(file, content)
end
def lib_files_content
lib_files.map { |file| inlined_content(file) }
end
danger-review:
image: ruby:2.5
variables:
DANGER_GITLAB_HOST: git.panter.ch
DANGER_GITLAB_API_BASE_URL: https://git.panter.ch/api/v4
allow_failure: true
before_script:
- gem install danger-gitlab --no-document
only:
variables:
- $DANGER_GITLAB_API_TOKEN
except:
refs:
- master
script:
- git version
- danger --fail-on-errors=true
# frozen_string_literal: true
def heredoc(text)
text.gsub(/ +/, ' ')
end
# frozen_string_literal: true
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
# --> lib/heredoc.rb:
def heredoc(text)
text.gsub(/ +/, ' ')
end
# --> src/git/commit_message_too_long/Dangerfile:
git
.commits
.select { |commit| commit.message.split(/\n/).first.length > 50 }
.each do |commit|
warn heredoc(
<<~TEXT
The commit #{commit.sha} `#{commit.message}` is too long. Please adhere \
to the following basic structural rules for creating a meaningful \
commit message: * Keep the first line of your commit message short (not more than 50 chars). \
Many tools such as Github and graphical git clients strip the subject after \
50 characters, and people working on the command line are better off with \
text which is not too wide.
* Keep the second line empty
* Use the rest of the commit message to describe the commit. There’s no \
restriction on the format nor the length, so be as descriptive and \
informative as possible.
TEXT
)
end
# --> src/git/fixup_commit/Dangerfile:
git
.commits
.select { |commit| commit.message =~ /^fixup! / }
.each do |commit|
warn "The commit #{commit.sha} `#{commit.message}` is a fixup commit. " \
'Please squash your commits before merging.'
end
# --> src/git/master_merge_commit/Dangerfile:
if git.commits.any? { |commit| commit.message =~ /^Merge branch 'master'/ }
warn 'Please rebase to get rid of the merge commits in this PR'
end
# --> src/git/wip_commit/Dangerfile:
git
.commits
.select { |commit| commit.message =~ /\bWIP\b/ }
.each do |commit|
warn "The commit #{commit.sha} `#{commit.message}` is a WIP commit. " \
'Please cleanup your git history before merging.'
end
# --> src/merge_request/issue_reference/Dangerfile:
unless gitlab.mr_body =~ %r{/issues/\d+} || gitlab.mr_body =~ /#\d+/
warn heredoc(
<<~TEXT
This merge request does not reference any issues. Please add an \
issue reference to the Description.
TEXT
)
end
# frozen_string_literal: true
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
# --> lib/heredoc.rb:
def heredoc(text)
text.gsub(/ +/, ' ')
end
# --> src/git/commit_message_too_long/Dangerfile:
git
.commits
.select { |commit| commit.message.split(/\n/).first.length > 50 }
.each do |commit|
warn heredoc(
<<~TEXT
The commit #{commit.sha} `#{commit.message}` is too long. Please adhere \
to the following basic structural rules for creating a meaningful \
commit message: * Keep the first line of your commit message short (not more than 50 chars). \
Many tools such as Github and graphical git clients strip the subject after \
50 characters, and people working on the command line are better off with \
text which is not too wide.
* Keep the second line empty
* Use the rest of the commit message to describe the commit. There’s no \
restriction on the format nor the length, so be as descriptive and \
informative as possible.
TEXT
)
end
# frozen_string_literal: true
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
# --> lib/heredoc.rb:
def heredoc(text)
text.gsub(/ +/, ' ')
end
# --> src/git/fixup_commit/Dangerfile:
git
.commits
.select { |commit| commit.message =~ /^fixup! / }
.each do |commit|
warn "The commit #{commit.sha} `#{commit.message}` is a fixup commit. " \
'Please squash your commits before merging.'
end
# frozen_string_literal: true
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
# --> lib/heredoc.rb:
def heredoc(text)
text.gsub(/ +/, ' ')
end
# --> src/git/master_merge_commit/Dangerfile:
if git.commits.any? { |commit| commit.message =~ /^Merge branch 'master'/ }
warn 'Please rebase to get rid of the merge commits in this PR'
end
# frozen_string_literal: true
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
# --> lib/heredoc.rb:
def heredoc(text)
text.gsub(/ +/, ' ')
end
# --> src/git/wip_commit/Dangerfile:
git
.commits
.select { |commit| commit.message =~ /\bWIP\b/ }
.each do |commit|
warn "The commit #{commit.sha} `#{commit.message}` is a WIP commit. " \
'Please cleanup your git history before merging.'
end
# frozen_string_literal: true
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# RUN `rake generate` TO REGENERATE.
# --> lib/heredoc.rb:
def heredoc(text)
text.gsub(/ +/, ' ')
end
# --> src/merge_request/issue_reference/Dangerfile:
unless gitlab.mr_body =~ %r{/issues/\d+} || gitlab.mr_body =~ /#\d+/
warn heredoc(
<<~TEXT
This merge request does not reference any issues. Please add an \
issue reference to the Description.
TEXT
)
end
# frozen_string_literal: true
require './lib/heredoc'
git
.commits
.select { |commit| commit.message.split(/\n/).first.length > 50 }
.each do |commit|
warn heredoc(
<<~TEXT
The commit #{commit.sha} `#{commit.message}` is too long. Please adhere \
to the following basic structural rules for creating a meaningful \
commit message:
* Keep the first line of your commit message short (not more than 50 chars). \
Many tools such as Github and graphical git clients strip the subject after \
50 characters, and people working on the command line are better off with \
text which is not too wide.
* Keep the second line empty
* Use the rest of the commit message to describe the commit. There’s no \
restriction on the format nor the length, so be as descriptive and \
informative as possible.
TEXT
)
end
# frozen_string_literal: true
git
.commits
.select { |commit| commit.message =~ /^fixup! / }
.each do |commit|
warn "The commit #{commit.sha} `#{commit.message}` is a fixup commit. " \
'Please squash your commits before merging.'
end
# frozen_string_literal: true
if git.commits.any? { |commit| commit.message =~ /^Merge branch 'master'/ }
warn 'Please rebase to get rid of the merge commits in this PR'
end
# frozen_string_literal: true
git
.commits
.select { |commit| commit.message =~ /\bWIP\b/ }
.each do |commit|
warn "The commit #{commit.sha} `#{commit.message}` is a WIP commit. " \
'Please cleanup your git history before merging.'
end
# frozen_string_literal: true
require './lib/heredoc'
unless gitlab.mr_body =~ %r{/issues/\d+} || gitlab.mr_body =~ /#\d+/
warn heredoc(
<<~TEXT
This merge request does not reference any issues. Please add an \
issue reference to the Description.
TEXT
)
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment