Search code examples
pythongithubgithub-api

How to approve Github pull request using access token?


As per API documentation https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request

We can use a CURL to approve pull request i.e.

curl -s -H "Authorization: token ghp_TOKEN" \
 -X POST -d '{"event": "APPROVE"}' \
 "https://api.github.com/repos/{owner}/{repo}/pulls/{pull_number}/reviews"

but I get this error:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest"
}

Although, the CURL is working fine for other APIs like:

curl -s -H "Authorization: token ghp_TOKEN" \
 -X POST -d '{"body": "some message"}' \
 "https://api.github.com/repos/{owner}/{repo}/issues/{pull_number}/reviews"

I have tried everything to make this work. Can somebody please help me with this?


Solution

  • After bit of experiments, it has worked with API /repos/{owner}/{repo}/pulls/{pull_number}/reviews

    I must say that Github documentation is very poor that I have to spend almost 3 hours to figure this out. A small but proper CURL would have helped in a few seconds and would have saved my time.

    Anyway, leaving this solution on StackOverflow so that, this helps other people and saves their precious time.

    CURL:

    curl -s -H "Authorization: token ghp_BWuQzbiDANEvrQP9vZbqa5LHBAxxIzwi2gM7" \
     -X POST -d '{"event":"APPROVE"}' \
     "https://api.github.com/repos/tech-security/chatbot/pulls/4/reviews"
    

    Python Code:

    import requests
    
    headers = {
        'Authorization': 'token ghp_BWuQzbiDANEvrQP9vZbqa5LHBAxxIzwi2gM7',
    }
    
    data = '{"event":"APPROVE"}'
    
    response = requests.post('https://api.github.com/repos/tech-security/chatbot/pulls/4/reviews', headers=headers, data=data)
    
    print (response.json())
    

    Note: above github token is dummy, so don't freak out please! :D