Skip to content

[BUG]: github_rest_api supports list response #1776

@riezebosch

Description

@riezebosch

Describe the need

data "github_rest_api" "comments" {
  endpoint = "repos/${resource.github_repository.this.full_name}/issues/${resource.github_issue.this.number}/comments"
}

this is the actual response:

2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0: [
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  {
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "url": "https://api.github.com/repos/riezebosch/caption-this/issues/comments/1622043114",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "html_url": "https://github.com/riezebosch/caption-this/issues/1#issuecomment-1622043114",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "issue_url": "https://api.github.com/repos/riezebosch/caption-this/issues/1",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "id": 1622043114,
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "node_id": "IC_kwDOJ39n3c5grmnq",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "user": {
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "login": "riezebosch",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "id": 1427334,
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "node_id": "MDQ6VXNlcjE0MjczMzQ=",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "avatar_url": "https://avatars.githubusercontent.com/u/1427334?v=4",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "gravatar_id": "",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "url": "https://api.github.com/users/riezebosch",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "html_url": "https://github.com/riezebosch",
2023-07-05T19:58:56.713+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "followers_url": "https://api.github.com/users/riezebosch/followers",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "following_url": "https://api.github.com/users/riezebosch/following{/other_user}",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "gists_url": "https://api.github.com/users/riezebosch/gists{/gist_id}",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "starred_url": "https://api.github.com/users/riezebosch/starred{/owner}{/repo}",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "subscriptions_url": "https://api.github.com/users/riezebosch/subscriptions",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "organizations_url": "https://api.github.com/users/riezebosch/orgs",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "repos_url": "https://api.github.com/users/riezebosch/repos",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "events_url": "https://api.github.com/users/riezebosch/events{/privacy}",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "received_events_url": "https://api.github.com/users/riezebosch/received_events",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "type": "User",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "site_admin": false
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   },
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "created_at": "2023-07-05T15:53:57Z",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "updated_at": "2023-07-05T17:57:36Z",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "author_association": "OWNER",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "body": "hello",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "reactions": {
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "url": "https://api.github.com/repos/riezebosch/caption-this/issues/comments/1622043114/reactions",
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "total_count": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "+1": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "-1": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "laugh": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "hooray": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "confused": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "heart": 0,
2023-07-05T19:58:56.714+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "rocket": 0,
2023-07-05T19:58:56.715+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:    "eyes": 0
2023-07-05T19:58:56.715+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   },
2023-07-05T19:58:56.715+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "performed_via_github_app": null
2023-07-05T19:58:56.715+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  }
2023-07-05T19:58:56.715+0200 [DEBUG] provider.terraform-provider-github_v5.29.0: ]
2023-07-05T19:58:56.715+0200 [DEBUG] provider.terraform-provider-github_v5.29.0: -----------------------------------------------------

But probably because the resource uses tomap it fails to parse the body, because this is wat I find in my state file:

  "resources": [
    {
      "module": "module.caption-this",
      "mode": "data",
      "type": "github_rest_api",
      "name": "comments",
      "provider": "provider[\"registry.terraform.io/hashicorp/github\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "body": {},
            "code": 200,
            "endpoint": "repos/riezebosch/caption-this/issues/1/comments",
            "headers": null,
            "id": "FB5D:10872:F94563:FB3D47:64A5AF60",
            "status": "200 OK"
          },
          "sensitive_attributes": []
        }
      ]
    },

It works when I, like the examples in the docs, use an endpoint that returns a single object instead:

data "github_rest_api" "example" {
  endpoint = "repos/${resource.github_repository.this.full_name}/git/refs/heads/main"
}
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0: {
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  "ref": "refs/heads/main",
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  "node_id": "REF_kwDOJ39n3a9yZWZzL2hlYWRzL21haW4",
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  "url": "https://api.github.com/repos/riezebosch/caption-this/git/refs/heads/main",
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  "object": {
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "sha": "fea67a926a7fffdf8fee06b46b978dd1744146d5",
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "type": "commit",
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:   "url": "https://api.github.com/repos/riezebosch/caption-this/git/commits/fea67a926a7fffdf8fee06b46b978dd1744146d5"
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0:  }
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0: }
2023-07-05T19:58:56.330+0200 [DEBUG] provider.terraform-provider-github_v5.29.0: -----------------------------------------------------

and in my state file:

{
      "module": "module.caption-this",
      "mode": "data",
      "type": "github_rest_api",
      "name": "example",
      "provider": "provider[\"registry.terraform.io/hashicorp/github\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "body": {
              "node_id": "REF_kwDOJ39n3a9yZWZzL2hlYWRzL21haW4",
              "url": "https://api.github.com/repos/riezebosch/caption-this/git/refs/heads/main"
            },
            "code": 200,
            "endpoint": "repos/riezebosch/caption-this/git/refs/heads/main",
            "headers": null,
            "id": "FB5D:10872:F9445E:FB3C51:64A5AF60",
            "status": "200 OK"
          },
          "sensitive_attributes": []
        }
      ]
    },

But even there it only picks up some of the root level properties and not from nested objects 🤷

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Up for grabsIssues that are ready to be worked on by anyoneType: BugSomething isn't working as documentedhacktoberfestIssues for participation in Hacktoberfest

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions