Skip to content

Commit

Permalink
Make the Git commit message configurable (#815)
Browse files Browse the repository at this point in the history
* Add configurable commit message suffix
  • Loading branch information
nkallergis authored Oct 31, 2024
1 parent a62ab68 commit 38a6260
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/814.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the Git commit message of GC Jobs to be configurable.
26 changes: 21 additions & 5 deletions nautobot_golden_config/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def gc_repo_prep(job, data):
return current_repos


def gc_repo_push(job, current_repos):
def gc_repo_push(job, current_repos, commit_message=""):
"""Push any work from worker to git repos in Job.
Args:
Expand All @@ -149,8 +149,17 @@ def gc_repo_push(job, current_repos):
f"Pushing {job.Meta.name} results to repo {repo['repo_obj'].base_url}.",
extra={"grouping": "GC Repo Commit and Push"},
)
repo["repo_obj"].commit_with_added(f"{job.Meta.name.upper()} JOB {now}")
if not commit_message:
commit_message = f"{job.Meta.name.upper()} JOB {now}"
repo["repo_obj"].commit_with_added(commit_message)
repo["repo_obj"].push()
job.logger.info(
f'{repo["repo_obj"].nautobot_repo_obj.name}: the new Git repository hash is "{repo["repo_obj"].head}"',
extra={
"grouping": "GC Repo Commit and Push",
"object": repo["repo_obj"].nautobot_repo_obj,
},
)


def gc_repos(func):
Expand All @@ -168,7 +177,7 @@ def gc_repo_wrapper(self, *args, **kwargs):
if kwargs.get("fail_job_on_task_failure"):
raise NornirNautobotException(error_msg) from error
finally:
gc_repo_push(job=self, current_repos=current_repos)
gc_repo_push(job=self, current_repos=current_repos, commit_message=kwargs.get("commit_message"))

return gc_repo_wrapper

Expand Down Expand Up @@ -203,6 +212,13 @@ class GoldenConfigJobMixin(Job): # pylint: disable=abstract-method
"""Reused mixin to be able to set defaults for instance attributes in all GC jobs."""

fail_job_on_task_failure = BooleanVar(description="If any tasks for any device fails, fail the entire job result.")
commit_message = StringVar(
label="Git commit message",
required=False,
description=r"If empty, defaults to `{job.Meta.name.upper()} JOB {now}`.",
min_length=2,
max_length=72,
)

def __init__(self, *args, **kwargs):
"""Initialize the job."""
Expand Down Expand Up @@ -308,7 +324,7 @@ def run(self, *args, **data): # pylint: disable=unused-argument, too-many-branc
failed_jobs.append("Compliance")
except Exception as error: # pylint: disable=broad-exception-caught
error_msg = f"`E3001:` General Exception handler, original error message ```{error}```"
gc_repo_push(job=self, current_repos=current_repos)
gc_repo_push(job=self, current_repos=current_repos, commit_message=data.get("commit_message"))
if len(failed_jobs) > 1:
jobs_list = ", ".join(failed_jobs)
elif len(failed_jobs) == 1:
Expand Down Expand Up @@ -357,7 +373,7 @@ def run(self, *args, **data): # pylint: disable=unused-argument, too-many-branc
failed_jobs.append("Compliance")
except Exception as error: # pylint: disable=broad-exception-caught
error_msg = f"`E3001:` General Exception handler, original error message ```{error}```"
gc_repo_push(job=self, current_repos=current_repos)
gc_repo_push(job=self, current_repos=current_repos, commit_message=data.get("commit_message"))
if len(failed_jobs) > 1:
jobs_list = ", ".join(failed_jobs)
elif len(failed_jobs) == 1:
Expand Down

0 comments on commit 38a6260

Please sign in to comment.