1
1
"""A module for checking the status of an instrument in relation to it's repo."""
2
+
2
3
import os
3
- from typing import Union
4
+ from typing import List , Tuple , Union
4
5
5
6
from ..communication_utils .ssh_access import (
6
7
SSHAccessUtils ,
@@ -30,6 +31,7 @@ def __init__(self, hostname: str) -> None:
30
31
self ._commits_upstream_not_on_local_enum_messages = None
31
32
32
33
self ._uncommitted_changes_enum = None
34
+ self ._uncommitted_changes_messages = None
33
35
34
36
@property
35
37
def hostname (self ) -> str :
@@ -41,7 +43,7 @@ def hostname(self) -> str:
41
43
"""
42
44
return self ._hostname
43
45
44
- def check_for_uncommitted_changes (self ) -> CHECK :
46
+ def check_for_uncommitted_changes (self ) -> Tuple [ CHECK , List [ any ]] :
45
47
"""Check if there are any uncommitted changes on the instrument via SSH.
46
48
47
49
Args:
@@ -52,7 +54,7 @@ def check_for_uncommitted_changes(self) -> CHECK:
52
54
53
55
"""
54
56
command = f"cd { self .repo_dir } && git status --porcelain"
55
- ssh_process = SSHAccessUtils .run_ssh_commandd (
57
+ ssh_process = SSHAccessUtils .run_ssh_command (
56
58
self .hostname ,
57
59
os .environ ["SSH_CREDENTIALS_USR" ],
58
60
os .environ ["SSH_CREDENTIALS_PSW" ],
@@ -62,20 +64,20 @@ def check_for_uncommitted_changes(self) -> CHECK:
62
64
if os .environ ["DEBUG_MODE" ] == "true" :
63
65
print (f"DEBUG: Running command { command } " )
64
66
65
- # if os.environ["DEBUG_MODE"] == "true":
66
- # print(f"DEBUG: {ssh_process}")
67
-
68
67
if ssh_process ["success" ]:
69
68
status = ssh_process ["output" ]
70
69
71
70
JenkinsUtils .save_git_status (self .hostname , status , os .environ ["WORKSPACE" ])
72
71
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 , []
75
77
else :
76
- return CHECK .FALSE
78
+ return CHECK .FALSE , []
77
79
else :
78
- return CHECK .UNDETERMINABLE
80
+ return CHECK .UNDETERMINABLE , []
79
81
80
82
def get_parent_epics_branch (
81
83
self ,
@@ -91,7 +93,7 @@ def get_parent_epics_branch(
91
93
92
94
"""
93
95
command = f"cd { self .repo_dir } && git log"
94
- ssh_process = SSHAccessUtils .run_ssh_commandd (
96
+ ssh_process = SSHAccessUtils .run_ssh_command (
95
97
hostname ,
96
98
os .environ ["SSH_CREDENTIALS_USR" ],
97
99
os .environ ["SSH_CREDENTIALS_PSW" ],
@@ -131,9 +133,9 @@ def git_branch_comparer(
131
133
else :
132
134
branch_details = ""
133
135
134
- # fetch latest changes from the remote
136
+ # Fetch latest changes from the remote, NOT PULL
135
137
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 (
137
139
hostname ,
138
140
os .environ ["SSH_CREDENTIALS_USR" ],
139
141
os .environ ["SSH_CREDENTIALS_PSW" ],
@@ -143,29 +145,24 @@ def git_branch_comparer(
143
145
if os .environ ["DEBUG_MODE" ] == "true" :
144
146
print (f"DEBUG: Running command { fetch_command } " )
145
147
146
- # if os.environ["DEBUG_MODE"] == "true":
147
- # print(f"DEBUG: {ssh_process_fetch}")
148
-
149
148
if not ssh_process_fetch ["success" ]:
150
149
return (
151
150
CHECK .UNDETERMINABLE ,
152
151
None ,
153
152
)
154
153
155
154
command = f'cd { self .repo_dir } && git log --format="%h %s" { branch_details } '
155
+
156
156
if os .environ ["DEBUG_MODE" ] == "true" :
157
157
print (f"DEBUG: Running command { command } " )
158
158
159
- ssh_process = SSHAccessUtils .run_ssh_commandd (
159
+ ssh_process = SSHAccessUtils .run_ssh_command (
160
160
hostname ,
161
161
os .environ ["SSH_CREDENTIALS_USR" ],
162
162
os .environ ["SSH_CREDENTIALS_PSW" ],
163
163
command ,
164
164
)
165
165
166
- # if os.environ["DEBUG_MODE"] == "true":
167
- # print(f"DEBUG: {ssh_process}")
168
-
169
166
if ssh_process ["success" ]:
170
167
output = ssh_process ["output" ]
171
168
commit_dict = self .split_git_log (output , prefix )
@@ -210,8 +207,7 @@ def split_git_log(self, git_log: str, prefix: str) -> dict:
210
207
if prefix is None or message .startswith (prefix ):
211
208
commit_dict [hash ] = message
212
209
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
+
215
211
def check_instrument (self ) -> dict :
216
212
"""Check if there are any hotfixes or uncommitted changes on AN instrument.
217
213
@@ -222,9 +218,8 @@ def check_instrument(self) -> dict:
222
218
# Examples of how to use the git_branch_comparer function decided to not be used in this iteration of the check
223
219
# Check if any hotfixes run on the instrument with the prefix "Hotfix:"
224
220
# hotfix_commits_enum, hotfix_commits_messages = git_branch_comparer(
225
- # hostname, prefix="Hotfix:")
221
+ # hostname, local_branch, upstream_branch, prefix="Hotfix:")
226
222
227
- # Check if any unpushed commits run on the instrument
228
223
upstream_branch = None
229
224
if os .environ ["UPSTREAM_BRANCH_CONFIG" ] == "hostname" :
230
225
upstream_branch = "origin/" + self .hostname
@@ -239,7 +234,7 @@ def check_instrument(self) -> dict:
239
234
# 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
240
235
upstream_branch = os .environ ["UPSTREAM_BRANCH_CONFIG" ]
241
236
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
243
238
(
244
239
self .commits_upstream_not_on_local_enum ,
245
240
self .commits_upstream_not_on_local_messages ,
@@ -250,30 +245,19 @@ def check_instrument(self) -> dict:
250
245
prefix = None ,
251
246
)
252
247
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
255
249
(
256
250
self .commits_local_not_on_upstream_enum ,
257
251
self .commits_local_not_on_upstream_messages ,
258
252
) = self .git_branch_comparer (
259
253
self .hostname ,
260
254
changes_on = "HEAD" ,
261
- # subtracted_against="origin/" + self.hostname,
262
- subtracted_against = upstream_branch , # for inst scripts repo
255
+ subtracted_against = upstream_branch ,
263
256
prefix = None ,
264
257
)
265
258
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 ()
277
261
278
262
def as_string (self ) -> str :
279
263
"""Return the Instrument object as a string.
0 commit comments