[Python] Not able to identify the Github Pull request merge type

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    Here is the code that I am trying:

    merge_method_status = "Not Merged"
    if merged:
    # Fetch the latest PR info to get the 'merge_commit_sha' and other details
    pr_api_url = f"https://api.github.com/repos/{repo_name}/pulls/{pr_number}"
    headers = {"Authorization": f"token {GITHUB_TOKEN}"}
    async with httpx.AsyncClient() as client:
    pr_response = await client.get(pr_api_url, headers=headers)
    if pr_response.status_code == 200:
    pr_data = pr_response.json()
    print(pr_data)
    merge_commit_sha = pr_data.get("merge_commit_sha")
    head_sha = pr_data.get("head", {}).get("sha")
    base_sha = pr_data.get("base", {}).get("sha")

    # Determine merge method
    if pr_data.get("commits", 0) == 1:
    merge_method_status = "Rebase and Merged"
    elif head_sha == merge_commit_sha:
    merge_method_status = "Squash and Merged"
    else:
    merge_method_status = "Merge Commit"
    else:
    print(f"[ERROR] Failed to fetch PR details: {pr_response.text}")
    merge_method_status = "Merged" # fallback


    Now when I am trying to merge the PR I am willing to know what type of merging happened. There are 3 types

    1. Merge and commit
    2. Squash and merge
    3. Rebase and merge.

    I want to identify it using the github api. Can anyone suggest me what is the possible logic that I can try to find out what type of merging is happening?
    Please let me know what should I do to make the correct identification of the merging of the pull request.

    Continue reading...

Compartilhe esta Página