Add backport open list, change args to flags for back/frontport
ci/woodpecker/push/goreleaser Pipeline was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 2022-08-30 13:58:49 -05:00
parent 4cef44e312
commit 86ce1bed45
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 84 additions and 32 deletions

View File

@ -2,7 +2,6 @@ package cmd
import (
"context"
"errors"
"flag"
"fmt"
"strings"
@ -13,18 +12,30 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/rs/zerolog/log"
"github.com/skratchdot/open-golang/open"
)
var (
backportFS = flag.NewFlagSet("backport", flag.ContinueOnError)
Backport = &ffcli.Command{
backportFS = flag.NewFlagSet("backport", flag.ContinueOnError)
backportFromFlag = backportFS.String("from", "", "Release to backport from (ex: `main`, default: main)")
backportToFlag = backportFS.String("to", "", "Release to backport to (ex: `1.17`, default: <latest>)")
backportListFlag = backportFS.Bool("list", false, "Open repository to see needed backports")
Backport = &ffcli.Command{
Name: "backport",
FlagSet: backportFS,
ShortUsage: "backport <release>",
ShortUsage: "backport --from [release=main] --to [release=latest]",
ShortHelp: "backport cherry-picks a <commit> and applies it to a clean branch based on <release>",
Exec: func(ctx context.Context, args []string) error {
if len(args) < 1 {
return errors.New("backport requires one argument")
Exec: func(ctx context.Context, _ []string) error {
to := *backportToFlag
if to == "" {
to = latestRelease()
}
if *backportListFlag {
if err := open.Run(fmt.Sprintf("https://github.com/go-gitea/gitea/pulls?q=is%%3Apr+-label%%3Abackport%%2Fdone+label%%3Abackport%%2Fv1.%s+is%%3Amerged", to)); err != nil {
return err
}
return nil
}
fetch(ctx)
@ -33,8 +44,13 @@ var (
log.Fatal().Msg("working tree is dirty")
}
from := *backportFromFlag
if from == "" {
from = "main"
}
commits, err := repo().Log(&git.LogOptions{
From: head("main"),
From: head(from),
})
if err != nil {
return err
@ -61,8 +77,8 @@ var (
hash := optMap[resp]
branch := fmt.Sprintf("backport-%s", hash)
base := fmt.Sprintf("upstream/release/v%s", args[0])
if err := Branch.ParseAndRun(ctx, []string{branch, base}); err != nil {
base := fmt.Sprintf("upstream/release/v1.%s", to)
if err := Branch.ParseAndRun(ctx, []string{"--base", base, branch}); err != nil {
return err
}

View File

@ -10,28 +10,30 @@ import (
)
var (
branchFS = flag.NewFlagSet("branch", flag.ContinueOnError)
branchNoFetch = branchFS.Bool("no-fetch", false, "Skip fetching")
Branch = &ffcli.Command{
branchFS = flag.NewFlagSet("branch", flag.ContinueOnError)
branchNoFetchFlag = branchFS.Bool("no-fetch", false, "Skip fetching")
branchBaseFlag = branchFS.String("base", "main", "Ref to base from")
Branch = &ffcli.Command{
Name: "branch",
FlagSet: branchFS,
ShortUsage: "branch <name> [base=main]",
ShortHelp: "branch creates a new branch called <name> based on [base]",
ShortUsage: "branch --base [ref=main] <name>",
ShortHelp: "branch creates a new branch called <name> based on <base>",
Exec: func(ctx context.Context, args []string) error {
if len(args) < 1 {
return errors.New("branch requires at least one argument")
return errors.New("branch requires one argument")
}
name, base := args[0], "upstream/main"
if len(args) > 1 {
base = args[1]
name := args[0]
base := *branchBaseFlag
if base == "" {
base = "upstream/main"
}
if !isClean() {
log.Fatal().Msg("working tree is dirty")
}
if !*branchNoFetch {
if !*branchNoFetchFlag {
fetch(ctx)
}

View File

@ -4,6 +4,8 @@ import (
"context"
"os"
"os/exec"
"regexp"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
@ -58,3 +60,27 @@ func head(name string) plumbing.Hash {
}
return ref.Hash()
}
var releaseRe = regexp.MustCompile(`release/v1\.(\d+)`)
func latestRelease() string {
cmd := exec.Command("git", "ls-remote", "upstream", "release/*")
out, err := cmd.Output()
if err != nil {
log.Fatal().Err(err).Msg("could not get latest release")
}
matches := releaseRe.FindAllStringSubmatch(string(out), -1)
var latest string
for _, match := range matches {
m := match[1]
if len(m) < 3 {
m = strings.Repeat("0", 3-len(m)) + m
}
if m > latest {
latest = m
}
}
return strings.TrimLeft(latest, "0")
}

View File

@ -2,7 +2,6 @@ package cmd
import (
"context"
"errors"
"flag"
"fmt"
"strings"
@ -16,19 +15,21 @@ import (
)
var (
frontportFS = flag.NewFlagSet("frontport", flag.ContinueOnError)
Frontport = &ffcli.Command{
frontportFS = flag.NewFlagSet("frontport", flag.ContinueOnError)
frontportFromFlag = frontportFS.String("from", "", "Release to frontport from (ex: `1.17`, default: <latest>)")
frontportToFlag = frontportFS.String("to", "", "Release to frontport to (ex: `main`, default: `main`)")
Frontport = &ffcli.Command{
Name: "frontport",
FlagSet: frontportFS,
ShortUsage: "frontport <from> [release=main]",
ShortUsage: "frontport --from [release=latest] --to [release=main]",
ShortHelp: "frontport cherry-picks a <commit> and applies it to a clean branch based on <release>",
Exec: func(ctx context.Context, args []string) error {
if len(args) < 1 {
return errors.New("frontport requires at least one argument")
Exec: func(ctx context.Context, _ []string) error {
from := *frontportFromFlag
if from == "" {
from = latestRelease()
}
from := args[0]
if !strings.HasPrefix(from, "release") {
from = fmt.Sprintf("release/v%s", args[0])
from = fmt.Sprintf("release/v%s", from)
}
fetch(ctx)
@ -65,9 +66,13 @@ var (
hash := optMap[resp]
branch := fmt.Sprintf("frontport-%s", hash)
base := "upstream/main"
if len(args) > 1 {
base = fmt.Sprintf("upstream/release/v%s", args[1])
base := *frontportToFlag
if base == "" {
base = "upstream/main"
}
if !strings.HasPrefix(base, "upstream") {
base = fmt.Sprintf("upstream/release/v1.%s", base)
}
if err := Branch.ParseAndRun(ctx, []string{branch, base}); err != nil {
return err

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/go-git/go-git/v5 v5.4.2
github.com/peterbourgon/ff/v3 v3.3.0
github.com/rs/zerolog v1.27.0
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
)
require (

2
go.sum
View File

@ -82,6 +82,8 @@ github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Q
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=