Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build/
.pytest_cache/
*.egg
.env
.venv/
26 changes: 15 additions & 11 deletions tasksmd_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def _needs_update(task: Task, board_item: ProjectItem) -> bool:
board_item.assignee,
)
return True
if task.labels and sorted(task.labels) != sorted(board_item.labels):
if sorted(task.labels) != sorted(board_item.labels):
logger.debug(
" [DIFF] '%s' labels: %r != %r",
task.title,
Expand Down Expand Up @@ -458,21 +458,25 @@ def _apply_task_fields(
logger.warning(
"Could not resolve GitHub user '%s' for assignee", task.assignee
)
if task.labels and sorted(task.labels) != sorted(board_item.labels):
if sorted(task.labels) != sorted(board_item.labels):
# We need repo owner/name to resolve label IDs
owner = board_item.repo_owner or client.org
name = board_item.repo_name
if name:
label_ids = client.resolve_label_ids(owner, name, task.labels)
if label_ids:
client.set_issue_labels(board_item.content_id, label_ids)
if task.labels:
label_ids = client.resolve_label_ids(owner, name, task.labels)
if label_ids:
client.set_issue_labels(board_item.content_id, label_ids)
else:
logger.debug(
"Could not resolve label IDs for %r in %s/%s",
task.labels,
owner,
name,
)
else:
logger.debug(
"Could not resolve label IDs for %r in %s/%s",
task.labels,
owner,
name,
)
# Task has no labels — clear all labels from the issue
client.set_issue_labels(board_item.content_id, [])
else:
logger.debug(
"Cannot resolve labels for '%s': no repository information found",
Expand Down
51 changes: 48 additions & 3 deletions tests/test_execute_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,32 @@ def test_unresolvable_labels_does_not_crash(self):
assert result.updated == 1
client.set_issue_labels.assert_not_called()

def test_remove_all_labels_from_issue(self):
"""Labels should be cleared when task has no labels but issue does."""
board = [
_make_board_item(
"PVTI_1",
title="Task",
status="Todo",
content_type="Issue",
content_id="I_1",
labels=["bug", "docs"],
repo_name="tasksmd-sync",
),
]
client = _mock_client(board)
tf = TaskFile(
tasks=[
_make_task("Task", board_id="PVTI_1", labels=[]),
]
)

result = execute_sync(client, tf)

assert result.updated == 1
client.resolve_label_ids.assert_not_called()
client.set_issue_labels.assert_called_once_with("I_1", [])

def test_update_error_recorded(self):
"""Errors during update should be captured in result.errors."""
board = [
Expand Down Expand Up @@ -785,11 +811,11 @@ def test_no_diff_when_task_has_no_assignee(self):
board = _make_board_item("X", title="T", content_type="Issue", assignee="bob")
assert _needs_update(task, board) is False

def test_no_diff_when_task_has_no_labels(self):
"""If task has no labels, board's labels shouldn't cause a diff."""
def test_labels_removed_when_task_has_no_labels(self):
"""If task has no labels but board has labels, it SHOULD cause a diff."""
task = _make_task("T")
board = _make_board_item("X", title="T", content_type="Issue", labels=["bug"])
assert _needs_update(task, board) is False
assert _needs_update(task, board) is True

def test_label_order_irrelevant(self):
"""Labels should be compared as sets (order doesn't matter)."""
Expand Down Expand Up @@ -953,6 +979,25 @@ def test_issue_without_content_id_skips_assignee_labels(self):
client.resolve_user_id.assert_not_called()
client.set_issue_labels.assert_not_called()

def test_issue_clears_labels_when_task_has_none(self):
"""Real Issue should have labels cleared when task has no labels."""
client = _mock_client()
fields = _stub_fields()
task = _make_task("T", labels=[])
bi = _make_board_item(
"PVTI_1",
title="T",
content_type="Issue",
content_id="I_1",
labels=["bug", "docs"],
repo_name="tasksmd-sync",
)

_apply_task_fields(client, "PVTI_1", task, fields, board_item=bi)

client.resolve_label_ids.assert_not_called()
client.set_issue_labels.assert_called_once_with("I_1", [])


# ===================================================================
# _parse_item_node
Expand Down
38 changes: 38 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,41 @@ def test_issue_unchanged_when_labels_match():
plan = build_sync_plan(tf, board)
assert len(plan.unchanged) == 1
assert len(plan.update) == 0


def test_label_removal_triggers_update_for_issue():
"""Removing all labels from a task should trigger an update on a real Issue."""
tf = TaskFile(
tasks=[
_make_task("Task", board_id="PVTI_1", labels=[]),
]
)
board = [
_make_board_item(
"PVTI_1",
title="Task",
content_type="Issue",
labels=["bug", "docs"],
),
]
plan = build_sync_plan(tf, board)
assert len(plan.update) == 1


def test_partial_label_removal_triggers_update_for_issue():
"""Removing some labels from a task should trigger an update on a real Issue."""
tf = TaskFile(
tasks=[
_make_task("Task", board_id="PVTI_1", labels=["bug"]),
]
)
board = [
_make_board_item(
"PVTI_1",
title="Task",
content_type="Issue",
labels=["bug", "docs"],
),
]
plan = build_sync_plan(tf, board)
assert len(plan.update) == 1