Skip to content

Commit 6437188

Browse files
committed
update notes on script update with timestamp
1 parent 2ad35a6 commit 6437188

2 files changed

Lines changed: 70 additions & 5 deletions

File tree

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# git2jamf [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
2-
This action grabs the github repository (or any subdfolder of your choice) scans it for scripts and will create or update those scripts in jamf.
2+
This action grabs the github repository (or any subfolder of your choice) scans it for scripts and will create or update those scripts in jamf.
33

44
It starts by comparing filename of the github script (without the extension) against the name of the script in jamf:
5-
* If it doesn't exist, it will create it
6-
* if it exists, it will compare the hash of the body of both scripts and update it in jamf if they differ. Github is always treated as the source.
7-
* If enabled, it will add a prefix with the `branch name_` to a script.
5+
* If it doesn't exist, it will create it with a timestamped note indicating when it was created
6+
* If it exists, it will compare the hash of the body of both scripts and update it in jamf if they differ. When updating, it will also update the notes with a timestamp of when the script was last updated, preserving any existing custom notes
7+
* If enabled, it will add a prefix with the `branch name_` to a script.
88

99
After creating and updating scripts, if enabled, it can delete any leftover script that is not found in github, thus keeping Github as your one source.
1010

11+
## Notes Management
12+
The action automatically manages notes in Jamf scripts:
13+
- **New scripts**: Get a "created via github action on [timestamp]" note
14+
- **Updated scripts**: Get an "updated via github action on [timestamp]" note added/updated
15+
- **Existing notes**: Custom notes are preserved and GitHub action timestamps are kept at the top
16+
- **Order**: Created timestamp (if present) appears first, followed by updated timestamp, then any custom notes
17+
1118
## Future state
1219
* handle extension attributes.
1320
* slack notifications

action.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import jmespath
88
import hashlib
99
import sys
10+
from datetime import datetime
1011
from loguru import logger
1112

1213
logger.remove()
@@ -207,6 +208,59 @@ def compare_scripts(new, old):
207208
return False
208209

209210

211+
#function to create a creation note with timestamp
212+
@logger.catch
213+
def create_creation_note():
214+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S UTC")
215+
return f"created via github action on {timestamp}"
216+
217+
218+
#function to create or update notes with proper timestamping
219+
@logger.catch
220+
def update_script_notes(existing_notes, action_type="updated"):
221+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S UTC")
222+
action_line = f"{action_type} via github action on {timestamp}"
223+
224+
if not existing_notes:
225+
# No existing notes, just add the action line
226+
return action_line
227+
228+
lines = existing_notes.strip().split('\n')
229+
updated_lines = []
230+
found_created_line = False
231+
found_updated_line = False
232+
233+
# Look for existing github action lines
234+
for line in lines:
235+
line = line.strip()
236+
if line.startswith("created via github action"):
237+
if not found_created_line:
238+
updated_lines.append(line) # Keep the original created line
239+
found_created_line = True
240+
# Skip duplicate created lines
241+
elif line.startswith("updated via github action"):
242+
if not found_updated_line:
243+
updated_lines.append(action_line) # Replace with new updated line
244+
found_updated_line = True
245+
# Skip old updated lines
246+
else:
247+
# Keep other notes
248+
if line: # Only add non-empty lines
249+
updated_lines.append(line)
250+
251+
# If we didn't find an existing updated line, add it after created line (if exists) or at the top
252+
if not found_updated_line:
253+
if found_created_line:
254+
# Insert after the created line
255+
insert_index = 1 if len(updated_lines) > 0 else 0
256+
updated_lines.insert(insert_index, action_line)
257+
else:
258+
# Insert at the beginning
259+
updated_lines.insert(0, action_line)
260+
261+
return '\n'.join(updated_lines)
262+
263+
210264
#retrieves list of files given a folder path and the list of valid file extensions to look for
211265
@logger.catch
212266
def find_local_scripts(script_dir, script_extensions):
@@ -276,7 +330,8 @@ def push_scripts():
276330
logger.info("it doesn't exist, lets create it")
277331
#it doesn't exist, we can create it
278332
with open(script, 'r') as upload_script:
279-
payload = {"name": script_name, "info": "", "notes": "created via github action", "priority": "AFTER" , "categoryId": "1", "categoryName":"", "parameter4":"", "parameter5":"", "parameter6":"", "parameter7":"", "parameter8":"", "parameter9":"", "parameter10":"", "parameter11":"", "osRequirements":"", "scriptContents":f"{upload_script.read()}"}
333+
creation_note = create_creation_note()
334+
payload = {"name": script_name, "info": "", "notes": creation_note, "priority": "AFTER" , "categoryId": "1", "categoryName":"", "parameter4":"", "parameter5":"", "parameter6":"", "parameter7":"", "parameter8":"", "parameter9":"", "parameter10":"", "parameter11":"", "osRequirements":"", "scriptContents":f"{upload_script.read()}"}
280335
create_jamf_script(url, token, payload)
281336
elif len(script_search) == 1:
282337
jamf_script = script_search.pop()
@@ -290,6 +345,9 @@ def push_scripts():
290345
logger.info("the local version is different than the one in jamf, updating jamf")
291346
#the hash of the scripts is not the same, so we'll update it
292347
jamf_script['scriptContents'] = script_text
348+
# Update the notes with timestamp
349+
existing_notes = jamf_script.get('notes', '')
350+
jamf_script['notes'] = update_script_notes(existing_notes, "updated")
293351
update_jamf_script(url, token, jamf_script)
294352
else:
295353
logger.info("we're skipping this one.")

0 commit comments

Comments
 (0)