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

#326: Backport update to cobra and UX improvements #327

Merged
merged 16 commits into from
Jan 11, 2024
43 changes: 35 additions & 8 deletions cmd/backport/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,47 @@ The backport utility will create backport issues and perform a cherry-pick of th

If a commit is provided, `backport` assumes you're running from the repository the operation is related to. This is simply to avoid having to guess or figure out where you store your code on your local system.

### Flags

| **Flag** | **Description** | **Required** |
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ |
| `repo`, `r` | Name of the repository to perform the backport, e.g: `k3s`, `rke2` | TRUE |
| `issue`, `i` | ID of the original issue on GitHub | TRUE |
| `branches`, `b` | Branches the issue is being backported to, one or more (comma separated) | TRUE |
| `owner`, `o` | Owner of the repository e.g: `k3s-io`, `rancher` | TRUE |
| `commits`, `c` | Commits to be backported, if none is provided, only the issues will be created. When passing this flag, it assumes you're running from the repository this operation is related to (comma separated) | FALSE |
| `user`, `u` | User to assign new issues to (default: user assignted to the original issue) | FALSE |
| `dry-run`, `n` | Skip creating issues and pushing changes to remote | FALSE |
| `github-token`, `g`, `GITHUB_TOKEN` | Github Token | TRUE |

### Examples

* Backport K3s change into release-1.21 and release-1.22. Only create the backport issues.
```sh
# Backport K3s change into release-1.21 and release-1.22. Only create the backport issues.
./backport -o k3s-io -r k3s -b 'release-1.21,release-1.22' -i 123
cd k3s
export GITHUB_TOKEN={YOUR_GITHUB_TOKEN}
backport -r k3s -o k3s-io -b 'release-1.21,release-1.22' -i 123
```

# Backport K3s change into release-1.21 and release-1.22. Creates the backport issues and cherry-picks the given commit id.
./backport -o k3s-io -r k3s -b 'release-1.21,release-1.22' -i 123 -c '181210f8f9c779c26da1d9b2075bde0127302ee0'
* Backport K3s change into release-1.21 and release-1.22. Creates the backport issues and cherry-picks the given commit id.
```sh
cd k3s
export GITHUB_TOKEN={YOUR_GITHUB_TOKEN}
backport -r k3s -o k3s-io -b 'release-1.21,release-1.22' -i 123 -c '181210f8f9c779c26da1d9b2075bde0127302ee0'
```

# Backport RKE2 change into release-1.20, release-1.21 and release-1.22
./backport -o rancher -r rke2 -b 'release-1.20,release-1.21,release-1.22' -i 456 -c 'cd700d9a444df8f03b8ce88cb90261ed1bc49f27'
* Backport RKE2 change into release-1.20, release-1.21 and release-1.22
```sh
cd rke2
export GITHUB_TOKEN={YOUR_GITHUB_TOKEN}
backport -r rke2 -o rancher -b 'release-1.20,release-1.21,release-1.22' -i 456 -c 'cd700d9a444df8f03b8ce88cb90261ed1bc49f27'
```

# Backport K3s change into release-1.21 and release-1.22 and assign to given user.
./backport -o k3s-io -r k3s -b 'release-1.21,release-1.22' -i 123 -u susejsmith
* Backport K3s change into release-1.21 and release-1.22 and assign to given user.
```sh
cd k3s
export GITHUB_TOKEN={YOUR_GITHUB_TOKEN}
backport -r k3s -o k3s-io -b 'release-1.21,release-1.22' -i 123 -u susejsmith
```

## Contributions
Expand Down
144 changes: 56 additions & 88 deletions cmd/backport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,85 @@ package main

import (
"context"
"flag"
"fmt"
"errors"
"os"
"strings"

"github.com/rancher/ecm-distro-tools/repository"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
name string
version string
gitSHA string
)

const usage = `version: %s
Usage: %[2]s [-r repo] [-b branches] [-i issue]

Env Variables:
GITHUB_TOKEN user token for posting issues
Options:
-h help
-v show version and exit
-r repo repository that should be used
-i issue id original issue id
-c commits commits to be backported (comma seperated)
-b branch(es) branches issue is being backported to
-u user user to assign new issues to (default: user assigned to orig. issue)

Examples:
# generate 2 backport issues for k3s issue 1234
%[2]s -r k3s -b "release-1.25,release-1.26" -i 1234 -c 1
%[2]s -r k3s -b "release-1.26" -i 1234 -c 1,2,3
%[2]s -r k3s -b "release-1.26" -i 1234 -c 1,2,3 -u susejsmith

Note: if a commit is provided, %[2]s utility needs to be ran from either
the RKE2 or k3s directory.
`
type BackportCmdOpts struct {
Repo string
Issue uint
Commits []string
Branches []string
User string
Owner string
DryRun bool
}

var (
vers bool
owner string
repo string
commitIDs string
issueID uint
branches string
user string
)
var backportCmdOpts BackportCmdOpts

