Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 391
user2490003 Avatar asked Aug 12 '19 15:08

user2490003


1 Answers

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
like image 193
Matt L. Avatar answered Oct 14 '22 03:10

Matt L.