How do you close a pull request in python?

Question:

I created a pull request using

github_repo.create_pull(base='master', head=branch_name, title='title')

I can’t seem to find how to close one.

Asked By: njb

||

Answers:

Ref the PyGitHub code:

https://github.com/PyGithub/PyGithub/blob/master/github/PullRequest.py#L495

See GitHub API, resource "Pulls", Update a pull request for query parameter state

state string

Either open, closed, or all to filter by state.

Default: open

I’d say you want to update the PR with query-parameter asstate=closed:

pr = ??? Code to get the PR you want ???
pr.edit(state="closed")
Answered By: Tom Dalton

To close a PR with package PyGithub use Repository.get_pull() to return an instance of PullRequest, then PullRequest.edit(state='closed').

# either create a PR within given repo and return the instance
pr = github_repo.create_pull(base='master', head=branch_name, title='title')
print(pr.id)

# or get the PR instance by PR id
id = pr.id
pr = github_repo.get_pull(id)

# to close it update the PR with state=closed 
pr.edit(state='closed')
print('PR was closed at: ', pr.closed_at)
Answered By: hc_dev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.