mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-23 07:31:09 +02:00
After the merge workflow has been reversed and branches hosted in `hashicorp/vault` are downstream from community branches hosted in `hashicorp/vault-enterprise`, most contributions to the source code will originate in `hashicorp/vault-enterprise` and be backported to a community branch in hosted in `hashicorp/vault-enterprise`. These community branches will be considered the primary source of truth and we'll automatically push changes from them to mirrors hosted in `hashicorp/vault`. This workflow ought to yield a massive efficiency boost for HashiCorp contributors with access to `hashicorp/vault-enterprise`. Before the workflow would look something like: - Develop a change in vault-enterprise - Manually extract relevant changes from your vault-enterprise branch into a new community vault branch. - Add any stubs that might be required so as to support any enterprise only changes. - Get the community change reviewed. If changes are necessary it often means changing and testing them on both the enteprise and community branches. - Merge the community change - Wait for it to sync to enterprise - *Hope you changes have not broken the build*. If they have, fix the build. - Update your enterprise branch - Get the enterprise branch reviewed again - Merge enterprise change - Deal with complicated backports. After the workflow will look like: - Develop the change on enterprise - Get the change reviewed - Address feedback and test on the same branch - Merge the change - Automation will extract community changes and create a community backport PR for you depending on changes files and branch activeness. - Automation will create any enterprise backports for you. - Fix any backport as necessary - Merge the changes - The pipeline will automatically push the changes to the community branch mirror hosted in hashicorp/vault. No more - Duplicative reviews - Risky merges - Waiting for changes to sync from community to enterprise - Manual decompistion of changes from enterprise and community - *Doing the same PR 3 times* - Dealing with a different backport process depending on which branches are active or not. These changes do come at cost however. Since they always originate from `vault-enterprise` only HashiCorp employees can take advatange of the workflow. We need to be able to support community contributions that originate from the mirrors but retain attribution. That's what this PR is designed to do. The community will be able to open a pull request as normal and have it reviewed as such, but rather than merging it into the mirror we'll instead copy the PR and open it against the corresponding enterprise base branch and have it merged it from there. The change will automatically get backported to the community branch if necessary, which eventually makes it back to the mirror in hashicorp/vault. To handle our squash merge workflow while retaining the correct attribution, we'll automatically create merge commits in the copied PR that include `Co-Authored-By:` trailers for all commit authors on the original PR. We also take care to ensure that the HashiCorp maitainers that approve the PR and/or are assigned to it are also assigned to the copied PR. This change is only the tooling to enable it. The workflow that drives it will be implemented in VAULT-34827. Signed-off-by: Ryan Cragun <me@ryan.ec>
117 lines
5.0 KiB
Go
117 lines
5.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/vault/tools/pipeline/internal/pkg/changed"
|
|
"github.com/hashicorp/vault/tools/pipeline/internal/pkg/github"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var createGithubBackportState struct {
|
|
req github.CreateBackportReq
|
|
ceExclude []string
|
|
ceAllowInactive []string
|
|
}
|
|
|
|
func newCreateGithubBackportCmd() *cobra.Command {
|
|
backportCmd := &cobra.Command{
|
|
Use: "backport 1234",
|
|
Short: "Create a backport pull request from another pull request",
|
|
Long: "Create a backport pull request from another pull request",
|
|
RunE: runCreateGithubBackportCmd,
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
switch len(args) {
|
|
case 1:
|
|
pr, err := strconv.ParseUint(args[0], 10, 0)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid pull number: %s: %w", args[0], err)
|
|
}
|
|
if pr <= math.MaxUint32 {
|
|
createGithubBackportState.req.PullNumber = uint(pr)
|
|
} else {
|
|
return fmt.Errorf("invalid pull number: %s: number is too large", args[0])
|
|
}
|
|
return nil
|
|
case 0:
|
|
return errors.New("no pull request number has been provided")
|
|
default:
|
|
return fmt.Errorf("invalid arguments: only pull request number is expected, received %d arguments: %v", len(args), args)
|
|
}
|
|
},
|
|
}
|
|
|
|
backportCmd.PersistentFlags().StringSliceVarP(&createGithubBackportState.ceAllowInactive, "ce-allow-inactive-groups", "a", []string{"docs", "changelog", "pipeline"}, "Change file groups that should be allowed to backport to inactive CE branches")
|
|
backportCmd.PersistentFlags().StringVar(&createGithubBackportState.req.CEBranchPrefix, "ce-branch-prefix", "ce", "The branch name prefix")
|
|
backportCmd.PersistentFlags().StringSliceVarP(&createGithubBackportState.ceExclude, "ce-exclude-groups", "e", []string{"enterprise"}, "Change file groups that should be excluded from the backporting to CE branches")
|
|
backportCmd.PersistentFlags().StringVar(&createGithubBackportState.req.BaseOrigin, "base-origin", "origin", "The name to use for the base remote origin")
|
|
backportCmd.PersistentFlags().StringVarP(&createGithubBackportState.req.Owner, "owner", "o", "hashicorp", "The Github organization")
|
|
backportCmd.PersistentFlags().StringVarP(&createGithubBackportState.req.Repo, "repo", "r", "vault-enterprise", "The Github repository. Private repositories require auth via a GITHUB_TOKEN env var")
|
|
backportCmd.PersistentFlags().StringVarP(&createGithubBackportState.req.RepoDir, "repo-dir", "d", "", "The path to the vault repository dir. If not set a temporary directory will be used")
|
|
backportCmd.PersistentFlags().StringVarP(&createGithubBackportState.req.ReleaseVersionConfigPath, "releases-version-path", "m", "", "The path to .release/versions.hcl")
|
|
backportCmd.PersistentFlags().UintVar(&createGithubBackportState.req.ReleaseRecurseDepth, "recurse", 3, "If no path to a config file is given, recursively search backwards for it and stop at root or until we've his the configured depth.")
|
|
|
|
// NOTE: The following are technically flags but they only for testing testing
|
|
// the command before we cut over to new utility.
|
|
backportCmd.PersistentFlags().StringVar(&createGithubBackportState.req.EntBranchPrefix, "ent-branch-prefix", "", "The ent branch name prefix. Only used for testing before migration to the new workflow")
|
|
backportCmd.PersistentFlags().StringVar(&createGithubBackportState.req.BackportLabelPrefix, "backport-label-prefix", "backport", "The name to use for the base remote origin")
|
|
|
|
err := backportCmd.PersistentFlags().MarkHidden("ent-branch-prefix")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = backportCmd.PersistentFlags().MarkHidden("backport-label-prefix")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return backportCmd
|
|
}
|
|
|
|
func runCreateGithubBackportCmd(cmd *cobra.Command, args []string) error {
|
|
cmd.SilenceUsage = true // Don't spam the usage on failure
|
|
|
|
for i, ig := range createGithubBackportState.ceAllowInactive {
|
|
if i == 0 && createGithubBackportState.req.CEAllowInactiveGroups == nil {
|
|
createGithubBackportState.req.CEAllowInactiveGroups = changed.FileGroups{}
|
|
}
|
|
createGithubBackportState.req.CEAllowInactiveGroups = createGithubBackportState.req.CEAllowInactiveGroups.Add(changed.FileGroup(ig))
|
|
}
|
|
|
|
for i, eg := range createGithubBackportState.ceExclude {
|
|
if i == 0 && createGithubBackportState.req.CEExclude == nil {
|
|
createGithubBackportState.req.CEExclude = changed.FileGroups{}
|
|
}
|
|
createGithubBackportState.req.CEExclude = createGithubBackportState.req.CEExclude.Add(changed.FileGroup(eg))
|
|
}
|
|
|
|
res := createGithubBackportState.req.Run(context.TODO(), githubCmdState.Github, githubCmdState.Git)
|
|
if res == nil {
|
|
res = &github.CreateBackportRes{}
|
|
}
|
|
if err := res.Err(); err != nil {
|
|
res.ErrorMessage = err.Error()
|
|
}
|
|
|
|
switch rootCfg.format {
|
|
case "json":
|
|
b, err := res.ToJSON()
|
|
if err != nil {
|
|
return errors.Join(res.Err(), err)
|
|
}
|
|
fmt.Println(string(b))
|
|
default:
|
|
fmt.Println(res.ToTable().Render())
|
|
}
|
|
|
|
return res.Err()
|
|
}
|