Skip to content

Commit

Permalink
feat: Job diff info
Browse files Browse the repository at this point in the history
  • Loading branch information
ismet55555 committed Dec 2, 2022
1 parent 409d927 commit 63fab51
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 47 deletions.
19 changes: 12 additions & 7 deletions yojenkins/cli/cli_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,20 @@ def rebuild(profile: str, token: str, job: str, number: int, url: str, latest: b


@log_to_history
def diff(profile: str, token: str, build_url_1: str, build_url_2: str, type: str, char_ignore: int, no_color: bool,
def diff(profile: str, token: str, build_url_1: str, build_url_2: str, logs: bool, char_ignore: int, no_color: bool,
diff_only: bool, diff_guide: bool) -> None:
"""TODO
"""Get the diff comparison for two builds
Args:
profile: The profile/account to use
token: API Token for Jenkins server
TODO
profile: The profile/account to use
token: API Token for Jenkins server
build_url_1: First build for comparison
build_url_2: Second build for comparison
logs: Compare build logs
char_ignore: Number of characters to ignore at start of each line
no_color: Output diff with no color
diff_only: Only show the lines that have changed
diff_guide: Show diff guide, showing where exactly difference is in line
"""
yj_obj = cu.config_yo_jenkins(profile, token)
yj_obj.build.diff(build_url_1, build_url_2, type, char_ignore, no_color, diff_only, diff_guide)
yj_obj.build.diff(build_url_1, build_url_2, logs, char_ignore, no_color, diff_only, diff_guide)
17 changes: 17 additions & 0 deletions yojenkins/cli/cli_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,20 @@ def parameters(profile: str, token: str, job: str, opt_list: bool, **kwargs) ->
data, data_list = yj_obj.job.parameters(job_name=job)
data = data_list if opt_list else data
cu.standard_out(data, **kwargs)


@log_to_history
def diff(profile: str, token: str, job_1: str, job_2: str, no_color: bool, diff_only: bool, diff_guide: bool) -> None:
"""Get the diff comparison for two jobs
Args:
profile: The profile/account to use
token: API Token for Jenkins server
job_1: First job for comparison (name or url)
job_2: Second job for comparison (name or url)
no_color: Output diff with no color
diff_only: Only show the lines that have changed
diff_guide: Show diff guide, showing where exactly difference is in line
"""
yj_obj = cu.config_yo_jenkins(profile, token)
yj_obj.job.diff(job_1, job_2, no_color, diff_only, diff_guide)
9 changes: 5 additions & 4 deletions yojenkins/cli/cli_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ def standard_out(data: Union[Dict, List],


def is_full_url(url: str) -> bool:
"""TODO Docstring
"""Check if the provided url is a full and valide URL
### DUPLICATE: See yojenkins.utility.utility
Args:
TODO
url: The URL to check
Returns:
TODO
True if full and valid, else False
"""

# TODO: Remove this function from this file
# Do url check within the class, not within the cli to not keep repeating it
# In classes use yojenkins.utility.utility.is_full_url()
Expand Down
17 changes: 9 additions & 8 deletions yojenkins/cli_sub_commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,13 @@ def rebuild(ctx, debug, **kwargs):
@cli_decorators.profile
@click.argument('build-url-1', nargs=1, type=str, required=True)
@click.argument('build-url-2', nargs=1, type=str, required=True)
@click.option('--type',
type=click.Choice(['info', 'logs'], case_sensitive=False),
default="info",
show_default=True,
required=False,
help='Type of diff comparison')
# @click.option('--type',
# type=click.Choice(['info', 'logs'], case_sensitive=False),
# default="info",
# show_default=True,
# required=False,
# help='Type of diff comparison')
@click.option('--logs', type=bool, default=False, required=False, is_flag=True, help='Build logs diff')
@click.option('--char-ignore',
default=0,
type=click.IntRange(0),
Expand All @@ -261,9 +262,9 @@ def diff(debug, **kwargs):
\b
- yojenkins build diff "myFolder/myJob/4" "myFolder/myJob/5"
- yojenkins build diff "myJob/4/console" "myJob/5" --type logs --diff-guide
- yojenkins build diff "myJob/4/console" "myJob/5" --logs --diff-guide
- yojenkins build diff "myJob/5/" "yourJob/8" --diff-only
- yojenkins build diff "myJob/2/" "youJob/2/" --type logs --char-ignore 40
- yojenkins build diff "myJob/2/" "youJob/2/" --logs --char-ignore 40
- yojenkins build diff "myJob/5/" "yourJob/8" --stats-only
"""
Expand Down
33 changes: 33 additions & 0 deletions yojenkins/cli_sub_commands/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,36 @@ def parameters(debug, **kwargs):
"""
set_debug_log_level(debug)
cli_job.parameters(**translate_kwargs(kwargs))


@job.command(short_help='\tFind difference between two jobs')
@cli_decorators.debug
@cli_decorators.profile
@click.argument('job-1', nargs=1, type=str, required=True)
@click.argument('job-2', nargs=1, type=str, required=True)
@click.option('--no-color', type=bool, default=False, required=False, is_flag=True, help='Show output without color')
@click.option('--diff-only',
type=bool,
default=False,
required=False,
is_flag=True,
help='Show only lines that are different')
@click.option('--diff-guide',
type=bool,
default=False,
required=False,
is_flag=True,
help='Show where the difference is in line')
def diff(debug, **kwargs):
"""Get the diff comparison for two jobs
EXAMPLES:
\b
- yojenkins build diff "myFolder/myJob" "myFolder/myJob"
- yojenkins build diff "myJob" "myJob" --diff-guide
- yojenkins build diff "myJob" "yourJob" --diff-only
"""
set_debug_log_level(debug)
cli_job.diff(**translate_kwargs(kwargs))
17 changes: 8 additions & 9 deletions yojenkins/utility/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ def is_full_url(url: str) -> bool:
Returns:
True if valid and full URL, else False
"""

# TODO: Replace this same function in cli_utility.py usages with this one within classes

parsed_url = parse_url(url)
Expand Down Expand Up @@ -1254,14 +1253,14 @@ def diff_show(text_1: str, text_2: str, label_1: str, label_2: str, char_ignore:
"""Display/Show line diffs between two specified texts
Args:
text_1: String text 1
text_2: String text 2 to compare to text 1
label_1: text_1 label/description
label_2: text_2 label/description
text_1: String text 1
text_2: String text 2 to compare to text 1
label_1: text_1 label/description
label_2: text_2 label/description
char_ignore: Number of characters to ignore for comparison at start of each line
no_color: Display with no color
diff_only: Only show lines that are different
diff_guide: Show diff specifiers/guides to show where difference is on line
no_color: Display with no color
diff_only: Only show lines that are different
diff_guide: Show diff specifiers/guides to show where difference is on line
"""
logger.debug('Showing the diff of two provided text strings ...')

Expand Down Expand Up @@ -1302,7 +1301,7 @@ def diff_show(text_1: str, text_2: str, label_1: str, label_2: str, char_ignore:
elif first_char == "-":
color, bold = "red", False
elif first_char == "?":
color, bold = "magenta", True
color, bold = "yellow", True
else:
color, bold = None, False

Expand Down
37 changes: 19 additions & 18 deletions yojenkins/yo_jenkins/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,19 +676,22 @@ def diff(
self,
build_url_1: str = '',
build_url_2: str = '',
type: str = "info",
logs: bool = False,
char_ignore: int = 0,
no_color: bool = False,
diff_only: bool = False,
diff_guide: bool = False,
) -> bool:
"""TODO Docstring
) -> None:
"""Get the diff comparison for two builds
Args:
TODO
Returns:
TODO
build_url_1: First build for comparison
build_url_2: Second build for comparison
logs: Compare build logs
char_ignore: Number of characters to ignore at start of each line
no_color: Output diff with no color
diff_only: Only show the lines that have changed
diff_guide: Show diff guide, showing where exactly difference is in line
"""
build_url_1 = utility.build_url_complete(build_url_1)
if not build_url_1:
Expand All @@ -697,24 +700,15 @@ def diff(
if not build_url_2:
fail_out('Failed to parse provided BUILD_URL_2. Please check specified arguments')

logger.debug(f'Getting build {type.upper()} diff for the following two builds:')
logger.debug(f'Getting build {"LOGS" if logs else "INFO"} diff for the following two builds:')
logger.debug(f' - Build 1: {build_url_1}')
logger.debug(f' - Build 2: {build_url_2}')

logger.debug("Diff output options specified:")
logger.debug(f' - Show no color: {no_color}')
logger.debug(f' - Show diff only: {diff_only}')
logger.debug(f' - Show diff guide: {diff_guide}')

if type.lower() == 'info':
build_info_1 = self.info(build_url=build_url_1)
build_info_2 = self.info(build_url=build_url_2)
build_info_yaml_1 = yaml.safe_dump(build_info_1, default_flow_style=False, indent=2)
build_info_yaml_2 = yaml.safe_dump(build_info_2, default_flow_style=False, indent=2)
diff_show(build_info_yaml_1, build_info_yaml_2, "--- BUILD 1", "+++ BUILD 2", char_ignore, no_color,
diff_only, diff_guide)

elif type.lower() == 'logs':
if logs:
build_logs_1, _, success = self.rest.request(f"{build_url_1.strip('/')}/consoleText",
'get',
is_endpoint=False,
Expand All @@ -731,3 +725,10 @@ def diff(

diff_show(build_logs_1, build_logs_2, "--- BUILD 1", "+++ BUILD 2", char_ignore, no_color, diff_only,
diff_guide)
else:
build_info_1 = self.info(build_url=build_url_1)
build_info_2 = self.info(build_url=build_url_2)
build_info_yaml_1 = yaml.safe_dump(build_info_1, default_flow_style=False, indent=2)
build_info_yaml_2 = yaml.safe_dump(build_info_2, default_flow_style=False, indent=2)
diff_show(build_info_yaml_1, build_info_yaml_2, "--- BUILD 1", "+++ BUILD 2", char_ignore, no_color,
diff_only, diff_guide)
41 changes: 40 additions & 1 deletion yojenkins/yo_jenkins/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

import jenkins
import xmltodict
import yaml

from yojenkins.monitor import JobMonitor
from yojenkins.utility import utility
from yojenkins.utility.utility import fail_out, failures_out
from yojenkins.utility.utility import diff_show, fail_out, failures_out
from yojenkins.yo_jenkins.jenkins_item_classes import JenkinsItemClasses
from yojenkins.yo_jenkins.jenkins_item_config import JenkinsItemConfig

Expand Down Expand Up @@ -803,3 +804,41 @@ def parameters(self, job_name: str = '', job_url: str = '') -> Tuple[list, list]
parameters_list.append(item)

return parameters, parameters_list

def diff(
self,
job_1: str = '',
job_2: str = '',
no_color: bool = False,
diff_only: bool = False,
diff_guide: bool = False,
) -> None:
"""Get the diff comparison for two jobs
Args:
job_1: First job for comparison (name or url)
job_2: Second job for comparison (name or url)
no_color: Output diff with no color
diff_only: Only show the lines that have changed
diff_guide: Show diff guide, showing where exactly difference is in line
"""
logger.debug(f'Getting jobs INFO diff for the following two jobs:')
logger.debug(f' - Job 1: {job_1}')
logger.debug(f' - Job 2: {job_2}')
logger.debug("Diff output options specified:")
logger.debug(f' - Show no color: {no_color}')
logger.debug(f' - Show diff only: {diff_only}')
logger.debug(f' - Show diff guide: {diff_guide}')

if not utility.is_full_url(job_1):
job_info_1 = self.info(job_name=job_1)
else:
job_info_1 = self.info(job_url=job_1)
if not utility.is_full_url(job_2):
job_info_2 = self.info(job_name=job_2)
else:
job_info_2 = self.info(job_url=job_2)

job_info_yaml_1 = yaml.safe_dump(job_info_1, default_flow_style=False, indent=2)
job_info_yaml_2 = yaml.safe_dump(job_info_2, default_flow_style=False, indent=2)
diff_show(job_info_yaml_1, job_info_yaml_2, "--- JOB 1", "+++ JOB 2", 0, no_color, diff_only, diff_guide)

0 comments on commit 63fab51

Please sign in to comment.