以编程方式更改github pull请求的基本分支

时间:2018-05-04 21:11:50

标签: github pull-request

标题说明了一切......

任何人都有任何代码以编程方式更新github pull请求的基本分支?不要真的关心语言。

2 个答案:

答案 0 :(得分:3)

更改PR(拉取请求)的基本分支的API方法是described here

  

GitHub最近(2016年8月,不到2年前写这篇文章时)在change the base branch on a Pull Request创建后添加了该功能。
  现在我们正在更新Pull Request API以启用新功能。

     

例如:

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

Pull Request基数将更新为指向master分支。

您可以使用任何所需的脚本语言嵌入curl来电。

答案 1 :(得分:1)

我在Go: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)
            }

        }
    }
}