Delete a comment の実装 : hkob の雑記録 (480)

はじめに

hkob の雑記録の第480回目(連続53日目)は、Delete a comment をテスト・実装します。

スクリプトの作成

Update で使ってきたコメントを削除してみます。スクリプトは以下のようになります。

#!/bin/sh
curl -X DELETE 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \
  -H 'Notion-Version: 2025-09-03' \
  -H 'Authorization: Bearer '"$NOTION_API_KEY"''

Block などは is_archived などのフラグが立つだけですが、コメントは完全に消えるようです。削除したコメントの JSON が返却されてきました。

{
  "object": "comment",
  "id": "37921658-0840-45ae-924a-d9ce1d60375b",
  "parent": {
    "type": "page_id",
    "page_id": "c01166c6-13ae-45cb-b968-18b4ef2f5a77"
  },
  "discussion_id": "44753616-4099-4a5f-97c6-53eb758e7a9d",
  "created_time": "2022-09-01T23:42:00.000Z",
  "last_edited_time": "2026-04-29T11:44:00.000Z",
  "created_by": {
    "object": "user",
    "id": "019a87c7-d197-44a4-b19a-baa684400f81"
  },
  "rich_text": [
    {
      "type": "text",
      "text": {
        "content": "update comment by markdown",
        "link": null
      },
      "annotations": {
        "bold": false,
        "italic": false,
        "strikethrough": false,
        "underline": false,
        "code": false,
        "color": "default"
      },
      "plain_text": "update comment by markdown",
      "href": null
    }
  ],
  "display_name": {
    "type": "integration",
    "resolved_name": "notion_ruby_mapping"
  },
  "request_id": "1e2397fb-5d80-4096-93ef-5ad3a2bcbd97"
}

テストを作成

Block の削除を参考にテストを記述しました。is_archived はないので、その部分だけ削除しています。

    describe "destroy" do
      let(:id) { TestConnection::EDIT_TEXT_COMMENT_ID }
      let(:target) { CommentObject.find id }

      context "dry_run" do
        let(:dry_run) { target.destroy dry_run: true }

        it_behaves_like "dry run", :delete, :comment_path, use_id: true
      end

      context "delete" do
        let(:deleted_item) { target.destroy }

        it "receive id" do
          expect(deleted_item.id).to eq id
        end
      end
    end

実装の作成

実装は以下のようになりました。すでに dry_run はテスト通過しています。copilot は delete_comment_request と書いてくれたのですが、Rails に合わせて destroy_comment_request に変更しています(ここだけ修正)。

    def destroy(dry_run: false)
      if dry_run
        Base.dry_run_script :delete, NotionCache.instance.comment_path(id)
      else
        json = NotionCache.instance.destroy_comment_request id
        @text_objects = RichTextArray.new "rich_text", json: json["rich_text"]
        self
      end
    end

NotionCache の destroy_comment_request は以下のようになります。これも

    # @param [String] id
    # @return [Hash]
    def destroy_comment_requesst(id)
      request :delete, comment_path(id)
    end

WebMock の stub を追加

いつものようにここまでで実装は完了です。あとは WebMock の stub を追加するだけとなります。

WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Please stub: DELETE https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b with headers {省略}                   

destroy_comment の stubs メソッドは以下のようになりました。

    def destroy_comment
      generate_stubs_sub :delete, __method__, :comment_path, {
        by_id: [EDIT_TEXT_COMMENT_ID, 200],
      }
    end

これによりテストは全て通過しました。

  destroy
    dry_run
      behaves like dry run
        is expected to eq "#!/bin/sh\ncurl -X DELETE 'https://api.notion.com/v1/comments/37921658084045ae924ad9ce1d60375b' \\\n  -H 'Notion-Version: 2026-03-11' \\\n  -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''"                                                                                            
    delete
      receive id

Finished in 0.0079 seconds (files took 0.2564 seconds to load)
8 examples, 0 failures

おわりに

これで 4/17 にリリースされた Multi-value filters と Comment に関する更新・削除の実装が終わりました。まだ View の実装は済んでいないのですが、先にこちらをリリースしてしまおうかと思っています。ここまで全部 development で作業してしまったので、view 実装の branch と今回のアップデートの更新を分けておこうと思います。

https://hkob.notion.site/hkob-16dd8e4e98ab807cbe3cf3cc94cdfe0f?pvs=4hkob.notion.site