11"""A module for checking the status of an instrument in relation to it's repo."""
2+
23import os
3- from typing import Union
4+ from typing import List , Tuple , Union
45
56from ..communication_utils .ssh_access import (
67 SSHAccessUtils ,
@@ -30,6 +31,7 @@ def __init__(self, hostname: str) -> None:
3031 self ._commits_upstream_not_on_local_enum_messages = None
3132
3233 self ._uncommitted_changes_enum = None
34+ self ._uncommitted_changes_messages = None
3335
3436 @property
3537 def hostname (self ) -> str :
@@ -41,7 +43,7 @@ def hostname(self) -> str:
4143 """
4244 return self ._hostname
4345
44- def check_for_uncommitted_changes (self ) -> CHECK :
46+ def check_for_uncommitted_changes (self ) -> Tuple [ CHECK , List [ any ]] :
4547 """Check if there are any uncommitted changes on the instrument via SSH.
4648
4749 Args:
@@ -52,7 +54,7 @@ def check_for_uncommitted_changes(self) -> CHECK:
5254
5355 """
5456 command = f"cd { self .repo_dir } && git status --porcelain"
55- ssh_process = SSHAccessUtils .run_ssh_commandd (
57+ ssh_process = SSHAccessUtils .run_ssh_command (
5658 self .hostname ,
5759 os .environ ["SSH_CREDENTIALS_USR" ],
5860 os .environ ["SSH_CREDENTIALS_PSW" ],
@@ -62,20 +64,20 @@ def check_for_uncommitted_changes(self) -> CHECK:
6264 if os .environ ["DEBUG_MODE" ] == "true" :
6365 print (f"DEBUG: Running command { command } " )
6466
65- # if os.environ["DEBUG_MODE"] == "true":
66- # print(f"DEBUG: {ssh_process}")
67-
6867 if ssh_process ["success" ]:
6968 status = ssh_process ["output" ]
7069
7170 JenkinsUtils .save_git_status (self .hostname , status , os .environ ["WORKSPACE" ])
7271
73- if status .strip () != "" :
74- return CHECK .TRUE
72+ status_stripped = status .strip ()
73+ if status_stripped != "" and os .environ ["SHOW_UNCOMMITTED_CHANGES_MESSAGES" ] == "true" :
74+ return CHECK .TRUE , status_stripped .split ("\n " )
75+ elif status_stripped != "" :
76+ return CHECK .TRUE , []
7577 else :
76- return CHECK .FALSE
78+ return CHECK .FALSE , []
7779 else :
78- return CHECK .UNDETERMINABLE
80+ return CHECK .UNDETERMINABLE , []
7981
8082 def get_parent_epics_branch (
8183 self ,
@@ -91,7 +93,7 @@ def get_parent_epics_branch(
9193
9294 """
9395 command = f"cd { self .repo_dir } && git log"
94- ssh_process = SSHAccessUtils .run_ssh_commandd (
96+ ssh_process = SSHAccessUtils .run_ssh_command (
9597 hostname ,
9698 os .environ ["SSH_CREDENTIALS_USR" ],
9799 os .environ ["SSH_CREDENTIALS_PSW" ],
@@ -131,9 +133,9 @@ def git_branch_comparer(
131133 else :
132134 branch_details = ""
133135
134- # fetch latest changes from the remote
136+ # Fetch latest changes from the remote, NOT PULL
135137 fetch_command = f"cd { self .repo_dir } && git fetch origin"
136- ssh_process_fetch = SSHAccessUtils .run_ssh_commandd (
138+ ssh_process_fetch = SSHAccessUtils .run_ssh_command (
137139 hostname ,
138140 os .environ ["SSH_CREDENTIALS_USR" ],
139141 os .environ ["SSH_CREDENTIALS_PSW" ],
@@ -143,29 +145,24 @@ def git_branch_comparer(
143145 if os .environ ["DEBUG_MODE" ] == "true" :
144146 print (f"DEBUG: Running command { fetch_command } " )
145147
146- # if os.environ["DEBUG_MODE"] == "true":
147- # print(f"DEBUG: {ssh_process_fetch}")
148-
149148 if not ssh_process_fetch ["success" ]:
150149 return (
151150 CHECK .UNDETERMINABLE ,
152151 None ,
153152 )
154153
155154 command = f'cd { self .repo_dir } && git log --format="%h %s" { branch_details } '
155+
156156 if os .environ ["DEBUG_MODE" ] == "true" :
157157 print (f"DEBUG: Running command { command } " )
158158
159- ssh_process = SSHAccessUtils .run_ssh_commandd (
159+ ssh_process = SSHAccessUtils .run_ssh_command (
160160 hostname ,
161161 os .environ ["SSH_CREDENTIALS_USR" ],
162162 os .environ ["SSH_CREDENTIALS_PSW" ],
163163 command ,
164164 )
165165
166- # if os.environ["DEBUG_MODE"] == "true":
167- # print(f"DEBUG: {ssh_process}")
168-
169166 if ssh_process ["success" ]:
170167 output = ssh_process ["output" ]
171168 commit_dict = self .split_git_log (output , prefix )
@@ -210,8 +207,7 @@ def split_git_log(self, git_log: str, prefix: str) -> dict:
210207 if prefix is None or message .startswith (prefix ):
211208 commit_dict [hash ] = message
212209 return commit_dict
213-
214- # TODO: Improve this function to allow selecting of what checks to run and combine the uncommited one to this function
210+
215211 def check_instrument (self ) -> dict :
216212 """Check if there are any hotfixes or uncommitted changes on AN instrument.
217213
@@ -222,9 +218,8 @@ def check_instrument(self) -> dict:
222218 # Examples of how to use the git_branch_comparer function decided to not be used in this iteration of the check
223219 # Check if any hotfixes run on the instrument with the prefix "Hotfix:"
224220 # hotfix_commits_enum, hotfix_commits_messages = git_branch_comparer(
225- # hostname, prefix="Hotfix:")
221+ # hostname, local_branch, upstream_branch, prefix="Hotfix:")
226222
227- # Check if any unpushed commits run on the instrument
228223 upstream_branch = None
229224 if os .environ ["UPSTREAM_BRANCH_CONFIG" ] == "hostname" :
230225 upstream_branch = "origin/" + self .hostname
@@ -239,7 +234,7 @@ def check_instrument(self) -> dict:
239234 # if the UPSTREAM_BRANCH_CONFIG is not set to any of the above, set it to the value of the environment variable assuming user wants custom branch
240235 upstream_branch = os .environ ["UPSTREAM_BRANCH_CONFIG" ]
241236
242- # Check if any upstream commits are not on the instrument, default to the parent origin branch, either main or galil-old
237+ # Check if any commits on upstream that are not on the local branch
243238 (
244239 self .commits_upstream_not_on_local_enum ,
245240 self .commits_upstream_not_on_local_messages ,
@@ -250,30 +245,19 @@ def check_instrument(self) -> dict:
250245 prefix = None ,
251246 )
252247
253- # print(self.commits_upstream_not_on_local_enum, self.commits_upstream_not_on_local_messages)
254-
248+ # Check if any commits on local branch that are not on the upstream
255249 (
256250 self .commits_local_not_on_upstream_enum ,
257251 self .commits_local_not_on_upstream_messages ,
258252 ) = self .git_branch_comparer (
259253 self .hostname ,
260254 changes_on = "HEAD" ,
261- # subtracted_against="origin/" + self.hostname,
262- subtracted_against = upstream_branch , # for inst scripts repo
255+ subtracted_against = upstream_branch ,
263256 prefix = None ,
264257 )
265258
266- # Check if any uncommitted changes run on the instrument
267- self .uncommitted_changes_enum = self .check_for_uncommitted_changes ()
268-
269- # # return the result of the checks
270- # instrument_status = {
271- # "commits_not_pushed_messages": commits_local_not_on_upstream_messages,
272- # "commits_not_pushed": commits_local_not_on_upstream_enum,
273- # "uncommitted_changes": uncommitted_changes_enum,
274- # }
275-
276- # return instrument_status
259+ # Check if any uncommitted changes are on the instrument
260+ self .uncommitted_changes_enum , self .uncommitted_changes_messages = self .check_for_uncommitted_changes ()
277261
278262 def as_string (self ) -> str :
279263 """Return the Instrument object as a string.
0 commit comments