You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

调用GitHub API关闭Pull Request时遭遇404错误求助

Troubleshooting 404 Error When Closing a GitHub PR via API

Let’s break down the most likely causes for that 404 error and fix your close function step by step:

1. Incomplete or Malformed API URL

Your current close function has a truncated URL (url = "https://api.github.com/repos/{}/{}/pulls/{}"....). First, ensure you’re formatting the URL correctly, just like you did in the merge function. The endpoint for updating a PR (to close it) uses the same structure as fetching PR details—you just need to finish populating it with the owner, repo, and PR number.

2. Wrong HTTP Request Method

Unlike merging a PR (which uses PUT), closing a PR requires a PATCH request to update the PR’s state. Using PUT here is a common mistake that can lead to 404 or invalid request errors.

3. Missing Required Request Body

To close the PR, you need to explicitly set the state to "closed" in the request body. An empty data={} won’t trigger the close action.

Corrected close Function

Here’s how to fix your function to address all these issues:

def close(pull):
    # Fully format the API URL with repository details and PR number
    url = "https://api.github.com/repos/{}/{}/pulls/{}".format(
        os.environ.get("GITHUB_USERNAME"), 
        os.environ.get("GITHUB_REPO"), 
        pull['number']
    )
    # Send a PATCH request to update the PR state to closed
    response = requests.patch(url, auth=get_auth(), json={"state": "closed"})
    
    if response.status_code == 200:
        # PR closed successfully
        return True
    else:
        # Return error code for debugging
        return response.status_code

Additional Checks to Rule Out Edge Cases

  • Verify Authentication: Confirm your get_auth() function returns valid credentials (username/token) with the repo scope (required to modify PRs). While merge works, double-check the token permissions—though 404 is less common for auth issues, it’s worth eliminating.
  • Validate PR Number & Repository: Ensure pull['number'] refers to an existing PR in the repository specified by GITHUB_USERNAME and GITHUB_REPO. Since your merge function works, this is probably fine, but it’s a quick sanity check.
  • Test the URL Directly: Print the full URL your code generates and test it in a tool like curl or Postman with your auth credentials. This will help you confirm if the endpoint is reachable and isolate whether the issue is in your code or API setup.

内容的提问来源于stack exchange,提问作者Forrest Keppler

火山引擎 最新活动