Update sprint_scrum.py

Changed cycles to modules, functionality is working, looking for feedback on how Nick would like this to run
This commit is contained in:
2025-11-17 20:49:40 +00:00
parent 3e26fe1c17
commit 60b0ec826a

View File

@@ -7,7 +7,7 @@ import os
#---------------------------Set Up-------------------------------------------- #---------------------------Set Up--------------------------------------------
PLANE_URL = "https://project-management.cui-secure.us/api/v1/workspaces/midwatch" PLANE_URL = "https://project-management.cui-secure.us/api/v1"
load_dotenv() load_dotenv()
TOKEN = os.getenv("TOKEN") TOKEN = os.getenv("TOKEN")
PROJECT_ID = "2a3b613e-a182-4278-8547-9bd5250bf67f" PROJECT_ID = "2a3b613e-a182-4278-8547-9bd5250bf67f"
@@ -22,7 +22,7 @@ HEADER = {
#---------------------------Testing Grounds------------------------------------ #---------------------------Testing Grounds------------------------------------
# test = requests.get(f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles", headers=HEADER) # test = requests.get(f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/modules", headers=HEADER)
# if test.status_code == 200: # if test.status_code == 200:
# print("Connected to Plane API successfully.") # print("Connected to Plane API successfully.")
@@ -36,7 +36,7 @@ HEADER = {
#-----------------------Functionality----------------------------------------------- #-----------------------Functionality-----------------------------------------------
def get_last_sprint(): def get_last_sprint():
url = f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles" url = f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/modules"
response = requests.get(url, headers=HEADER) response = requests.get(url, headers=HEADER)
data = response.json() data = response.json()
if response.status_code != 200: if response.status_code != 200:
@@ -52,30 +52,25 @@ def get_last_sprint():
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
completed = [] completed = []
active = []
upcoming = [] upcoming = []
for s in sprints: for s in sprints:
start = datetime.fromisoformat(s["start_date"].replace("Z", "+00:00")) raw = s["target_date"]
end = datetime.fromisoformat(s["end_date"].replace("Z", "+00:00"))
end = datetime.strptime(raw, "%Y-%m-%d").replace(tzinfo=timezone.utc)
if end < now: if end < now:
completed.append(s) completed.append(s)
elif start <= now <= end:
active.append(s)
else: else:
upcoming.append(s) upcoming.append(s)
completed.sort(key=lambda s: s["end_date"], reverse=True) completed.sort(key=lambda s: s["target_date"], reverse=True)
if completed: if completed:
last = completed[0] last = completed[0]
print(f"Last completed sprint: {last['name']} (ended {last['end_date']})") print(f"Last completed sprint: {last['name']} (ended {last['target_date']})")
return last return last
elif active:
current = active[0]
print(f"Current sprint in progress: {current['name']} (ends {current['end_date']})")
return current
else: else:
print("No active or completed sprints found.") print("No active or completed sprints found.")
return None return None
@@ -83,7 +78,7 @@ def get_last_sprint():
def get_all_issues(): def get_all_issues():
response = requests.get( response = requests.get(
f"{PLANE_URL}/projects/{TESTING_PROJECT}/issues?expand=state", f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/issues?expand=state",
headers=HEADER headers=HEADER
) )
response.raise_for_status() response.raise_for_status()
@@ -102,12 +97,11 @@ def get_all_issues():
print(f"Retrieved {len(uncompleted)} uncompleted issues from project {TESTING_PROJECT}") print(f"Retrieved {len(uncompleted)} uncompleted issues from project {TESTING_PROJECT}")
return uncompleted return uncompleted
def get_unfinished_issues(sprint, all_issues): def get_unfinished_issues(sprint, all_issues):
sprint_id = sprint['id'] sprint_id = sprint['id']
response = requests.get( response = requests.get(
f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/{sprint_id}/cycle-issues/", f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/modules/{sprint_id}/module-issues/",
headers=HEADER headers=HEADER
) )
response.raise_for_status() response.raise_for_status()
@@ -126,66 +120,38 @@ def get_unfinished_issues(sprint, all_issues):
] ]
print(f"Found {len(unfinished_ids)} unfinished issues in sprint {sprint_id}") print(f"Found {len(unfinished_ids)} unfinished issues in sprint {sprint_id}")
archive_last_sprint = requests.patch(
f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/modules/{sprint_id}/",
headers=HEADER,
json={"status": "completed"}
)
print(f"Patch Last Sprint: {archive_last_sprint.status_code}")
return unfinished_ids return unfinished_ids
def create_cycle_minimal(name="Auto-Sprint"):
url = f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/"
payload = {"name": name}
print("Creating new cycle:", payload)
res = requests.post(url, headers=HEADER, json=payload)
print("Create response:", res.text)
res.raise_for_status()
cycle = res.json()
return cycle
def update_cycle_details(cycle_id, start_date, end_date, description):
url = f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/{cycle_id}"
payload = {
"start_date": start_date,
"end_date": end_date,
"description": description,
"name": f"Auto-Sprint {start_date}"
}
print(f"Updating cycle {cycle_id}:", payload)
res = requests.patch(url, headers=HEADER, json=payload)
print("Patch response:", res.text)
res.raise_for_status()
return res.json()
def attach_issues_to_cycle(cycle_id, issue_ids):
url = f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/{cycle_id}/cycle-issues/"
payload = {"issues": issue_ids}
print(f"Attaching issues to cycle {cycle_id}:", payload)
res = requests.post(url, headers=HEADER, json=payload)
print("Attach response:", res.text)
res.raise_for_status()
return res.json()
def create_new_sprint(last_sprint, unresolved_issue_ids): def create_new_sprint(last_sprint, unresolved_issue_ids):
last_end = datetime.fromisoformat(last_sprint["end_date"].replace("Z", "+00:00")) last_end = datetime.fromisoformat(last_sprint["target_date"])
new_start = last_end + timedelta(days=1) new_start = last_end + timedelta(days=1)
new_end = new_start + timedelta(days=SPRINT_LENGTH) new_end = new_start + timedelta(days=SPRINT_LENGTH)
start_str = new_start.strftime("%Y-%m-%dT00:00:00Z") starting_str = new_start.strftime("%Y-%m-%d")
end_str = new_end.strftime("%Y-%m-%dT23:59:59Z") ending_str = new_end.strftime("%Y-%m-%d")
print("Creating cycle with name only...") print("Creating cycle with name only...")
create_payload = { create_payload = {
"name": f"Auto-Sprint {new_start}" "name": f"Auto-Sprint {starting_str}-{ending_str}",
"description": f"This is an Automated Sprint for the dates {starting_str} to {ending_str}",
"start_date": starting_str,
"target_date": ending_str,
"status":"in-progress"
} }
create_res = requests.post( create_res = requests.post(
f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/", f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/modules/",
headers=HEADER, headers=HEADER,
json=create_payload json=create_payload
) )
@@ -197,27 +163,12 @@ def create_new_sprint(last_sprint, unresolved_issue_ids):
cycle_id = cycle["id"] cycle_id = cycle["id"]
print("Cycle created:", cycle_id) print("Cycle created:", cycle_id)
patch_payload = {
"start_date": "2025-11-15",
"end_date": "2025-11-16",
"description": f"Automated sprint starting {start_str}"
}
print("➡ Patching cycle with:", patch_payload)
patch_res = requests.patch(
f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/{cycle_id}",
headers=HEADER,
json=patch_payload
)
print("Patch response:", patch_res.text)
patch_res.raise_for_status()
patched_cycle = patch_res.json()
if unresolved_issue_ids: if unresolved_issue_ids:
attach_payload = {"issues": unresolved_issue_ids} attach_payload = {"issues": unresolved_issue_ids}
print("Attaching issues:", attach_payload) print("Attaching issues:", attach_payload)
attach_res = requests.post( attach_res = requests.post(
f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/{cycle_id}/cycle-issues/", f"{PLANE_URL}/workspaces/midwatch/projects/{TESTING_PROJECT}/modules/{cycle_id}/module-issues/",
headers=HEADER, headers=HEADER,
json=attach_payload json=attach_payload
) )
@@ -228,7 +179,6 @@ def create_new_sprint(last_sprint, unresolved_issue_ids):
return { return {
"cycle_id": cycle_id, "cycle_id": cycle_id,
"updated": patched_cycle,
"attached_issues": unresolved_issue_ids "attached_issues": unresolved_issue_ids
} }
@@ -242,14 +192,3 @@ last_sprint = get_last_sprint()
get_unfinished_issues(last_sprint, all_uncompleted) get_unfinished_issues(last_sprint, all_uncompleted)
print(create_new_sprint(last_sprint, get_unfinished_issues(last_sprint, all_uncompleted))) print(create_new_sprint(last_sprint, get_unfinished_issues(last_sprint, all_uncompleted)))
# patch_try = requests.patch(f"{PLANE_URL}/projects/{TESTING_PROJECT}/cycles/52261070-73ae-4ac6-aeed-805dfa5fb3d0",
# headers=HEADER,
# json= {
# "name": "Auto-Sprint 2025-11-15",
# "start_date": "2015-11-17",
# "end_date": "2025-11-19"
# })
# print(f"Status: {patch_try.status_code}")
# print(f"Response: {patch_try.text}")