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

fix(create-pr-new-pkg): handle the permission error #870

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cli/create_pr_new_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ This tool does the following things.
}

func (runner *Runner) createPRNewPkgAction(c *cli.Context) error {
return newpkg.CreatePRNewPkgs(c.Context, c.Args().Slice()...) //nolint:wrapcheck
return newpkg.CreatePRNewPkgs(c.Context, runner.LogE, c.Args().Slice()...) //nolint:wrapcheck
}
30 changes: 27 additions & 3 deletions pkg/create-pr-new-pkg/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package newpkg

import (
"bytes"
"context"
_ "embed"
"errors"
Expand All @@ -12,13 +13,14 @@ import (
"strings"

"github.com/aquaproj/registry-tool/pkg/initcmd"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

//go:embed pr_template.md
var bodyTemplate []byte

func CreatePRNewPkgs(ctx context.Context, pkgNames ...string) error {
func CreatePRNewPkgs(ctx context.Context, logE *logrus.Entry, pkgNames ...string) error {
if len(pkgNames) == 0 {
return errors.New(`usage: $ aqua-registry create-pr-new-pkg <pkgname>...
e.g. $ aqua-registry create-pr-new-pkg cli/cli`)
Expand Down Expand Up @@ -56,8 +58,17 @@ e.g. $ aqua-registry create-pr-new-pkg cli/cli`)
if err := initcmd.Init(ctx); err != nil {
return err //nolint:wrapcheck
}
if err := command(ctx, "git", "push", "origin", branch); err != nil {
return err
stderr := &bytes.Buffer{}
if err := commandStderr(ctx, io.MultiWriter(os.Stderr, stderr), "git", "push", "origin", branch); err != nil {
if strings.Contains(stderr.String(), "returned error: 403") {
logE.WithFields(logrus.Fields{
"doc": "https://aquaproj.github.io/docs/products/aqua-registry/contributing#cmdx-new-fails-to-push-a-commit-to-the-origin",
}).Warn(`you don't have the permission to push commits to the origin.
Please fork aquaproj/aqua-registry and fix the origin url to your fork repository.
For details, please see the document`)
} else {
return err
}
}
if err := command(ctx, "aqua", "-c", "aqua/dev.yaml", "exec", "--", "gh", "pr", "create", "-w", "-t", "feat: add "+pkgName, "-b", body); err != nil {
return err
Expand Down Expand Up @@ -103,3 +114,16 @@ func command(ctx context.Context, cmdName string, args ...string) error {
}
return nil
}

func commandStderr(ctx context.Context, stderr io.Writer, cmdName string, args ...string) error {
s := cmdName + " " + strings.Join(args, " ")
fmt.Fprintln(os.Stderr, "+ "+s)
cmd := exec.CommandContext(ctx, cmdName, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("execute a command: %s: %w", s, err)
}
return nil
}
Loading