From 13e6cc9ccd60f7ab1ea2cc991a8d4e9d51c85c0f Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Wed, 26 Oct 2022 17:13:23 +0200 Subject: [PATCH 1/8] Remove promte and publish subcommands --- cmd/promote.go | 263 --------------------------- cmd/publish.go | 78 -------- cmd/root.go | 2 - internal/promote/pull_requests.go | 106 ----------- internal/promote/removed_packages.go | 36 ---- internal/publish/publish.go | 181 ------------------ internal/publish/publish_test.go | 30 --- internal/publish/pull_requests.go | 122 ------------- 8 files changed, 818 deletions(-) delete mode 100644 cmd/promote.go delete mode 100644 cmd/publish.go delete mode 100644 internal/promote/pull_requests.go delete mode 100644 internal/promote/removed_packages.go delete mode 100644 internal/publish/publish.go delete mode 100644 internal/publish/publish_test.go delete mode 100644 internal/publish/pull_requests.go diff --git a/cmd/promote.go b/cmd/promote.go deleted file mode 100644 index d2e23806fd..0000000000 --- a/cmd/promote.go +++ /dev/null @@ -1,263 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package cmd - -import ( - "fmt" - "strings" - "time" - - "github.com/AlecAivazis/survey/v2" - "github.com/pkg/errors" - "github.com/spf13/cobra" - - "github.com/elastic/elastic-package/internal/cobraext" - "github.com/elastic/elastic-package/internal/github" - "github.com/elastic/elastic-package/internal/promote" - "github.com/elastic/elastic-package/internal/storage" -) - -const promoteLongDescription = `Use this command to move packages between the snapshot, staging, and production stages of the package registry. - -This command is intended primarily for use by administrators. - -It allows for selecting packages for promotion and opens new pull requests to review changes. Please be aware that the tool checks out an in-memory Git repository and switches over branches (snapshot, staging and production), so it may take longer to promote a larger number of packages.` - -const ( - promoteDirectionSnapshotStaging = "snapshot-staging" - promoteDirectionStagingProduction = "staging-production" - promoteDirectionSnapshotProduction = "snapshot-production" -) - -var promotionDirections = []string{promoteDirectionSnapshotStaging, promoteDirectionStagingProduction, promoteDirectionSnapshotProduction} - -func setupPromoteCommand() *cobraext.Command { - cmd := &cobra.Command{ - Use: "promote", - Short: "Promote packages", - Long: promoteLongDescription, - RunE: promoteCommandAction, - SilenceUsage: true, - } - cmd.Flags().StringP(cobraext.DirectionFlagName, "d", "", cobraext.DirectionFlagDescription) - cmd.Flags().BoolP(cobraext.NewestOnlyFlagName, "n", false, cobraext.NewestOnlyFlagDescription) - cmd.Flags().StringSliceP(cobraext.PromotedPackagesFlagName, "p", nil, cobraext.PromotedPackagesFlagDescription) - - return cobraext.NewCommand(cmd, cobraext.ContextGlobal) -} - -func promoteCommandAction(cmd *cobra.Command, _ []string) error { - cmd.Println("Promote packages") - cmd.Println("DEPRECATED: Packages stored in the Package Storage v2 won't require to be promoted. This command will be removed soon. README: https://github.com/elastic/elastic-package/blob/main/docs/howto/use_package_storage_v2.md") - - // Setup GitHub - err := github.EnsureAuthConfigured() - if err != nil { - return errors.Wrap(err, "GitHub auth configuration failed") - } - - githubClient, err := github.Client() - if err != nil { - return errors.Wrap(err, "creating GitHub client failed") - } - - githubUser, err := github.User(githubClient) - if err != nil { - return errors.Wrap(err, "fetching GitHub user failed") - } - cmd.Printf("Current GitHub user: %s\n", githubUser) - - // Prompt for promotion options - sourceStage, destinationStage, err := promptPromotion(cmd) - if err != nil { - return errors.Wrap(err, "prompt for promotion failed") - } - - newestOnly, err := promptPromoteNewestOnly(cmd) - if err != nil { - return errors.Wrap(err, "prompt for promoting newest versions only failed") - } - - cmd.Println("Cloning repository...") - repository, err := storage.CloneRepository(githubUser, sourceStage) - if err != nil { - return errors.Wrapf(err, "cloning source repository failed (branch: %s)", sourceStage) - } - - cmd.Println("Creating list of packages...") - allPackages, err := storage.ListPackages(repository) - if err != nil { - return errors.Wrapf(err, "listing packages failed") - } - - packagesToBeSelected := allPackages.FilterPackages(newestOnly) - if len(packagesToBeSelected) == 0 { - fmt.Println("No packages available for promotion.") - return nil - } - - promotedPackages, err := promptPackages(cmd, packagesToBeSelected) - if err != nil { - return errors.Wrap(err, "prompt for package selection failed") - } - - removedPackages := promote.DeterminePackagesToBeRemoved(allPackages, promotedPackages, newestOnly) - - nonce := time.Now().UnixNano() - // Copy packages to destination - fmt.Printf("Promote packages from %s to %s...\n", sourceStage, destinationStage) - newDestinationBranch := fmt.Sprintf("promote-from-%s-to-%s-%d", sourceStage, destinationStage, nonce) - err = storage.CopyPackages(repository, sourceStage, destinationStage, promotedPackages, newDestinationBranch) - if err != nil { - return errors.Wrapf(err, "copying packages failed (source: %s, destination: %s)", sourceStage, destinationStage) - } - - // Remove packages from source - newSourceBranch := fmt.Sprintf("delete-from-%s-%d", sourceStage, nonce) - err = storage.RemovePackages(repository, sourceStage, removedPackages, newSourceBranch) - if err != nil { - return errors.Wrapf(err, "removing packages failed (source: %s)", sourceStage) - } - - // Push changes - err = storage.PushChanges(githubUser, repository, newSourceBranch, newDestinationBranch) - if err != nil { - return errors.Wrapf(err, "pushing changes failed") - } - - // Calculate package signatures - signedPackages, err := storage.CalculatePackageSignatures(repository, newDestinationBranch, promotedPackages) - if err != nil { - return errors.Wrap(err, "signing packages failed") - } - - // Open PRs - url, err := promote.OpenPullRequestWithPromotedPackages(githubClient, githubUser, newDestinationBranch, destinationStage, sourceStage, destinationStage, signedPackages) - if err != nil { - return errors.Wrapf(err, "opening PR with promoted packages failed (head: %s, base: %s)", newDestinationBranch, destinationStage) - } - cmd.Println("Pull request with promoted packages:", url) - - url, err = promote.OpenPullRequestWithRemovedPackages(githubClient, githubUser, newSourceBranch, sourceStage, sourceStage, url, removedPackages) - if err != nil { - return errors.Wrapf(err, "opening PR with removed packages failed (head: %s, base: %s)", newDestinationBranch, destinationStage) - } - cmd.Println("Pull request with removed packages:", url) - - cmd.Println("Done") - return nil -} - -func promptPromotion(cmd *cobra.Command) (string, string, error) { - direction, err := cmd.Flags().GetString(cobraext.DirectionFlagName) - if err != nil { - return "", "", errors.Wrapf(err, "can't read %s flag:", cobraext.DirectionFlagName) - } - - if direction != "" { - if !isSupportedPromotionDirection(direction) { - return "", "", fmt.Errorf("unsupported promotion direction, use: %s", - strings.Join(promotionDirections, ", ")) - } - - s := strings.Split(direction, "-") - return s[0], s[1], nil - } - - promotionPrompt := &survey.Select{ - Message: "Which promotion would you like to run", - Options: promotionDirections, - Default: promoteDirectionSnapshotStaging, - } - - err = survey.AskOne(promotionPrompt, &direction) - if err != nil { - return "", "", err - } - - s := strings.Split(direction, "-") - return s[0], s[1], nil -} - -func isSupportedPromotionDirection(direction string) bool { - for _, d := range promotionDirections { - if d == direction { - return true - } - } - return false -} - -func promptPromoteNewestOnly(cmd *cobra.Command) (bool, error) { - newestOnly := false - - newestOnlyFlag := cmd.Flags().Lookup(cobraext.NewestOnlyFlagName) - if newestOnlyFlag.Changed { - newestOnly, _ = cmd.Flags().GetBool(cobraext.NewestOnlyFlagName) - return newestOnly, nil - } - - prompt := &survey.Confirm{ - Message: "Would you like to promote newest versions only and remove older ones?", - Default: true, - } - err := survey.AskOne(prompt, &newestOnly) - if err != nil { - return false, err - } - return newestOnly, nil -} - -func promptPackages(cmd *cobra.Command, packages storage.PackageVersions) (storage.PackageVersions, error) { - revisions, _ := cmd.Flags().GetStringSlice(cobraext.PromotedPackagesFlagName) - if len(revisions) > 0 { - parsed, err := storage.ParsePackageVersions(revisions) - if err != nil { - return nil, errors.Wrap(err, "can't parse package versions") - } - return selectPackageVersions(packages, parsed) - } - - packagesPrompt := &survey.MultiSelect{ - Message: "Which packages would you like to promote", - Options: packages.Strings(), - PageSize: 100, - } - - var selectedOptions []string - err := survey.AskOne(packagesPrompt, &selectedOptions, survey.WithValidator(survey.Required)) - if err != nil { - return nil, err - } - - var selected storage.PackageVersions - for _, option := range selectedOptions { - for _, p := range packages { - if p.String() == option { - selected = append(selected, p) - } - } - } - return selected, nil -} - -func selectPackageVersions(packages storage.PackageVersions, toBeSelected storage.PackageVersions) (storage.PackageVersions, error) { - var selected storage.PackageVersions - for _, r := range toBeSelected { - var found bool - for _, pv := range packages { - if pv.Equal(r) { - selected = append(selected, pv) - found = true - break - } - } - - if !found { - return nil, fmt.Errorf("package revision is not present (%s) in the source stage, try to run the command without %s flag", r.String(), cobraext.NewestOnlyFlagName) - } - } - return selected, nil -} diff --git a/cmd/publish.go b/cmd/publish.go deleted file mode 100644 index 535ac4b4a2..0000000000 --- a/cmd/publish.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package cmd - -import ( - "github.com/pkg/errors" - "github.com/spf13/cobra" - - "github.com/elastic/elastic-package/internal/cobraext" - "github.com/elastic/elastic-package/internal/github" - "github.com/elastic/elastic-package/internal/publish" -) - -const publishLongDescription = `Use this command to publish a new package revision. - -The command checks if the package hasn't been already published (whether it's present in snapshot/staging/production branch or open as pull request). If the package revision hasn't been published, it will open a new pull request.` - -func setupPublishCommand() *cobraext.Command { - cmd := &cobra.Command{ - Use: "publish", - Short: "Publish the package to the Package Registry", - Long: publishLongDescription, - RunE: publishCommandAction, - } - - // Fork flag can be a workaround for users that don't own forks of the Package Storage. - cmd.Flags().BoolP(cobraext.ForkFlagName, "f", true, cobraext.ForkFlagDescription) - - // SkipPullRequest flag can used to verify if the "publish" command works properly (finds correct revisions), - // for which the operator doesn't want to immediately close just opened PRs (standard dry-run). - cmd.Flags().BoolP(cobraext.SkipPullRequestFlagName, "s", false, cobraext.SkipPullRequestFlagDescription) - - return cobraext.NewCommand(cmd, cobraext.ContextPackage) -} - -func publishCommandAction(cmd *cobra.Command, args []string) error { - cmd.Println("Publish the package") - cmd.Println("DEPRECATED: Package candidates to the Package Storage v2 will be published using Jenkins jobs. This command will be removed soon. README: https://github.com/elastic/elastic-package/blob/main/docs/howto/use_package_storage_v2.md") - - fork, err := cmd.Flags().GetBool(cobraext.ForkFlagName) - if err != nil { - return cobraext.FlagParsingError(err, cobraext.ForkFlagName) - } - - skipPullRequest, err := cmd.Flags().GetBool(cobraext.SkipPullRequestFlagName) - if err != nil { - return cobraext.FlagParsingError(err, cobraext.SkipPullRequestFlagName) - } - - // Setup GitHub - err = github.EnsureAuthConfigured() - if err != nil { - return errors.Wrap(err, "GitHub auth configuration failed") - } - - githubClient, err := github.Client() - if err != nil { - return errors.Wrap(err, "creating GitHub client failed") - } - - // GitHub user - githubUser, err := github.User(githubClient) - if err != nil { - return errors.Wrap(err, "fetching GitHub user failed") - } - cmd.Printf("Current GitHub user: %s\n", githubUser) - - // Publish the package - err = publish.Package(githubUser, githubClient, fork, skipPullRequest) - if err != nil { - return errors.Wrap(err, "can't publish the package") - } - - cmd.Println("Done") - return nil -} diff --git a/cmd/root.go b/cmd/root.go index 9cb7ba3902..292febfdd1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,8 +27,6 @@ var commands = []*cobraext.Command{ setupInstallCommand(), setupLintCommand(), setupProfilesCommand(), - setupPromoteCommand(), - setupPublishCommand(), setupServiceCommand(), setupStackCommand(), setupStatusCommand(), diff --git a/internal/promote/pull_requests.go b/internal/promote/pull_requests.go deleted file mode 100644 index ec03ef1e23..0000000000 --- a/internal/promote/pull_requests.go +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package promote - -import ( - "context" - "fmt" - "strings" - - "github.com/google/go-github/v32/github" - "github.com/pkg/errors" - - "github.com/elastic/elastic-package/internal/storage" -) - -const ( - repositoryOwner = "elastic" - repositoryName = "package-storage" - - githubTitleCharacterLimit = 256 -) - -// OpenPullRequestWithRemovedPackages method opens a PR against "base" branch with removed packages. -// Head is the branch containing the changes that will be added to the base branch. -func OpenPullRequestWithRemovedPackages(client *github.Client, username, head, base, sourceStage, promotionURL string, removedPackages storage.PackageVersions) (string, error) { - title := buildPullRequestRemoveTitle(sourceStage, removedPackages) - description := buildPullRequestRemoveDescription(sourceStage, promotionURL, removedPackages) - return openPullRequestWithPackages(client, username, head, base, title, description) -} - -// OpenPullRequestWithPromotedPackages method opens a PR against "base" branch with promoted packages. -// Head is the branch containing the changes that will be added to the base branch. -func OpenPullRequestWithPromotedPackages(client *github.Client, username, head, base, sourceStage, destinationStage string, signedPackages storage.SignedPackageVersions) (string, error) { - title := buildPullRequestPromoteTitle(sourceStage, destinationStage, signedPackages.ToPackageVersions()) - description := buildPullRequestPromoteDescription(sourceStage, destinationStage, signedPackages) - return openPullRequestWithPackages(client, username, head, base, title, description) -} - -func openPullRequestWithPackages(client *github.Client, user, head, base, title, description string) (string, error) { - userHead := fmt.Sprintf("%s:%s", user, head) - maintainerCanModify := true - pullRequest, _, err := client.PullRequests.Create(context.Background(), repositoryOwner, repositoryName, &github.NewPullRequest{ - Title: &title, - Head: &userHead, - Base: &base, - Body: &description, - MaintainerCanModify: &maintainerCanModify, - }) - if err != nil { - return "", errors.Wrap(err, "opening pull request failed") - } - - _, _, err = client.Issues.Edit(context.Background(), repositoryOwner, repositoryName, *pullRequest.Number, &github.IssueRequest{ - Assignees: &[]string{ - user, - }, - }) - if err != nil { - return "", errors.Wrap(err, "editing assignees in the pull request failed") - } - return *pullRequest.HTMLURL, nil -} - -func buildPullRequestPromoteTitle(sourceStage, destinationStage string, promotedPackages storage.PackageVersions) string { - details := strings.Join(promotedPackages.Strings(), ", ") - title := fmt.Sprintf("[%s] Promote packages from %s (%s)", destinationStage, sourceStage, details) - if len(title) > githubTitleCharacterLimit { - return fmt.Sprintf("[%s] Promote many packages from %s", destinationStage, sourceStage) - } - return title -} - -func buildPullRequestPromoteDescription(sourceStage, destinationStage string, signedPackages storage.SignedPackageVersions) string { - var builder strings.Builder - builder.WriteString(fmt.Sprintf("This PR promotes packages from `%s` to `%s`.\n", sourceStage, destinationStage)) - builder.WriteString("\n") - builder.WriteString("Promoted packages:\n") - for _, str := range signedPackages.Strings() { - builder.WriteString(fmt.Sprintf("* `%s`\n", str)) - } - return builder.String() -} - -func buildPullRequestRemoveTitle(stage string, removedPackages storage.PackageVersions) string { - details := strings.Join(removedPackages.Strings(), ", ") - title := fmt.Sprintf("[%s] Remove promoted packages (%s)", stage, details) - if len(title) > githubTitleCharacterLimit { - return fmt.Sprintf("[%s] Remove many promoted packages", stage) - } - return title -} - -func buildPullRequestRemoveDescription(sourceStage, promotionURL string, versions storage.PackageVersions) string { - var builder strings.Builder - builder.WriteString(fmt.Sprintf("This PR removes packages from `%s`.\n", sourceStage)) - builder.WriteString("\n") - builder.WriteString("Removed packages:\n") - for _, str := range versions.Strings() { - builder.WriteString(fmt.Sprintf("* `%s`\n", str)) - } - builder.WriteString("\n") - builder.WriteString(fmt.Sprintf("Please make sure that the promotion PR is merged first: %s", promotionURL)) - return builder.String() -} diff --git a/internal/promote/removed_packages.go b/internal/promote/removed_packages.go deleted file mode 100644 index c26f442eb0..0000000000 --- a/internal/promote/removed_packages.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package promote - -import "github.com/elastic/elastic-package/internal/storage" - -// DeterminePackagesToBeRemoved method lists packages supposed to be removed from the stage. -func DeterminePackagesToBeRemoved(allPackages storage.PackageVersions, promotedPackages storage.PackageVersions, newestOnly bool) storage.PackageVersions { - var removed storage.PackageVersions - - for _, p := range allPackages { - var toBeRemoved bool - - for _, r := range promotedPackages { - if p.Name != r.Name { - continue - } - - if newestOnly { - toBeRemoved = true - break - } - - if p.Equal(r) { - toBeRemoved = true - } - } - - if toBeRemoved { - removed = append(removed, p) - } - } - return removed -} diff --git a/internal/publish/publish.go b/internal/publish/publish.go deleted file mode 100644 index 088ea3d4ed..0000000000 --- a/internal/publish/publish.go +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package publish - -import ( - "fmt" - "os" - "path/filepath" - "strings" - "time" - - "github.com/go-git/go-git/v5" - "github.com/google/go-github/v32/github" - "github.com/pkg/errors" - - "github.com/elastic/elastic-package/internal/builder" - "github.com/elastic/elastic-package/internal/logger" - "github.com/elastic/elastic-package/internal/packages" - "github.com/elastic/elastic-package/internal/storage" -) - -const ( - snapshotStage = "snapshot" - stagingStage = "staging" - productionStage = "production" -) - -// Package function publishes the current package to the package-storage. -func Package(githubUser string, githubClient *github.Client, fork, skipPullRequest bool) error { - packageRoot, err := packages.MustFindPackageRoot() - if err != nil { - return errors.Wrap(err, "locating package root failed") - } - - m, err := packages.ReadPackageManifestFromPackageRoot(packageRoot) - if err != nil { - return errors.Wrapf(err, "reading package manifest failed (path: %s)", packageRoot) - } - - buildDir, found, err := builder.FindBuildPackagesDirectory() - if err != nil { - return errors.Wrap(err, "locating build directory failed") - } - if !found { - return errors.New("build directory not found. Please run 'elastic-package build' first") - } - - builtPackageDir := filepath.Join(buildDir, m.Name, m.Version) - fmt.Printf("Use build directory: %s\n", builtPackageDir) - _, err = os.Stat(builtPackageDir) - if errors.Is(err, os.ErrNotExist) { - return errors.Wrapf(err, "build directory '%s' is missing. Please run 'elastic-package build' first", builtPackageDir) - } - if err != nil { - return errors.Wrapf(err, "stat file failed: %s", builtPackageDir) - } - - fmt.Println("Clone package-storage repository") - r, err := storage.CloneRepositoryWithFork(githubUser, productionStage, fork) - if err != nil { - return errors.Wrap(err, "cloning source repository failed") - } - - fmt.Printf("Find latest package revision of \"%s\" in package-storage\n", m.Name) - latestRevision, stage, err := findLatestPackageRevision(r, m.Name) - if err != nil { - return errors.Wrap(err, "can't find latest package revision") - } - - if latestRevision == nil { - logger.Debugf("Package has not been published yet") - } else if latestRevision.Version == m.Version { - fmt.Printf("Package has already been published (stage: %s, version: %s)\n", stage, latestRevision.Version) - return nil - } else { - logger.Debugf("Latest package revision: %s (stage: %s)", latestRevision.String(), stage) - logger.Debugf("Copy sources of the latest package revision to index") - } - - fmt.Println("Check if pull request is already open") - alreadyOpen, err := checkIfPullRequestAlreadyOpen(githubClient, *m) - if err != nil { - return errors.Wrapf(err, "can't check if pull request is already open") - } - if alreadyOpen { - fmt.Println("Pull request with package update is already open") - return nil - } - - destination, err := copyLatestRevisionIfAvailable(r, latestRevision, stage, m) - if err != nil { - return errors.Wrap(err, "can't copy sources of latest package revision") - } - - commitHash, err := storage.CopyOverLocalPackage(r, buildDir, m) - if err != nil { - return errors.Wrap(err, "can't copy over the updated package") - } - - fmt.Println("Push new package revision to storage") - err = storage.PushChangesWithFork(githubUser, r, fork, destination) - if err != nil { - return errors.Wrapf(err, "pushing changes failed") - } - - if skipPullRequest { - fmt.Println("Skip opening a new pull request") - return nil - } - - fmt.Println("Open new pull request") - err = openPullRequest(githubClient, githubUser, destination, *m, commitHash, fork) - if err != nil { - return errors.Wrapf(err, "can't open a new pull request") - } - return nil -} - -func findLatestPackageRevision(r *git.Repository, packageName string) (*storage.PackageVersion, string, error) { - var revisions storage.PackageVersions - - revisionStageMap := map[string]string{} - for _, currentStage := range []string{productionStage, stagingStage, snapshotStage} { - logger.Debugf("Find revisions of the \"%s\" package in %s", packageName, currentStage) - err := storage.ChangeStage(r, currentStage) - if err != nil { - return nil, "", errors.Wrapf(err, "can't change stage to %s", currentStage) - } - - revs, err := storage.ListPackagesByName(r, packageName) - if err != nil { - return nil, "", errors.Wrapf(err, "can't list packages") - } - - for _, rev := range revs { - logger.Debugf("Found package revision: %s", rev.String()) - revisionStageMap[rev.String()] = currentStage - } - revisions = append(revisions, revs...) - } - - if len(revisions) == 0 { - logger.Debugf("No published revisions of the \"%s\" package so far. It seems to be brand new.", packageName) - return nil, "", nil - } - - revisions = revisions.FilterPackages(true) - latest := revisions[0] - return &latest, revisionStageMap[latest.String()], nil -} - -func copyLatestRevisionIfAvailable(r *git.Repository, latestRevision *storage.PackageVersion, stage string, manifest *packages.PackageManifest) (string, error) { - nonce := time.Now().UnixNano() - destinationBranch := fmt.Sprintf("update-%s-%d", snapshotStage, nonce) - err := storage.CopyPackagesWithTransform(r, stage, snapshotStage, optionalPackageVersions(latestRevision), destinationBranch, createRewriteResourcePath(manifest)) - if err != nil { - return "", errors.Wrap(err, "can't copy latest revision") - } - return destinationBranch, nil -} - -func optionalPackageVersions(pv *storage.PackageVersion) storage.PackageVersions { - if pv != nil { - return storage.PackageVersions{*pv} - } - return storage.PackageVersions{} -} - -func createRewriteResourcePath(manifest *packages.PackageManifest) func(string, []byte) (string, []byte) { - return func(resourcePath string, content []byte) (string, []byte) { - prefix := "packages/" + manifest.Name + "/" - if !strings.HasPrefix(resourcePath, prefix) { - return resourcePath, content - } - resourcePath = resourcePath[len(prefix):] - i := strings.IndexByte(resourcePath, '/') - return prefix + manifest.Version + resourcePath[i:], content - } -} diff --git a/internal/publish/publish_test.go b/internal/publish/publish_test.go deleted file mode 100644 index b184434cd8..0000000000 --- a/internal/publish/publish_test.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package publish - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/elastic/elastic-package/internal/packages" -) - -func TestCreateRewriteResourcePath(t *testing.T) { - rewrittenVersion := "0.4.1" - f := createRewriteResourcePath(&packages.PackageManifest{ - Name: "nginx", - Version: rewrittenVersion, - }) - - resourceTemplate := "packages/nginx/%s/data_stream/stubstatus/agent/stream/stream.yml.hbs" - resourcePath := fmt.Sprintf(resourceTemplate, "0.4.0") - content := []byte("HELLO WORLD") - - actualResourcePath, actualContent := f(resourcePath, content) - assert.Equal(t, string(content), string(actualContent)) - assert.Equal(t, fmt.Sprintf(resourceTemplate, rewrittenVersion), actualResourcePath) -} diff --git a/internal/publish/pull_requests.go b/internal/publish/pull_requests.go deleted file mode 100644 index 8c812ed281..0000000000 --- a/internal/publish/pull_requests.go +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. - -package publish - -import ( - "context" - "fmt" - "strings" - - "github.com/google/go-github/v32/github" - "github.com/pkg/errors" - - "github.com/elastic/elastic-package/internal/logger" - "github.com/elastic/elastic-package/internal/packages" -) - -const ( - repositoryName = "package-storage" - repositoryOwner = "elastic" - - defaultPackageOwnerTeam = "elastic/integrations" -) - -func checkIfPullRequestAlreadyOpen(githubClient *github.Client, manifest packages.PackageManifest) (bool, error) { - expectedTitle := buildPullRequestTitle(manifest) - q := fmt.Sprintf(`repo:elastic/package-storage base:snapshot is:pr is:open in:title "%s"`, expectedTitle) - logger.Debugf("Use Search API to find an open pull request (query: '%s')", q) - searchResults, _, err := githubClient.Search.Issues(context.Background(), q, new(github.SearchOptions)) - if err != nil { - return false, errors.Wrap(err, "can't search pull requests") - } - - for _, item := range searchResults.Issues { - if *item.Title == expectedTitle { - logger.Debugf("Found pull request: %s", *item.HTMLURL) - return true, nil - } - } - return false, nil -} - -func openPullRequest(githubClient *github.Client, githubUser, destinationBranch string, manifest packages.PackageManifest, commitHash string, fork bool) error { - user := repositoryOwner - if fork { - user = githubUser - } - logger.Debugf("Current user: %s", user) - - title := buildPullRequestTitle(manifest) - diffURL := buildPullRequestDiffURL(user, commitHash) - description := buildPullRequestDescription(manifest, diffURL) - - userHead := fmt.Sprintf("%s:%s", user, destinationBranch) - maintainerCanModify := true - base := snapshotStage - - logger.Debugf("Create new pull request (head: %s, base: %s)", userHead, base) - pullRequest, _, err := githubClient.PullRequests.Create(context.Background(), repositoryOwner, repositoryName, &github.NewPullRequest{ - Title: &title, - Head: &userHead, - Base: &base, - Body: &description, - MaintainerCanModify: &maintainerCanModify, - }) - if err != nil { - return errors.Wrap(err, "can't open new pull request") - } - - logger.Debugf("Pull request URL: %s", *pullRequest.HTMLURL) - - // Try to set reviewers - reviewers := buildReviewersRequest(manifest) - logger.Debugf("Update reviewers (pull request ID: %d)", *pullRequest.Number) - pullRequest, _, err = githubClient.PullRequests.RequestReviewers(context.Background(), repositoryOwner, repositoryName, *pullRequest.Number, reviewers) - if err != nil { - return errors.Wrap(err, "can't request reviewers, please double-check if the owner exists and has access to the repository") - } - - if len(pullRequest.RequestedTeams) != 0 || len(pullRequest.RequestedReviewers) != 0 { - logger.Debugf("Reviewers requested successfully (teams: %d, reviewers: %d)", len(pullRequest.RequestedTeams), len(pullRequest.RequestedReviewers)) - return nil - } - - // Fallback reviewers to default package owner - logger.Debugf("Update reviewers with default owner (pull request ID: %d)", *pullRequest.Number) - _, _, err = githubClient.PullRequests.RequestReviewers(context.Background(), repositoryOwner, repositoryName, *pullRequest.Number, - buildDefaultReviewersRequest()) - if err != nil { - return errors.Wrap(err, "can't request reviewers with default owner") - } - return nil -} - -func buildPullRequestTitle(manifest packages.PackageManifest) string { - return fmt.Sprintf(`[snapshot] Update "%s" package to version %s`, manifest.Name, manifest.Version) -} - -func buildPullRequestDiffURL(username, commitHash string) string { - return fmt.Sprintf("https://github.com/%s/package-storage/commit/%s", username, commitHash) -} - -func buildPullRequestDescription(manifest packages.PackageManifest, diffURL string) string { - return fmt.Sprintf("This PR updates `%s` package to version %s.\n\nChanges: %s", manifest.Name, - manifest.Version, diffURL) -} - -func buildReviewersRequest(manifest packages.PackageManifest) github.ReviewersRequest { - if manifest.Owner.Github == "" { - return buildDefaultReviewersRequest() - } - - if i := strings.Index(manifest.Owner.Github, "/"); i > -1 { - return github.ReviewersRequest{TeamReviewers: []string{manifest.Owner.Github[i+1:]}} - } - return github.ReviewersRequest{Reviewers: []string{manifest.Owner.Github}} -} - -func buildDefaultReviewersRequest() github.ReviewersRequest { - return github.ReviewersRequest{TeamReviewers: []string{defaultPackageOwnerTeam}} -} From caee46452b8cf2b167d2461930b50e6346a13527 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Wed, 26 Oct 2022 18:23:09 +0200 Subject: [PATCH 2/8] Update status command --- cmd/status.go | 2 - cmd/status_test.go | 44 +++++++++---------- cmd/testdata/status-beta-versions | 7 +++ ...ns-in-stage => status-local-version-stage} | 8 ++-- cmd/testdata/status-no-versions | 4 -- cmd/testdata/status-preview-versions | 15 +++---- .../status-release-candidate-versions | 7 +++ cmd/testdata/status-some-versions | 11 ----- cmd/testdata/status-version-one-stage | 4 -- internal/packages/status/status.go | 12 ----- internal/registry/client.go | 6 --- 11 files changed, 43 insertions(+), 77 deletions(-) create mode 100644 cmd/testdata/status-beta-versions rename cmd/testdata/{status-multiple-versions-in-stage => status-local-version-stage} (61%) create mode 100644 cmd/testdata/status-release-candidate-versions delete mode 100644 cmd/testdata/status-some-versions diff --git a/cmd/status.go b/cmd/status.go index 1f9af0636e..d5ed106f00 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -101,8 +101,6 @@ func print(p *status.PackageStatus, w io.Writer) error { cyan.Fprintln(w, formatOwner(p)) environmentTable = append(environmentTable, formatManifest("Local", *p.Local, nil)) } - environmentTable = append(environmentTable, formatManifests("Snapshot", p.Snapshot)) - environmentTable = append(environmentTable, formatManifests("Staging", p.Staging)) environmentTable = append(environmentTable, formatManifests("Production", p.Production)) if p.PendingChanges != nil { diff --git a/cmd/status_test.go b/cmd/status_test.go index a2a0ceb652..c4f35558b2 100644 --- a/cmd/status_test.go +++ b/cmd/status_test.go @@ -25,10 +25,14 @@ func fooPackage(version string) packages.PackageManifest { Version: version, Title: "Foo", Description: "Foo integration", + Owner: packages.Owner{ + Github: "team", + }, } } func TestStatusFormatAndPrint(t *testing.T) { + localPackage := fooPackage("2.0.0-rc1") cases := []struct { title string pkgStatus *status.PackageStatus @@ -50,20 +54,27 @@ func TestStatusFormatAndPrint(t *testing.T) { expected: "./testdata/status-version-one-stage", }, { - title: "some versions", + title: "beta versions", pkgStatus: &status.PackageStatus{ Name: "foo", Production: []packages.PackageManifest{ fooPackage("1.0.0"), - }, - Staging: []packages.PackageManifest{ fooPackage("1.1.0-beta1"), }, - Snapshot: []packages.PackageManifest{ + }, + expected: "./testdata/status-beta-versions", + }, + { + title: "release candidate versions", + pkgStatus: &status.PackageStatus{ + Name: "foo", + Production: []packages.PackageManifest{ + fooPackage("1.0.0"), + fooPackage("1.1.0-beta1"), fooPackage("2.0.0-rc1"), }, }, - expected: "./testdata/status-some-versions", + expected: "./testdata/status-release-candidate-versions", }, { title: "preview versions", @@ -71,40 +82,25 @@ func TestStatusFormatAndPrint(t *testing.T) { Name: "foo", Production: []packages.PackageManifest{ fooPackage("0.9.0"), - }, - Staging: []packages.PackageManifest{ fooPackage("1.0.0-preview1"), - }, - Snapshot: []packages.PackageManifest{ fooPackage("1.0.0-preview5"), }, }, expected: "./testdata/status-preview-versions", }, { - title: "multiple versions in stage", + title: "local version stage", pkgStatus: &status.PackageStatus{ - Name: "foo", + Name: "foo", + Local: &localPackage, Production: []packages.PackageManifest{ fooPackage("1.0.0"), fooPackage("1.0.1"), fooPackage("1.0.2"), - }, - Staging: []packages.PackageManifest{ - fooPackage("1.0.0"), - fooPackage("1.0.1"), - fooPackage("1.0.2"), fooPackage("1.1.0-beta1"), }, - Snapshot: []packages.PackageManifest{ - fooPackage("1.0.0"), - fooPackage("1.0.1"), - fooPackage("1.0.2"), - fooPackage("1.1.0-beta1"), - fooPackage("2.0.0-rc1"), - }, }, - expected: "./testdata/status-multiple-versions-in-stage", + expected: "./testdata/status-local-version-stage", }, } diff --git a/cmd/testdata/status-beta-versions b/cmd/testdata/status-beta-versions new file mode 100644 index 0000000000..5c7b92abdd --- /dev/null +++ b/cmd/testdata/status-beta-versions @@ -0,0 +1,7 @@ +Package: foo +Package Versions: ++-------------+---------------------+---------+-------+-----------------+ +| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | ++-------------+---------------------+---------+-------+-----------------+ +| Production | 1.1.0-beta1 (1.0.0) | Beta | Foo | Foo integration | ++-------------+---------------------+---------+-------+-----------------+ diff --git a/cmd/testdata/status-multiple-versions-in-stage b/cmd/testdata/status-local-version-stage similarity index 61% rename from cmd/testdata/status-multiple-versions-in-stage rename to cmd/testdata/status-local-version-stage index 97732e5105..48bcce131d 100644 --- a/cmd/testdata/status-multiple-versions-in-stage +++ b/cmd/testdata/status-local-version-stage @@ -1,13 +1,11 @@ Package: foo +Owner: team Package Versions: +-------------+--------------------------------+-------------------+-------+-----------------+ | ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | +-------------+--------------------------------+-------------------+-------+-----------------+ -| Snapshot | 2.0.0-rc1 (1.0.0, 1.0.1, | Release Candidate | Foo | Foo integration | -| | 1.0.2, 1.1.0-beta1) | | | | +| Local | 2.0.0-rc1 | Release Candidate | Foo | Foo integration | +-------------+--------------------------------+-------------------+-------+-----------------+ -| Staging | 1.1.0-beta1 (1.0.0, 1.0.1, | Beta | Foo | Foo integration | +| Production | 1.1.0-beta1 (1.0.0, 1.0.1, | Beta | Foo | Foo integration | | | 1.0.2) | | | | +-------------+--------------------------------+-------------------+-------+-----------------+ -| Production | 1.0.2 (1.0.0, 1.0.1) | GA | Foo | Foo integration | -+-------------+--------------------------------+-------------------+-------+-----------------+ diff --git a/cmd/testdata/status-no-versions b/cmd/testdata/status-no-versions index d29d3b4d0b..81f261a04b 100644 --- a/cmd/testdata/status-no-versions +++ b/cmd/testdata/status-no-versions @@ -3,9 +3,5 @@ Package Versions: +-------------+---------+---------+-------+-------------+ | ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | +-------------+---------+---------+-------+-------------+ -| Snapshot | - | - | - | - | -+-------------+---------+---------+-------+-------------+ -| Staging | - | - | - | - | -+-------------+---------+---------+-------+-------------+ | Production | - | - | - | - | +-------------+---------+---------+-------+-------------+ diff --git a/cmd/testdata/status-preview-versions b/cmd/testdata/status-preview-versions index 0c766486b8..093b3d48a0 100644 --- a/cmd/testdata/status-preview-versions +++ b/cmd/testdata/status-preview-versions @@ -1,11 +1,8 @@ Package: foo Package Versions: -+-------------+----------------+-------------------+-------+-----------------+ -| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | -+-------------+----------------+-------------------+-------+-----------------+ -| Snapshot | 1.0.0-preview5 | Technical Preview | Foo | Foo integration | -+-------------+----------------+-------------------+-------+-----------------+ -| Staging | 1.0.0-preview1 | Technical Preview | Foo | Foo integration | -+-------------+----------------+-------------------+-------+-----------------+ -| Production | 0.9.0 | Technical Preview | Foo | Foo integration | -+-------------+----------------+-------------------+-------+-----------------+ ++-------------+--------------------------------+-------------------+-------+-----------------+ +| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | ++-------------+--------------------------------+-------------------+-------+-----------------+ +| Production | 1.0.0-preview5 (0.9.0, | Technical Preview | Foo | Foo integration | +| | 1.0.0-preview1) | | | | ++-------------+--------------------------------+-------------------+-------+-----------------+ diff --git a/cmd/testdata/status-release-candidate-versions b/cmd/testdata/status-release-candidate-versions new file mode 100644 index 0000000000..b33830d5bb --- /dev/null +++ b/cmd/testdata/status-release-candidate-versions @@ -0,0 +1,7 @@ +Package: foo +Package Versions: ++-------------+--------------------------------+-------------------+-------+-----------------+ +| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | ++-------------+--------------------------------+-------------------+-------+-----------------+ +| Production | 2.0.0-rc1 (1.0.0, 1.1.0-beta1) | Release Candidate | Foo | Foo integration | ++-------------+--------------------------------+-------------------+-------+-----------------+ diff --git a/cmd/testdata/status-some-versions b/cmd/testdata/status-some-versions deleted file mode 100644 index d87c8ba0b9..0000000000 --- a/cmd/testdata/status-some-versions +++ /dev/null @@ -1,11 +0,0 @@ -Package: foo -Package Versions: -+-------------+-------------+-------------------+-------+-----------------+ -| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | -+-------------+-------------+-------------------+-------+-----------------+ -| Snapshot | 2.0.0-rc1 | Release Candidate | Foo | Foo integration | -+-------------+-------------+-------------------+-------+-----------------+ -| Staging | 1.1.0-beta1 | Beta | Foo | Foo integration | -+-------------+-------------+-------------------+-------+-----------------+ -| Production | 1.0.0 | GA | Foo | Foo integration | -+-------------+-------------+-------------------+-------+-----------------+ diff --git a/cmd/testdata/status-version-one-stage b/cmd/testdata/status-version-one-stage index f4e3bbe4e8..3c951c7c2e 100644 --- a/cmd/testdata/status-version-one-stage +++ b/cmd/testdata/status-version-one-stage @@ -3,9 +3,5 @@ Package Versions: +-------------+---------+---------+-------+-----------------+ | ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | +-------------+---------+---------+-------+-----------------+ -| Snapshot | - | - | - | - | -+-------------+---------+---------+-------+-----------------+ -| Staging | - | - | - | - | -+-------------+---------+---------+-------+-----------------+ | Production | 1.0.0 | GA | Foo | Foo integration | +-------------+---------+---------+-------+-----------------+ diff --git a/internal/packages/status/status.go b/internal/packages/status/status.go index 9f50a8e793..b75b18ee66 100644 --- a/internal/packages/status/status.go +++ b/internal/packages/status/status.go @@ -20,8 +20,6 @@ type PackageStatus struct { PendingChanges *changelog.Revision Local *packages.PackageManifest Production []packages.PackageManifest - Staging []packages.PackageManifest - Snapshot []packages.PackageManifest } // LocalPackage returns the status of a given package including local development information @@ -61,22 +59,12 @@ func LocalPackage(packageRootPath string, options registry.SearchOptions) (*Pack // RemotePackage returns the status of a given package func RemotePackage(packageName string, options registry.SearchOptions) (*PackageStatus, error) { - snapshotManifests, err := registry.Snapshot.Revisions(packageName, options) - if err != nil { - return nil, errors.Wrap(err, "retrieving snapshot deployment failed") - } - stagingManifests, err := registry.Staging.Revisions(packageName, options) - if err != nil { - return nil, errors.Wrap(err, "retrieving staging deployment failed") - } productionManifests, err := registry.Production.Revisions(packageName, options) if err != nil { return nil, errors.Wrap(err, "retrieving production deployment failed") } return &PackageStatus{ Name: packageName, - Snapshot: snapshotManifests, - Staging: stagingManifests, Production: productionManifests, }, nil } diff --git a/internal/registry/client.go b/internal/registry/client.go index 29d64da309..09d69b7c2b 100644 --- a/internal/registry/client.go +++ b/internal/registry/client.go @@ -14,17 +14,11 @@ import ( const ( productionURL = "https://epr.elastic.co" - stagingURL = "https://epr-staging.elastic.co" - snapshotURL = "https://epr-snapshot.elastic.co" ) var ( // Production is a pre-configured production client Production = NewClient(productionURL) - // Staging is a pre-configured staging client - Staging = NewClient(stagingURL) - // Snapshot is a pre-configured snapshot client - Snapshot = NewClient(snapshotURL) ) // Client is responsible for exporting dashboards from Kibana. From de92be8fdb85bb44b51d8f0d6712273765f0fee7 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 27 Oct 2022 11:52:25 +0200 Subject: [PATCH 3/8] Add test for local pending changes --- cmd/status.go | 2 +- cmd/status_test.go | 24 ++++++++++++++++++++++++ cmd/testdata/status-pending-changes | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 cmd/testdata/status-pending-changes diff --git a/cmd/status.go b/cmd/status.go index d5ed106f00..e38cba4695 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -111,7 +111,7 @@ func print(p *status.PackageStatus, w io.Writer) error { for _, change := range p.PendingChanges.Changes { changelogTable = append(changelogTable, formatChangelogEntry(change)) } - table := tablewriter.NewWriter(os.Stdout) + table := tablewriter.NewWriter(w) table.SetHeader([]string{"Type", "Description", "Link"}) table.SetHeaderColor( twColor(tablewriter.Colors{tablewriter.Bold}), diff --git a/cmd/status_test.go b/cmd/status_test.go index c4f35558b2..fb8728baf7 100644 --- a/cmd/status_test.go +++ b/cmd/status_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/require" "github.com/elastic/elastic-package/internal/packages" + "github.com/elastic/elastic-package/internal/packages/changelog" "github.com/elastic/elastic-package/internal/packages/status" ) @@ -33,6 +34,17 @@ func fooPackage(version string) packages.PackageManifest { func TestStatusFormatAndPrint(t *testing.T) { localPackage := fooPackage("2.0.0-rc1") + localPendingChanges := changelog.Revision{ + Version: "2.0.0-rc2", + Changes: []changelog.Entry{ + changelog.Entry{ + Description: "New feature", + Type: "enhancement", + Link: "http:github.com/org/repo/pull/2", + }, + }, + } + cases := []struct { title string pkgStatus *status.PackageStatus @@ -102,6 +114,18 @@ func TestStatusFormatAndPrint(t *testing.T) { }, expected: "./testdata/status-local-version-stage", }, + { + title: "local pending changes", + pkgStatus: &status.PackageStatus{ + Name: "foo", + Local: &localPackage, + PendingChanges: &localPendingChanges, + Production: []packages.PackageManifest{ + fooPackage("1.0.0"), + }, + }, + expected: "./testdata/status-pending-changes", + }, } for _, c := range cases { diff --git a/cmd/testdata/status-pending-changes b/cmd/testdata/status-pending-changes new file mode 100644 index 0000000000..84fddf56f0 --- /dev/null +++ b/cmd/testdata/status-pending-changes @@ -0,0 +1,17 @@ +Package: foo +Owner: team +Next Version: 2.0.0-rc2 +Pending Changes: ++-------------+-------------+---------------------------------+ +| TYPE | DESCRIPTION | LINK | ++-------------+-------------+---------------------------------+ +| enhancement | New feature | http:github.com/org/repo/pull/2 | ++-------------+-------------+---------------------------------+ +Package Versions: ++-------------+-----------+-------------------+-------+-----------------+ +| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | ++-------------+-----------+-------------------+-------+-----------------+ +| Local | 2.0.0-rc1 | Release Candidate | Foo | Foo integration | ++-------------+-----------+-------------------+-------+-----------------+ +| Production | 1.0.0 | GA | Foo | Foo integration | ++-------------+-----------+-------------------+-------+-----------------+ From dd944ae375e80ce2a4bf4303792fc6323e3f0039 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 27 Oct 2022 11:58:08 +0200 Subject: [PATCH 4/8] Remove promote and publish flags --- internal/cobraext/flags.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/internal/cobraext/flags.go b/internal/cobraext/flags.go index 43cbfdb83b..15a9e24133 100644 --- a/internal/cobraext/flags.go +++ b/internal/cobraext/flags.go @@ -71,9 +71,6 @@ const ( DataStreamsFlagName = "data-streams" DataStreamsFlagDescription = "comma-separated data streams to test" - DirectionFlagName = "direction" - DirectionFlagDescription = "promotion direction" - DeferCleanupFlagName = "defer-cleanup" DeferCleanupFlagDescription = "defer test cleanup for debugging purposes" @@ -86,9 +83,6 @@ const ( FailFastFlagName = "fail-fast" FailFastFlagDescription = "fail immediately if any file requires updates (do not overwrite)" - ForkFlagName = "fork" - ForkFlagDescription = "use fork mode (set to \"false\" if user can't fork the storage repository)" - GenerateTestResultFlagName = "generate" GenerateTestResultFlagDescription = "generate test result file" @@ -101,12 +95,6 @@ const ( ProfileFormatFlagName = "format" ProfileFormatFlagDescription = "format of the profiles list (table | json)" - NewestOnlyFlagName = "newest-only" - NewestOnlyFlagDescription = "promote newest packages and remove old ones" - - PromotedPackagesFlagName = "packages" - PromotedPackagesFlagDescription = "packages to be promoted (comma-separated values: apache-1.2.3,nginx-5.6.7)" - ReportFormatFlagName = "report-format" ReportFormatFlagDescription = "format of test report" @@ -123,9 +111,6 @@ const ( SignPackageFlagName = "sign" SignPackageFlagDescription = "sign package" - SkipPullRequestFlagName = "skip-pull-request" - SkipPullRequestFlagDescription = "skip opening a new pull request" - TLSSkipVerifyFlagName = "tls-skip-verify" TLSSkipVerifyFlagDescription = "skip TLS verify" From 7bb0f43f9ecc708462c266a55e0a7e67e52ff13b Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 27 Oct 2022 12:01:39 +0200 Subject: [PATCH 5/8] Update Readme Readme updated without the promote and publish commands. Removed also the reference to Github Authorization tokens since there is no need to communicate from elastic-package to Github to create Pull Requests. --- README.md | 36 +----------------------------------- tools/readme/readme.md.tmpl | 18 +----------------- 2 files changed, 2 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 7e17dd336c..447d52b124 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ WARNING: This is a generated file. Do NOT edit it manually. To regenerate this f # elastic-package `elastic-package` is a command line tool, written in Go, used for developing Elastic packages. It can help you lint, format, -test, build, and promote your packages. Learn about each of these and other features in [_Commands_](#commands) below. +test and build your packages. Learn about each of these and other features in [_Commands_](#commands) below. Currently, `elastic-package` only supports packages of type [Elastic Integrations](https://github.com/elastic/integrations). @@ -196,24 +196,6 @@ Individual user profiles appear in ~/.elastic-package/stack, and contain all the Once a new profile is created, it can be specified with the -p flag, or the ELASTIC_PACKAGE_PROFILE environment variable. User profiles are not overwritten on upgrade of elastic-stack, and can be freely modified to allow for different stack configs. -### `elastic-package promote` - -_Context: global_ - -Use this command to move packages between the snapshot, staging, and production stages of the package registry. - -This command is intended primarily for use by administrators. - -It allows for selecting packages for promotion and opens new pull requests to review changes. Please be aware that the tool checks out an in-memory Git repository and switches over branches (snapshot, staging and production), so it may take longer to promote a larger number of packages. - -### `elastic-package publish` - -_Context: package_ - -Use this command to publish a new package revision. - -The command checks if the package hasn't been already published (whether it's present in snapshot/staging/production branch or open as pull request). If the package revision hasn't been published, it will open a new pull request. - ### `elastic-package service` _Context: package_ @@ -284,22 +266,6 @@ Use this command to print the version of elastic-package that you have installed -### GitHub authorization - -The `promote` and `publish` commands require access to the GitHub API to open pull requests or check authorized account data. -The tool uses the GitHub token to authorize user's call to API. The token can be stored in the `~/.elastic/github.token` -file or passed via the `GITHUB_TOKEN` environment variable. - -Here are the instructions on how to create your own personal access token (PAT): -https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token - -Make sure you have enabled the following scopes: -* `public_repo` — to open pull requests on GitHub repositories. -* `read:user` and `user:email` — to read your user profile information from GitHub in order to populate pull requests appropriately. - -After creating or modifying your personal access token, authorize the token for -use of the Elastic organization: https://docs.github.com/en/github/authenticating-to-github/authenticating-with-saml-single-sign-on/authorizing-a-personal-access-token-for-use-with-saml-single-sign-on - ## Development Even though the project is "go-gettable", there is the `Makefile` present, which can be used to build, format or vendor diff --git a/tools/readme/readme.md.tmpl b/tools/readme/readme.md.tmpl index 3e83a39f1b..b35977a686 100644 --- a/tools/readme/readme.md.tmpl +++ b/tools/readme/readme.md.tmpl @@ -5,7 +5,7 @@ WARNING: This is a generated file. Do NOT edit it manually. To regenerate this f # elastic-package `elastic-package` is a command line tool, written in Go, used for developing Elastic packages. It can help you lint, format, -test, build, and promote your packages. Learn about each of these and other features in [_Commands_](#commands) below. +test and build your packages. Learn about each of these and other features in [_Commands_](#commands) below. Currently, `elastic-package` only supports packages of type [Elastic Integrations](https://github.com/elastic/integrations). @@ -90,22 +90,6 @@ Run `elastic-package completion` and follow the instruction for your shell. {{ .Cmds }} -### GitHub authorization - -The `promote` and `publish` commands require access to the GitHub API to open pull requests or check authorized account data. -The tool uses the GitHub token to authorize user's call to API. The token can be stored in the `~/.elastic/github.token` -file or passed via the `GITHUB_TOKEN` environment variable. - -Here are the instructions on how to create your own personal access token (PAT): -https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token - -Make sure you have enabled the following scopes: -* `public_repo` — to open pull requests on GitHub repositories. -* `read:user` and `user:email` — to read your user profile information from GitHub in order to populate pull requests appropriately. - -After creating or modifying your personal access token, authorize the token for -use of the Elastic organization: https://docs.github.com/en/github/authenticating-to-github/authenticating-with-saml-single-sign-on/authorizing-a-personal-access-token-for-use-with-saml-single-sign-on - ## Development Even though the project is "go-gettable", there is the `Makefile` present, which can be used to build, format or vendor From 0b53208d25aceaa6dffa8fcb303fedd564d8855e Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Thu, 27 Oct 2022 12:20:50 +0200 Subject: [PATCH 6/8] Remove comment from CI job --- .ci/package-storage-publish.groovy | 1 - 1 file changed, 1 deletion(-) diff --git a/.ci/package-storage-publish.groovy b/.ci/package-storage-publish.groovy index b8b7c09f70..f6e70ca59d 100644 --- a/.ci/package-storage-publish.groovy +++ b/.ci/package-storage-publish.groovy @@ -54,7 +54,6 @@ pipeline { withGoEnv() { dir("${BASE_DIR}") { sh(label: 'Install elastic-package',script: "make install") - // sh(label: 'Install elastic-package', script: 'go build github.com/elastic/elastic-package') dir("test/packages/package-storage/package_storage_candidate") { sh(label: 'Build package', script: "elastic-package build -v --zip") } From c5f9f02d6ad280390c3b7997be5f666510fd1749 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Mon, 21 Nov 2022 12:44:52 +0100 Subject: [PATCH 7/8] Add promote and publish commands with deprecated message Added promote and publish commands again with all the flags so commands will not fail by unknown command or unknown flag. Instead these commands will show a deprecated message --- cmd/promote.go | 39 +++++++++++++++++++++++++++++++++++++ cmd/publish.go | 40 ++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 ++ internal/cobraext/flags.go | 17 ++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 cmd/promote.go create mode 100644 cmd/publish.go diff --git a/cmd/promote.go b/cmd/promote.go new file mode 100644 index 0000000000..4ba1793a84 --- /dev/null +++ b/cmd/promote.go @@ -0,0 +1,39 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/elastic/elastic-package/internal/cobraext" +) + +const promoteLongDescription = `Use this command to move packages between the snapshot, staging, and production stages of the package registry. + +This command is intended primarily for use by administrators. + +It allows for selecting packages for promotion and opens new pull requests to review changes. Please be aware that the tool checks out an in-memory Git repository and switches over branches (snapshot, staging and production), so it may take longer to promote a larger number of packages.` + +func setupPromoteCommand() *cobraext.Command { + cmd := &cobra.Command{ + Use: "promote", + Short: "Promote packages", + Long: promoteLongDescription, + RunE: promoteCommandAction, + SilenceUsage: true, + } + + cmd.Flags().StringP(cobraext.DirectionFlagName, "d", "", cobraext.DirectionFlagDescription) + cmd.Flags().BoolP(cobraext.NewestOnlyFlagName, "n", false, cobraext.NewestOnlyFlagDescription) + cmd.Flags().StringSliceP(cobraext.PromotedPackagesFlagName, "p", nil, cobraext.PromotedPackagesFlagDescription) + + return cobraext.NewCommand(cmd, cobraext.ContextGlobal) +} + +func promoteCommandAction(cmd *cobra.Command, _ []string) error { + cmd.Println("Promote packages") + cmd.Println("DEPRECATED: Packages stored in the Package Storage v2 do not require to be promoted. README: https://github.com/elastic/elastic-package/blob/main/docs/howto/use_package_storage_v2.md") + return nil +} diff --git a/cmd/publish.go b/cmd/publish.go new file mode 100644 index 0000000000..e1dd48cb3d --- /dev/null +++ b/cmd/publish.go @@ -0,0 +1,40 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/elastic/elastic-package/internal/cobraext" +) + +const publishLongDescription = `Use this command to publish a new package revision. + +The command checks if the package hasn't been already published (whether it's present in snapshot/staging/production branch or open as pull request). If the package revision hasn't been published, it will open a new pull request.` + +func setupPublishCommand() *cobraext.Command { + cmd := &cobra.Command{ + Use: "publish", + Short: "Publish the package to the Package Registry", + Long: publishLongDescription, + RunE: publishCommandAction, + } + + // Fork flag can be a workaround for users that don't own forks of the Package Storage. + cmd.Flags().BoolP(cobraext.ForkFlagName, "f", true, cobraext.ForkFlagDescription) + + // SkipPullRequest flag can used to verify if the "publish" command works properly (finds correct revisions), + // for which the operator doesn't want to immediately close just opened PRs (standard dry-run). + cmd.Flags().BoolP(cobraext.SkipPullRequestFlagName, "s", false, cobraext.SkipPullRequestFlagDescription) + + return cobraext.NewCommand(cmd, cobraext.ContextPackage) +} + +func publishCommandAction(cmd *cobra.Command, args []string) error { + cmd.Println("Publish the package") + cmd.Println("DEPRECATED: Package candidates to the Package Storage v2 are published using Jenkins jobs. README: https://github.com/elastic/elastic-package/blob/main/docs/howto/use_package_storage_v2.md") + + return nil +} diff --git a/cmd/root.go b/cmd/root.go index 479b9fe67f..58c181d330 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,7 +26,9 @@ var commands = []*cobraext.Command{ setupFormatCommand(), setupInstallCommand(), setupLintCommand(), + setupPromoteCommand(), setupProfilesCommand(), + setupPublishCommand(), setupReportsCommand(), setupServiceCommand(), setupStackCommand(), diff --git a/internal/cobraext/flags.go b/internal/cobraext/flags.go index 1ffc11b252..6d41722dd0 100644 --- a/internal/cobraext/flags.go +++ b/internal/cobraext/flags.go @@ -146,4 +146,21 @@ const ( VariantFlagName = "variant" VariantFlagDescription = "service variant" + + // To be removed promote commands flags + DirectionFlagName = "direction" + DirectionFlagDescription = "promotion direction" + + NewestOnlyFlagName = "newest-only" + NewestOnlyFlagDescription = "promote newest packages and remove old ones" + + PromotedPackagesFlagName = "packages" + PromotedPackagesFlagDescription = "packages to be promoted (comma-separated values: apache-1.2.3,nginx-5.6.7)" + + // To be removed publish commands flags + ForkFlagName = "fork" + ForkFlagDescription = "use fork mode (set to \"false\" if user can't fork the storage repository)" + + SkipPullRequestFlagName = "skip-pull-request" + SkipPullRequestFlagDescription = "skip opening a new pull request" ) From 1b099dad61fb4fe4f4482589a56f5c1121c481c7 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Fri, 2 Dec 2022 14:37:33 +0100 Subject: [PATCH 8/8] Update readme and add DEPRECATED in promote and publish commands --- README.md | 18 ++++++++++++++++++ cmd/promote.go | 2 +- cmd/publish.go | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 023f0312bb..51721b441d 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,24 @@ Individual user profiles appear in ~/.elastic-package/stack, and contain all the Once a new profile is created, it can be specified with the -p flag, or the ELASTIC_PACKAGE_PROFILE environment variable. User profiles are not overwritten on upgrade of elastic-stack, and can be freely modified to allow for different stack configs. +### `elastic-package promote` + +_Context: global_ + +[DEPRECATED] Use this command to move packages between the snapshot, staging, and production stages of the package registry. + +This command is intended primarily for use by administrators. + +It allows for selecting packages for promotion and opens new pull requests to review changes. Please be aware that the tool checks out an in-memory Git repository and switches over branches (snapshot, staging and production), so it may take longer to promote a larger number of packages. + +### `elastic-package publish` + +_Context: package_ + +[DEPRECATED] Use this command to publish a new package revision. + +The command checks if the package hasn't been already published (whether it's present in snapshot/staging/production branch or open as pull request). If the package revision hasn't been published, it will open a new pull request. + ### `elastic-package report` _Context: package_ diff --git a/cmd/promote.go b/cmd/promote.go index 4ba1793a84..1108230e05 100644 --- a/cmd/promote.go +++ b/cmd/promote.go @@ -10,7 +10,7 @@ import ( "github.com/elastic/elastic-package/internal/cobraext" ) -const promoteLongDescription = `Use this command to move packages between the snapshot, staging, and production stages of the package registry. +const promoteLongDescription = `[DEPRECATED] Use this command to move packages between the snapshot, staging, and production stages of the package registry. This command is intended primarily for use by administrators. diff --git a/cmd/publish.go b/cmd/publish.go index e1dd48cb3d..ab5e4a0662 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -10,7 +10,7 @@ import ( "github.com/elastic/elastic-package/internal/cobraext" ) -const publishLongDescription = `Use this command to publish a new package revision. +const publishLongDescription = `[DEPRECATED] Use this command to publish a new package revision. The command checks if the package hasn't been already published (whether it's present in snapshot/staging/production branch or open as pull request). If the package revision hasn't been published, it will open a new pull request.`