Programmatically change base branch for a github pull request
Asked Answered
C

3

5

Title says it all…

Anyone have any code to update the base branch for a github pull request programmatically? Don't really care about the language.

Chao answered 4/5, 2018 at 21:11 Comment(0)
H
7

The API method to change the base branch of a PR (Pull Request) is described here:

GitHub recently (August 2016, less than 2 years ago at the time of writing this) added the ability to change the base branch on a Pull Request after it's created.
Now we're updating the Pull Request API to enable the new functionality.

For example:

curl "https://api.github.com/repos/github/hubot/pulls/123" \
  -H 'Authorization: token TOKEN' \
  -d '{ "base": "master" }'

The Pull Request base will be updated to point to the master branch.

You can embed that curl call in any script language you want.

Haematin answered 5/5, 2018 at 1:44 Comment(1)
@RobinGreen Agreed. I have rewritten the answer to include only the GitHub API part needed to change the base branch of a Pull Request.Haematin
C
2

I wrote a util to do this in Go at: https://github.com/clintmod/retarget-github-prs

package main

import (
    "context"
    "fmt"
    "github.com/google/go-github/github"
    "os"
    "strings"
)

func envVarError(name string) {
    fmt.Errorf("No %v environment variable found", name)
    os.Exit(1)
}

func missingArg(arg string, index int) {
    fmt.Printf("Missing arg %v at position %d\n", arg, index)
    os.Exit(1)
}

func validateArgs(args []string) {
    if len(args) < 2 {
        missingArg("Github Account", 1)
    }
    if len(args) < 3 {
        missingArg("Old Branch", 2)
    }
    if len(args) < 4 {
        missingArg("New Branch", 3)
    }
    if len(args) < 5 {
        missingArg("Repos (e.g. oceans,triton,rhode", 3)
    }
}

func main() {
    uname := os.Getenv("GITHUB_USERNAME")
    pass := os.Getenv("GITHUB_PASSWORD")

    if uname == "" {
        envVarError("GITHUB_USERNAME")
    }
    if pass == "" {
        envVarError("GITHUB_PASSWORD")
    }

    validateArgs(os.Args)

    owner := os.Args[1]
    oldBranch := os.Args[2]
    newBranch := os.Args[3]
    repos := strings.Split(os.Args[4], ",")

    tp := github.BasicAuthTransport{Username: uname, Password: pass}

    client := github.NewClient(tp.Client())

    for _, repo := range repos {
        opt := &github.PullRequestListOptions{"open", "", oldBranch, "created", "desc", github.ListOptions{Page: 1}}
        pulls, _, err := client.PullRequests.List(context.Background(), owner, repo, opt)

        if err != nil {
            fmt.Printf("Error: %v\n", err)
            return
        }

        numberOfPulls := len(pulls)
        fmt.Println("number of pulls = ", numberOfPulls)
        for i := 0; i < numberOfPulls; i++ {
            pull := pulls[i]
            pullNumber := *pull.Number
            *pull.Base.Ref = newBranch
            fmt.Printf("Retargeting pull request %v the %v branch\n", pullNumber, *pull.Base.Ref)
            _, _, err := client.PullRequests.Edit(context.Background(), owner, repo, pullNumber, pull)
            if err != nil {
                fmt.Errorf("%d: PullRequests.Edit returned error: %v", i, err)
            } else {
                fmt.Printf("pull request %v retargeted\n", pullNumber)
            }

        }
    }
}
Chao answered 7/5, 2018 at 17:18 Comment(1)
Thank you so much for this! I had to migrate 100s of PRs when converting all repos from master to main and this cut down my migration time dramatically!Disappointment
L
2

With the GitHub CLI, changing the base branch for a pull request is as simple as executing

$ gh pr edit [<number> | <url> | <branch>] --base <branch>
Lh answered 24/7, 2024 at 5:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.