Skip to content

Commit

Permalink
#2 Allow limiting allowed specification URI schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcs committed Apr 2, 2015
1 parent 9b5b559 commit 1947997
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .turnstile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ checks:
- branch-release
- codevalidator
specification:
format: 'jira'
allowed_schemes: ['https']
branch-release:
pattern: '^v(?:\d|\_|\.)+$'
7 changes: 6 additions & 1 deletion docs/user/checks/specification.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Specification Check
-------------------

Checks if a specification uri is valid. This check is ignored for merge commits.
Checks if a specification URI is a valid and absolute. This check is ignored for merge commits.

By default only HTTPS and offline URIs are accepted but you can change the allowed schemes:

specification:
allowed_schemes: ['https', 'ftp']
1 change: 0 additions & 1 deletion turnstile/checks/commit_msg/branch_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def check(user_configuration, repository_configuration, commit_message):
logger.debug('Branch: %s', branch)

check_options = repository_configuration.get('branch-type', {})

allowed = check_options.get('allowed', [])

logger.debug('Allowed Patterns: %s', allowed)
Expand Down
31 changes: 24 additions & 7 deletions turnstile/checks/commit_msg/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ def check(user_configuration, repository_configuration, commit_message):
Jira tickets are validated according to a specific regex
>>> commit_1 = message.CommitMessage('something', 'https://github.com/zalando-bus/turnstile/issues/42 m€sságe')
>>> result_1 = check(None, None, commit_1)
>>> result_1 = check(None, {}, commit_1)
>>> result_1.successful, result_1.details
(True, [])
>>> commit_2 = message.CommitMessage('something', 'invalid-1')
>>> result_2 = check(None, None, commit_2)
>>> result_2 = check(None, {}, commit_2)
>>> result_2.successful, result_2.details
(False, ['invalid-1 is not a valid specification URI.'])
>>> commit_3 = message.CommitMessage('something', 'Merge stuff')
>>> result_3 = check(None, None, commit_3)
>>> result_3 = check(None, {}, commit_3)
Traceback (most recent call last):
...
CheckIgnore
>>> commit_4 = message.CommitMessage('something', 'ftp://example.com/spec')
>>> result_4 = check(None, {'specification': {'allowed_schemes': ['https']}}, commit_4)
>>> result_4.successful, result_4.details
(False, ['ftp is not allowed. Allowed schemes are: https'])
>>> commit_5 = message.CommitMessage('something', 'ftp://example.com/spec')
>>> result_5 = check(None, {'specification': {'allowed_schemes': ['https', 'ftp']}}, commit_5)
>>> result_5.successful, result_5.details
(True, [])
:param user_configuration: User specific configuration
:type user_configuration: git_hooks.common.config.UserConfiguration
:param repository_configuration: Repository specific configuration
Expand All @@ -47,15 +57,22 @@ def check(user_configuration, repository_configuration, commit_message):
logger.debug("Commit is a merge, ignoring.")
raise checks.CheckIgnore

check_options = repository_configuration.get('specification', {})
allowed_schemes = check_options.get('allowed_schemes', ['https', 'offline'])

result = checks.CheckResult()
specification = commit_message.specification
is_valid = specification.valid
is_valid_uri = specification.valid
is_valid_scheme = specification.uri.scheme in allowed_schemes

logger.debug('Specification: %s', specification)
logger.debug("Specification is valid: %s", is_valid)
logger.debug("Specification is valid: %s", is_valid_uri)

result.successful = is_valid
if not is_valid:
result.successful = is_valid_uri and is_valid_scheme
if not is_valid_uri:
result.add_detail('{spec} is not a valid specification URI.'.format(spec=specification))
elif not is_valid_scheme:
template = '{scheme} is not allowed. Allowed schemes are: {allowed}'
result.add_detail(template.format(scheme=specification.uri.scheme, allowed=', '.join(allowed_schemes)))

return result

0 comments on commit 1947997

Please sign in to comment.