Skip to content

Commit

Permalink
address TkTech#56 and change commit formatting to look more like the …
Browse files Browse the repository at this point in the history
…git hook
  • Loading branch information
Dav1dde committed Feb 12, 2014
1 parent 4d40e8e commit abbd01d
Showing 1 changed file with 52 additions and 67 deletions.
119 changes: 52 additions & 67 deletions notifico/services/hooks/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,32 @@


class JenkinsConfigForm(wtf.Form):
print_started = wtf.BooleanField('Print Started', validators=[
wtf.Optional()
], default=False, description=(
'If checked, sends a message for every started job.'
phase = wtf.SelectMultipleField('Phase',
default=['finished'],
choices=[
('started', 'Started'),
('completed', 'Completed'),
('finished', 'Finished')
],
description=(
'Print messages for selected fields.'
))

print_completed = wtf.BooleanField('Print Completed', validators=[
wtf.Optional()
], default=False, description=(
'If checked, sends a message for every completed job.'
))

print_finished = wtf.BooleanField('Print Finished', validators=[
wtf.Optional()
], default=True, description=(
'If checked, sends a message for every finished job.'
))

omit_phase = wtf.BooleanField('Omit Phase', validators=[
wtf.Optional()
], default=True, description=(
'If checked, does not add the job\'s current phase to the message. '
'Recommended if only one of the above is checked.'
status = wtf.SelectMultipleField('Status',
default=['success', 'unstable', 'failure'],
choices=[
('success', 'Success'),
('unstable', 'Unstable'),
('failure', 'Failure')
],
description=(
'Print messages for selected fields.'
))

use_colors = wtf.BooleanField('Use Colors', validators=[
wtf.Optional()
], default=True, description=(
'If checked, commit messages will include minor mIRC coloring.'
'If checked, messages will include minor mIRC coloring.'
))


Expand All @@ -62,17 +59,21 @@ def handle_request(cls, user, request, hook):
if not payload:
return

phases = {
'STARTED': hook.config.get('print_started', False),
'COMPLETED': hook.config.get('print_completed', False),
'FINISHED': hook.config.get('print_finished', True)
}
if not phases.get(payload['build']['phase'], False):
phase = payload['build']['phase'].lower()
if not phase in hook.config.get('phase', []):
return

status = payload['build'].get('status', 'SUCCESS').lower()
# yeah documentation of the plugin differs
# from the actual implementation?
if status == 'failed':
status = 'failure'
if not status in hook.config.get('status', []):
return

omit_phase = hook.config.get('omit_phase', False)

strip = not hook.config.get('use_colors', True)
summary = cls._create_summary(payload, omit_phase)
summary = cls._create_summary(payload)

yield cls.message(summary, strip)

Expand All @@ -88,54 +89,38 @@ def _prefix_line(cls, line, payload):
return prefix + line

@classmethod
def _create_summary(cls, payload, omit_phase=False):
def _create_summary(cls, payload):
"""
Create and return a one-line summary of the build
"""
status_colour = {
'SUCCESS': HookService.colors['GREEN'],
'UNSTABLE': HookService.colors['YELLOW'],
'FAILED': HookService.colors['RED']
'UNSTABLE': HookService.colors['ORANGE'],
'FAILURE' : HookService.colors['RED'], # what it really is

This comment has been minimized.

Copy link
@TkTech

TkTech Feb 12, 2014

Comments above the line, not inline please.

'FAILED': HookService.colors['RED'] # according to the docs
}.get(
payload['build'].get('status', 'SUCCESS'),
HookService.colors['RED']
)

lines = []

# Build number
lines.append(u'Jenkins CI - build #{number}'.format(
project=payload['name'],
number=payload['build']['number'],
))

# Status
status = u''
if 'status' in payload['build']:
status = u'{status_colour}{message}{RESET}'.format(
status_colour=status_colour,
message=payload['build']['status'].capitalize(),
**HookService.colors
)

# Current phase
phase = u''
if not omit_phase:
phase = u'{status_colour}{message}{RESET}'.format(
status_colour=status_colour,
message=payload['build']['phase'].capitalize(),
**HookService.colors
)

lines.append(' | '.join(filter(bool, [phase, status])))

# URL to build
lines.append(u'{PINK}{url}{RESET}'.format(
url=payload['build']['full_url'],
**HookService.colors
))
number = payload['build']['number']
phase = payload['build']['phase'].lower()
status = payload['build']['status'].lower()
url = payload['build']['full_url']

line = u' '.join(lines)
fmt_string = (
'{ORANGE}jenkins{RESET} built {status_colour}#{number}{RESET} '
'{phase} ({status_colour}{status}{RESET}) {PINK}{url}{RESET}'
)

line = fmt_string.format(
status_colour=status_colour,
number=number,
phase=phase,
status=status,
url=url,
**HookService.colors
)
return cls._prefix_line(line, payload)

@classmethod
Expand Down

0 comments on commit abbd01d

Please sign in to comment.