Skip to content

Commit

Permalink
create issue after pushing branch
Browse files Browse the repository at this point in the history
  • Loading branch information
tashima42 committed Jan 9, 2024
1 parent 153f6b5 commit 3a6c06f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 94 deletions.
9 changes: 0 additions & 9 deletions cmd/backport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ func main() {
if err := cmd.MarkFlagRequired("issue"); err != nil {
logrus.Fatal(err)
}
if err := cmd.MarkFlagRequired("commits"); err != nil {
logrus.Fatal(err)
}
if err := cmd.MarkFlagRequired("branches"); err != nil {
logrus.Fatal(err)
}
Expand All @@ -58,12 +55,6 @@ func main() {
}

func backport(cmd *cobra.Command, args []string) error {
if len(backportCmdOpts.Branches) < 1 || backportCmdOpts.Branches[0] == "" {
return errors.New("no branches specified")
}
if len(backportCmdOpts.Commits) < 1 || backportCmdOpts.Commits[0] == "" {
return errors.New("no commits specified")
}
if backportCmdOpts.Owner == "" {
backportCmdOpts.Owner = defaultRepositoryOwner(backportCmdOpts.Repo)
}
Expand Down
170 changes: 85 additions & 85 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,118 +227,118 @@ type PerformBackportOpts struct {
// PerformBackport creates backport issues, performs a cherry-pick of the
// given commit if it exists.
func PerformBackport(ctx context.Context, client *github.Client, pbo *PerformBackportOpts) ([]*github.Issue, error) {
const (
issueTitle = "[%s] - %s"
issueBody = "Backport fix for %s\n\n* #%d"
)
var issues []*github.Issue
var cwd string
var r *git.Repository
var w *git.Worktree

cherryPick := false
if len(pbo.Commits) != 0 {
cherryPick = true
}

origIssue, err := RetrieveOriginalIssue(ctx, client, pbo.Owner, pbo.Repo, pbo.IssueID)
if err != nil {
return nil, err
}

issue := Issue{
Title: issueTitle,
Body: issueBody,
Title: "[%s] - %s",
Body: "Backport fix for %s\n\n* #%d",
}

issues := make([]*github.Issue, len(pbo.Branches))
for _, branch := range pbo.Branches {
logrus.Infof("creating issue:\n Owner: %s\n Repo: %s\n Branch: %s\n User: %s", pbo.Owner, pbo.Repo, branch, pbo.User)
if pbo.DryRun {
logrus.Info("dry run, skipping issue creation")
continue
}
newIssue, err := CreateBackportIssues(ctx, client, origIssue, pbo.Owner, pbo.Repo, branch, pbo.User, &issue)
if cherryPick {
// we're assuming this code is called from the repository itself
logrus.Info("getting working directory")
cwd, err = os.Getwd()
if err != nil {
return nil, err
}
issues = append(issues, newIssue)
}

// stop here if there are no commits given
if len(pbo.Commits) == 0 {
logrus.Info("no commits, interrupting run")
return issues, nil
}

// we're assuming this code is called from the repository itself
logrus.Info("getting working directory")
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
logrus.Info("working directory: " + cwd)

logrus.Info("opening git repository at working directory")
r, err := git.PlainOpen(cwd)
if err != nil {
return nil, err
}

logrus.Info("getting repository worktree")
w, err := r.Worktree()
if err != nil {
return nil, err
}
logrus.Info("working directory: " + cwd)

for _, branch := range pbo.Branches {
logrus.Info("fetching all branches from all remotes")
fetchOut, err := exec.RunCommand(cwd, "git", "fetch", "origin", branch+":"+branch)
logrus.Info("opening git repository at working directory")
r, err = git.PlainOpen(cwd)
if err != nil {
return nil, err
}
logrus.Info(fetchOut)
coo := git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/heads/" + branch),
return nil, fmt.Errorf("not in a git repository, make sure you are executing this inside the %s repo: %s", pbo.Repo, err)
}
logrus.Info("checking out on reference refs/heads/" + branch)
logrus.Infof("checkout options: %+v", coo)
if err := w.Checkout(&coo); err != nil {
return nil, errors.New("failed checkout: " + err.Error())
}

newBranchName := fmt.Sprintf("issue-%d_%s", pbo.IssueID, branch)
logrus.Info("new branch name: " + newBranchName)

logrus.Info("getting head reference")
headRef, err := r.Head()
logrus.Info("getting repository worktree")
w, err = r.Worktree()
if err != nil {
return nil, err
}
}

ref := plumbing.NewHashReference(plumbing.NewBranchReferenceName(newBranchName), headRef.Hash())
if err := r.Storer.SetReference(ref); err != nil {
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)
logrus.Infof("checkout options: %+v", coo)
if err := w.Checkout(&coo); err != nil {
return nil, errors.New("failed checkout: " + err.Error())
}

coo = git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/heads/" + newBranchName),
}
logrus.Info("checkout out on reference refs/heads/" + newBranchName)
if err := w.Checkout(&coo); err != nil {
return nil, errors.New("failed checkout: " + err.Error())
}
newBranchName := fmt.Sprintf("issue-%d_%s", pbo.IssueID, branch)
logrus.Info("new branch name: " + newBranchName)

for _, commit := range pbo.Commits {
logrus.Info("cherry picking commit: " + commit)
cherryPickOut, err := exec.RunCommand(cwd, "git", "cherry-pick", commit)
logrus.Info("getting head reference")
headRef, err := r.Head()
if err != nil {
return nil, err
}
logrus.Info(cherryPickOut)

if pbo.DryRun {
logrus.Info("dry run, skipping push to origin for branch " + newBranchName)
continue
}
logrus.Info("pushing " + newBranchName + "to origin")
pushOut, err := exec.RunCommand(cwd, "git", "push", "origin", newBranchName)
if err != nil {
logrus.Info("getting new hash reference")
ref := plumbing.NewHashReference(plumbing.NewBranchReferenceName(newBranchName), headRef.Hash())
logrus.Info("setting new hash reference")
if err := r.Storer.SetReference(ref); err != nil {
return nil, err
}
logrus.Info(pushOut)

coo = git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/heads/" + newBranchName),
}
logrus.Info("checkout out on reference refs/heads/" + newBranchName)
if err := w.Checkout(&coo); err != nil {
return nil, errors.New("failed checkout: " + err.Error())
}

for _, commit := range pbo.Commits {
logrus.Info("cherry picking commit: " + commit)
cherryPickOut, err := exec.RunCommand(cwd, "git", "cherry-pick", commit)
if err != nil {
return nil, err
}
logrus.Info(cherryPickOut)

if pbo.DryRun {
logrus.Info("dry run, skipping push to origin for branch " + newBranchName)
continue
}
logrus.Info("pushing " + newBranchName + " to origin")
pushOut, err := exec.RunCommand(cwd, "git", "push", "origin", newBranchName)
if err != nil {
return nil, err
}
logrus.Info(pushOut)
}
}

logrus.Infof("creating issue | owner: %s | Repo: %s | Branch: %s", pbo.Owner, pbo.Repo, branch)
if pbo.DryRun {
logrus.Info("dry run, skipping issue creation")
continue
}
newIssue, err := CreateBackportIssues(ctx, client, origIssue, pbo.Owner, pbo.Repo, branch, pbo.User, &issue)
if err != nil {
return nil, err
}
issues = append(issues, newIssue)
}

return issues, nil
Expand Down

0 comments on commit 3a6c06f

Please sign in to comment.