Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use upstream as the base to create branches for backports #359

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/backport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
cmd.Flags().StringVarP(&backportCmdOpts.User, "user", "u", "", "user to assign new issues to (default: user assigned to the original issue)")
cmd.Flags().StringVarP(&backportCmdOpts.Owner, "owner", "o", "", "owner of the repository, e.g: k3s-io, rancher")
cmd.Flags().BoolVarP(&backportCmdOpts.DryRun, "dry-run", "n", false, "skip creating issues and pushing changes to remote")
cmd.Flags().BoolVarP(&backportCmdOpts.DryRun, "skip-create-issue", "s", false, "skip creating issues")
cmd.Flags().BoolVarP(&backportCmdOpts.SkipCreateIssue, "skip-create-issue", "s", false, "skip creating issues")

if err := cmd.MarkFlagRequired("repo"); err != nil {
logrus.Fatal(err)
Expand Down
33 changes: 23 additions & 10 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/v39/github"
"github.com/rancher/ecm-distro-tools/exec"
Expand Down Expand Up @@ -272,20 +273,32 @@ func PerformBackport(ctx context.Context, client *github.Client, pbo *PerformBac
if err != nil {
return nil, err
}
upstreamRemoteURL := "https://github.com/" + pbo.Owner + "/" + pbo.Repo + ".git"
fmt.Println("creating remote: 'upstream " + upstreamRemoteURL + "'")
if _, err := r.CreateRemote(&config.RemoteConfig{
Name: "upstream",
URLs: []string{upstreamRemoteURL},
}); err != nil {
if err != git.ErrRemoteExists {
return nil, err
}
}
fmt.Println("fetching remote: upstream")
if err := r.Fetch(&git.FetchOptions{
RemoteName: "upstream",
Progress: os.Stdout,
Tags: git.AllTags,
}); err != nil {
if err != git.NoErrAlreadyUpToDate {
return nil, err
}
}
}

for _, branch := range pbo.Branches {
if cherryPick {
logrus.Info("fetching branch from origin: " + branch + ":" + branch)
fetchOut, err := exec.RunCommand(cwd, "git", "fetch", "origin", branch+":"+branch)
if err != nil {
return nil, err
}
logrus.Info(fetchOut)
coo := git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/heads/" + branch),
}
logrus.Info("checking out on reference refs/heads/" + branch)
coo := git.CheckoutOptions{Branch: plumbing.ReferenceName("refs/remotes/upstream/" + branch)}
logrus.Info("checking out on reference refs/remotes/upstream/" + branch)
logrus.Infof("checkout options: %+v", coo)
if err := w.Checkout(&coo); err != nil {
return nil, errors.New("failed checkout: " + err.Error())
Expand Down
Loading