func main() {
flag.Usage = func() {
w := os.Stderr
for _, arg := range os.Args {
if arg == "-h" {
w = os.Stdout
break
}
}
fmt.Fprintf(w, usage, version, name)
cmd := &cobra.Command{
Use: "backport",
Short: "Generate backport issues and cherry pick commits to branches",
Long: "The backport utility needs to be executed inside the repository you want to perform the actions",
RunE: backport,
}

flag.BoolVar(&vers, "v", false, "")
flag.StringVar(&owner, "o", "", "")
flag.StringVar(&repo, "r", "", "")
flag.StringVar(&commitIDs, "c", "", "")
flag.UintVar(&issueID, "i", 0, "")
flag.StringVar(&branches, "b", "", "")
flag.StringVar(&user, "u", "", "")
flag.Parse()
cmd.Flags().StringVarP(&backportCmdOpts.Repo, "repo", "r", "", "name of the repository to perform the backport (k3s, rke2)")
cmd.Flags().UintVarP(&backportCmdOpts.Issue, "issue", "i", 0, "ID of the original issue")
cmd.Flags().StringSliceVarP(&backportCmdOpts.Commits, "commits", "c", []string{}, "commits to be backported, if none is provided, only the issues will be created, when passing this flag, it assumes you're running from the repository this operation is related to (comma separated)")
cmd.Flags().StringSliceVarP(&backportCmdOpts.Branches, "branches", "b", []string{}, "branches the issue is being backported to, one or more (comma separated)")
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")

if vers {
fmt.Fprintf(os.Stdout, "version: %s - git sha: %s\n", version, gitSHA)
return
if err := cmd.MarkFlagRequired("repo"); err != nil {
logrus.Fatal(err)
}

ghToken := os.Getenv("GITHUB_TOKEN")
if ghToken == "" {
fmt.Println("error: please provide a GITHUB_TOKEN")
os.Exit(1)
if err := cmd.MarkFlagRequired("owner"); err != nil {
logrus.Fatal(err)
}

var commits []string
if commitIDs != "" {
commits = strings.Split(commitIDs, ",")
if err := cmd.MarkFlagRequired("issue"); err != nil {
logrus.Fatal(err)
}
if err := cmd.MarkFlagRequired("branches"); err != nil {
logrus.Fatal(err)
}

if issueID == 0 {
fmt.Println("error: please provide a valid issue id")
os.Exit(1)
if err := cmd.Execute(); err != nil {
logrus.Fatal(err)
}
}

func backport(cmd *cobra.Command, args []string) error {
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" {
return errors.New("env variable GITHUB_TOKEN is required")
}
ctx := context.Background()

client := repository.NewGithub(ctx, ghToken)

pbo := repository.PerformBackportOpts{
Owner: owner,
Repo: repo,
Commits: commits,
IssueID: issueID,
Branches: branches,
User: user,
githubClient := repository.NewGithub(ctx, githubToken)

pbo := &repository.PerformBackportOpts{
Owner: backportCmdOpts.Owner,
Repo: backportCmdOpts.Repo,
Branches: backportCmdOpts.Branches,
Commits: backportCmdOpts.Commits,
IssueID: backportCmdOpts.Issue,
User: backportCmdOpts.User,
DryRun: backportCmdOpts.DryRun,
}
issues, err := repository.PerformBackport(ctx, client, &pbo)
issues, err := repository.PerformBackport(ctx, githubClient, pbo)
if err != nil {
fmt.Println(err)
os.Exit(1)
return err
}

for _, issue := range issues {
fmt.Println("Backport issue created: " + issue.GetHTMLURL())
logrus.Info("Backport issue created: " + issue.GetHTMLURL())
}

os.Exit(0)
return nil
}
11 changes: 8 additions & 3 deletions cmd/k3s_release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Please reference the help menu from the binary.

## Configuration
| Name | Description |
|------------------|------------------------------------------------------------------------------------------------------------|
| ---------------- | ---------------------------------------------------------------------------------------------------------- |
| old_k8s_version | Previous k8s patch version |
| new_k8s_version | Latest released k8s patch version |
| old_k8s_client | Previous k8s client patch version, usually the same as the k8s version, but with a major of 0 instead of 1 |
Expand Down Expand Up @@ -50,9 +50,14 @@ Example:
"ssh_key_path": "$HOME/.ssh/github"
}
```
Export your Github token as an environment variable:
Export your Github token as an environment variable and run commands:
```bash
export GITHUB_TOKEN=your_token
export GITHUB_TOKEN={YOUR_TOKEN}
create-tags -c config.json
push-tags -c config.json
modify-k3s -c config.json
tag-rc-release -c config.json
tag-release -c config.json
```

## Errors
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ require (

require (
github.com/MetalBlueberry/go-plotly v0.4.0
github.com/spf13/cobra v1.8.0
github.com/urfave/cli/v2 v2.25.7
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
Expand Down
9 changes: 8 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -230,6 +231,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
Expand Down Expand Up @@ -283,6 +286,10 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
Loading