Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions create_worklog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@
ROOT_FOLDER_ID = os.environ["ROOT_FOLDER_ID"]
AUTH = (os.environ["ATLASSIAN_USER"], os.environ["ATLASSIAN_API_TOKEN"])

# In-memory cache for folder lookups: {parent_id: [{"id": "id", "title": "name"}, ...]}
_FOLDER_CACHE = {}

def get_folder_id_by_name(name, parent_id):
# parentId 하위 폴더 조회
url = f"{BASE_URL}/folders/{parent_id}?include-direct-children=true"
headers = {"Accept": "application/json"}
r = requests.get(url, auth=AUTH, headers=headers)
r.raise_for_status()
data = r.json()
# Check cache first
if parent_id in _FOLDER_CACHE:
children = _FOLDER_CACHE[parent_id]
print(f"[CACHE] Using cached folders for parent={parent_id}")
else:
# parentId 하위 폴더 조회
url = f"{BASE_URL}/folders/{parent_id}?include-direct-children=true"
headers = {"Accept": "application/json"}
r = requests.get(url, auth=AUTH, headers=headers)
r.raise_for_status()
data = r.json()

children = data.get("directChildren", {}).get("results", [])
print(f"[DEBUG] Folders under parent={parent_id}: {[c.get('title') for c in children]}")
children = data.get("directChildren", {}).get("results", [])
_FOLDER_CACHE[parent_id] = children
print(f"[DEBUG] Folders under parent={parent_id}: {[c.get('title') for c in children]}")

for f in children:
if f.get("title") == name:
Expand All @@ -42,6 +50,9 @@ def find_or_create_folder(name, parent_id):
if r.status_code in (200, 201):
data = r.json()
print(f"[CREATE] Folder created: {name} (id={data['id']})")
# Update cache with the newly created folder
if parent_id in _FOLDER_CACHE:
_FOLDER_CACHE[parent_id].append({"id": data["id"], "title": name})
return data["id"]
else:
print(f"[ERROR] Failed to create folder: {name}, status={r.status_code}, body={r.text}")
Expand Down
19 changes: 16 additions & 3 deletions test_create_worklog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@
os.environ["ATLASSIAN_USER"] = "test@example.com"
os.environ["ATLASSIAN_API_TOKEN"] = "test_token"

import sys
from unittest.mock import MagicMock

# Mock requests before importing create_worklog
mock_requests = MagicMock()
# Add a mock HTTPError to the mock_requests
class MockHTTPError(Exception):
pass
mock_requests.exceptions.HTTPError = MockHTTPError
sys.modules["requests"] = mock_requests

# requests가 import되기 전에 환경 변수가 설정되었는지 확인하기 위해 여기서 import합니다.
import create_worklog
import requests

class TestCreateWorklog(unittest.TestCase):
def setUp(self):
# Clear cache before each test
create_worklog._FOLDER_CACHE = {}

@patch('create_worklog.requests.get')
def test_get_folder_id_by_name_found(self, mock_get):
Expand Down Expand Up @@ -84,10 +97,10 @@ def test_find_or_create_folder_create_fail(self, mock_post, mock_get_folder):
# 실패한 API 응답을 모의 처리합니다.
mock_response = Mock()
mock_response.status_code = 500
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError
mock_response.raise_for_status.side_effect = mock_requests.exceptions.HTTPError
mock_post.return_value = mock_response

with self.assertRaises(requests.exceptions.HTTPError):
with self.assertRaises(mock_requests.exceptions.HTTPError):
create_worklog.find_or_create_folder("fail_folder", "parent")

@patch('create_worklog.requests.post')
Expand Down