keflavich
keflavich

Reputation: 19235

Github API - add an issue to a project?

Is it possible, using the github API, to add an Issue to a project board?

This document: https://docs.github.com/en/rest/reference/projects#create-a-project-card suggests it is possible, but my attempts using https://github.com/fastai/ghapi have failed.

I have tried:

api.projects.create_card(col.id, note=f'{issue.title} {issue.html_url}', content_id=issue.id)

but the card does not show up as an issue card, it shows up as a card referencing an issue - which is different.

I also tried:

curl -X POST -H "Accept: application/vnd.github.v3+json" https://api.github.com/projects/columns/####/cards -d '{"column_id": colid, "content_id": cid, "content_type": "json"}' -u user:$GITHUB_TOKEN

and that gave an error:

{
  "message": "Validation Failed",
  "errors": [
    {
      "resource": "ProjectCard",
      "code": "unprocessable",
      "field": "data",
      "message": "Could not resolve to a node with the global id of 'XXXXXXXXXX'."
    }
  ],
  "documentation_url": "https://docs.github.com/v3/projects/cards/#create-a-project-card"
}

which suggests that I've mucked up the curl request, but I'm not sure where.

Upvotes: 1

Views: 1478

Answers (1)

keflavich
keflavich

Reputation: 19235

I've found a solution following https://stackoverflow.com/a/57696731/814354. I need to declare the content_type in the POST request (which is information I was unable to find on the github documentation).

CURL version:

curl -X POST -H "Accept: application/vnd.github.v3+json" https://api.github.com/projects/columns/{column_id}/cards -d '{"column_id": {column_id}, "content_id": {issue_id}, "content_type": "Issue"}' -u {username}:$GITHUB_TOKEN

The ghapi version does not work:

# this fails - `content_id` and  `content_type` are not included in the request
api.projects.create_card(col.id, content_id=issue.id, content_type='Issue')

but you can still use the ghapi in a clunkier way:

api(path=f'/projects/columns/{col.id}/cards', verb='POST',
    data={'content_id': issue.id,
          'column_id': col.id,
          'content_type': 'Issue'
         },
         headers={"Accept": "application/vnd.github.v3+json"}
      )

Upvotes: 1

Related Questions