user2490003
user2490003

Reputation: 11890

Adding Pull Requests & Issues to a Project

Does the Github API provide an easy way to add a Pull Request or an Issue to a Project Board?

This is the programmatic equivalent of a user going to a pull request and selecting one more "Projects" from the sidebar menu

NOTE: The API does seem to provide a way to add cards to the Project, but I have to specify a specific project column. I'd love to just add the project blindly and let automation rules determine the column, similar to clicking on it through the UI.

Thanks!

Upvotes: 6

Views: 663

Answers (1)

Matt L.
Matt L.

Reputation: 3621

I think the best way to associate an existing pull request with a project, in some kind of default column, would be to daisy-chain three separate pieces of the Github API, the Get a Single Pull Request method, the Create a Project Card method, and the List Project Columns method. The idea is as follows:

  1. Use 'Get a Single Pull Request' to retrieve the ID
  2. Use 'List Project Columns' to get a list of the columns
  3. Do any conditional logic if you want to see if a certain column exists, or just use the first one, or create a certain column if it doesn't exist
  4. Use "Create a Project Card" to add the card using the Pull request ID and Column you've selected.

Here is a simplified example in Python:

import requests, json
#get pull request 
r = requests.get('https://api.github.com/repos/[myusername]/[myrepo]/pulls/2')
pull = json.loads(r.text)
#requires authentication ... create your token through Github.com
api_token = "mytoken"

#prepare dictionary of header data
h = {"Accept":"application/vnd.github.inertia-preview+json", "Authorization": "token %s" % api_token}

projects_r = requests.get('https://api.github.com/repos/[myusername]/[myrepo]/projects', headers=h)
#get projects data
projects = json.loads(projects_r.text)

#get columns url for the first project in the list projects
columns_url = projects[0]['columns_url']
columns_r = requests.get(columns_url, headers=h)
columns = json.loads(columns_r.text)

#get column url for the first column in the list of columns
column_url = columns[0]['cards_url']

#use retrieved data to build post 
data = {"content_id":pull_id, "content_type":"PullRequest"}

#use post method with headers and data to create card in column
result = requests.post(column_url, headers=h, data=json.dumps(data))
#returns with status 201, created

Upvotes: 2

Related Questions