Skip to content

Conversation

@yuji38kwmt
Copy link
Collaborator

@yuji38kwmt yuji38kwmt commented Sep 11, 2025

ロールメンバーごとに挙動が異なるので、ロールメンバーを渡せるようにしました。

@yuji38kwmt yuji38kwmt changed the title add test code utils.can_put_annotation : 引数の追加 Sep 11, 2025
@kci-pr-agent
Copy link

kci-pr-agent bot commented Sep 11, 2025

Title

add test code


Description

  • can_put_annotationにプロジェクトロール対応追加

  • ロール別権限制御とValueError処理実装

  • utils.pyでProjectMemberRoleインポート追加

  • project_member_roleテストを追加


Changes walkthrough 📝

Relevant files
Enhancement
utils.py
can_put_annotationにロール対応追加                                                             

annofabapi/utils.py

  • ProjectMemberRoleインポートを追加
  • can_put_annotationに引数project_member_role追加
  • ロール別アノテーション権限ロジック実装
  • 不正ロール時にValueError発生
  • +11/-5   
    Tests
    test_utils.py
    can_put_annotationテスト追加                                                                   

    tests/test_utils.py

  • ProjectMemberRolecan_put_annotationをインポート
  • project_member_roleシナリオのテスト追加
  • OWNER/ACCEPTER/WORKER動作検証を実装
  • +49/-1   

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @kci-pr-agent
    Copy link

    kci-pr-agent bot commented Sep 11, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    型の不一致

    Taskモデルに対して辞書風アクセスを行っています。Taskクラスの実装でサポートされているか確認してください。

    def can_put_annotation(task: Task, my_account_id: str, *, project_member_role: Optional[ProjectMemberRole] = None) -> bool:
        """
        対象タスクが、`put_annotation` APIで、アノテーションを更新できる状態かどうか。
        過去に担当者が割り当たっている場合、または現在の担当者が自分自身の場合は、アノテーションを更新できる。
    
        Args:
            task: 対象タスク
            my_account_id: 自分(ログインしているユーザ)のアカウントID
            project_member_role: プロジェクトメンバーロール。Noneの場合、プロジェクトオーナであるとみなします。
    
        Returns:
            Trueならば、タスクの担当者を変更せずに`put_annotation` APIを実行できる。
            Falseならば、タスクの担当者を変更してから、`put_annotation` APIを実行する必要がある。
        """
        if project_member_role is None or project_member_role == ProjectMemberRole.OWNER:
            return len(task["histories_by_phase"]) == 0 or task["account_id"] == my_account_id
        elif project_member_role in [ProjectMemberRole.ACCEPTER, ProjectMemberRole.WORKER]:
            return task["account_id"] == my_account_id
        else:
            raise ValueError(f"引数'project_member_role'の値は不正です。 :: {project_member_role=}")
    例外ハンドリング

    不正なproject_member_roleを渡した場合のエラーケースがテストされていません。例外発生の確認テストを追加検討してください。

    else:
        raise ValueError(f"引数'project_member_role'の値は不正です。 :: {project_member_role=}")
    不要なインポート

    TaskPhaseがテスト内で使用されていません。未使用インポートの削除を検討してください。

    from annofabapi.models import ProjectMemberRole, TaskPhase

    "histories_by_phase": [{"phase": "annotation", "phase_stage": 1, "worked": True, "account_id": self.OTHER_ACCOUNT_ID}],
    "account_id": self.OTHER_ACCOUNT_ID,
    }
    actual = can_put_annotation(task, self.MY_ACCOUNT_ID, project_member_role=ProjectMemberRole.OWNER)
    Copy link

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggestion: テスト名ではproject_member_role=Noneのケースを検証すると記載されていますが、実際にはOWNERを渡しているため、引数をNoneに修正してください。 [general, importance: 5]

    Suggested change
    actual = can_put_annotation(task, self.MY_ACCOUNT_ID, project_member_role=ProjectMemberRole.OWNER)
    actual = can_put_annotation(task, self.MY_ACCOUNT_ID, project_member_role=None)

    actual = can_put_annotation(task, self.MY_ACCOUNT_ID, project_member_role=ProjectMemberRole.OWNER)
    assert actual is False

    def test_can_put_annotation_project_member_role_owner_with_history_different_account(self):
    Copy link

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Suggestion: テスト名は“different_account”となっていますが、実際のシナリオは同じアカウントを想定しているため、テスト名を_same_accountに変更してください。 [general, importance: 4]

    Suggested change
    def test_can_put_annotation_project_member_role_owner_with_history_different_account(self):
    def test_can_put_annotation_project_member_role_owner_with_history_same_account(self):

    @yuji38kwmt yuji38kwmt requested a review from Copilot September 11, 2025 14:53
    Copy link

    Copilot AI left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Pull Request Overview

    This PR adds a project_member_role parameter to the can_put_annotation utility function to handle different behaviors based on project member roles. The function previously assumed the user was always a project owner, but now supports different logic for owners, accepters, and workers.

    • Added project_member_role parameter with optional typing and default None behavior
    • Updated function logic to handle different project member roles (OWNER, ACCEPTER, WORKER)
    • Added comprehensive test coverage for the new parameter and different role scenarios

    Reviewed Changes

    Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

    File Description
    annofabapi/utils.py Modified can_put_annotation function to accept and handle project_member_role parameter with role-specific logic
    tests/test_utils.py Added test class TestCanPutAnnotation with comprehensive test cases for different project member roles

    Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

    import dateutil.tz

    from annofabapi.models import Task, TaskHistory, TaskHistoryShort, TaskPhase
    from annofabapi.models import ProjectMemberRole, Task, TaskHistory, TaskHistoryShort, TaskPhase
    Copy link

    Copilot AI Sep 11, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Missing import for Optional type hint used in line 148. Add from typing import Optional to the imports.

    Copilot uses AI. Check for mistakes.
    Comment on lines +156 to +160
    project_member_role: プロジェクトメンバーロール。Noneの場合、プロジェクトオーナであるとみなします。
    Returns:
    Trueならば、タスクの状態を変更せずに`put_annotation` APIを実行できる。
    Trueならば、タスクの担当者を変更せずに`put_annotation` APIを実行できる。
    Falseならば、タスクの担当者を変更してから、`put_annotation` APIを実行する必要がある。
    Copy link

    Copilot AI Sep 11, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    The documentation states that None is treated as project owner, but this behavior change should be more explicitly documented since it affects the function's backward compatibility.

    Copilot uses AI. Check for mistakes.
    @yuji38kwmt yuji38kwmt merged commit af102db into main Sep 11, 2025
    7 checks passed
    @yuji38kwmt yuji38kwmt deleted the add-can-put-annotation branch September 11, 2025 14:56
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    1 participant