Skip to content

Instantly share code, notes, and snippets.

@danielgomezrico
Created January 8, 2022 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgomezrico/2652ffb721b658ff18af677265d97efd to your computer and use it in GitHub Desktop.
Save danielgomezrico/2652ffb721b658ff18af677265d97efd to your computer and use it in GitHub Desktop.
DangerFile - Danger System - script to check conventional commits titles and size of the PR
# https://www.regextester.com/109925
CONVENTIONS_URL = "your link"
def validate_title_format
def valid_title_format?(title)
conventional_commit_regex = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?: .*$/
result = title =~ conventional_commit_regex
result != nil
end
def is_capitalized?(text)
/[[:upper:]]/.match(text)
end
def find_title_error(title)
if !valid_title_format?(title)
return """
# Issue
- This PR title \"#{title}\" does not follow our conventions
## Details
- Check [PR Conventions](#{CONVENTIONS_URL})
- Allowed tags: `build`, `chore`, `ci`, `docs`, `feat`, `fix`, `perf`, `refactor`, `revert`, `style`, `test`
"""
end
target = title.split("(")[1].split(")").first
if is_capitalized?(target[0])
description = title.split(":")[1].strip
if is_capitalized?(description[0])
return "- The description **'#{description}'** should not have the first letter capitalized."
end
else
return "- The target **'#{target}'** should have the first letter capitalized."
end
return nil
end
title_error = find_title_error(github.pr_title.to_s)
if title_error == nil
message "Congrats! The PR does follow [PR Conventions](#{CONVENTIONS_URL}) :)"
else
fail title_error
end
end
def validate_title_links
def valid_title_clubhouse_card?(title)
# [ch-<number>]
clubhouse_regex = /(\[ch-)(\d)*(\])/
shortcut_regex = /(\[sc-)(\d)*(\])/
result = title =~ clubhouse_regex || title =~ shortcut_regex
result != nil
end
pr_title = github.pr_title.to_s
if !valid_title_clubhouse_card?(pr_title)
warn "This PR title does not have the clubhouse card id (format: [ch-<number>])"
end
end
def warn_changed_lines_count
lines_count = git.lines_of_code
if lines_count > 140
link = "https://docs.velocity.codeclimate.com/en/articles/2913568-pull-request-size"
warn "This PR **looks pretty big**, it modifies #{lines_count} lines of code and top performant teams handle that in less than 140 lines per PR ([Velocity Docs](link))."
else
message "Congrats! This PR **is small**! it modifies #{lines_count} lines of code."
end
end
message "Welcome #{github.pr_author}. Check the [PR Conventions](#{CONVENTIONS_URL}) :)"
validate_title_format
validate_title_links
warn_changed_lines_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment