如何使用访问令牌批准 Github 拉取请求?

Alo*_*ath 5 python github github-api

根据 API 文档https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request

我们可以使用 CURL 来批准拉取请求,即

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

但我收到此错误:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest"
}
Run Code Online (Sandbox Code Playgroud)

尽管如此,CURL 对于其他 API 来说工作得很好,例如:

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

我已尽一切努力使这项工作成功。有人可以帮我解决这个问题吗?

Alo*_*ath 6

经过一番实验,它已经可以与 API 配合使用了/repos/{owner}/{repo}/pulls/{pull_number}/reviews

我必须说 Github 文档非常差,我花了将近 3 个小时才弄清楚这一点。一个小而适当的 CURL 会在几秒钟内有所帮助,并且会节省我的时间。

无论如何,将此解决方案留在 StackOverflow 上,这样可以帮助其他人并节省他们的宝贵时间。

卷曲:

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

Python代码:

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())
Run Code Online (Sandbox Code Playgroud)

注意:上面的 github 令牌是虚拟的,所以请不要惊慌!:D

  • 对于任何想知道的人,在 GitHub Enterprise Server 中,CURL 命令中使用的 URL 结构是:`https://[enterprise-hostname]/api/v3/repos/${{github.repository}}/pulls/${ {github.event.number}}/评论` (2认同)