blob: fda9ceacea5a86609bd8037ff37e0935bf11747e [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/maruel/subcommands"
"go.chromium.org/luci/auth"
gerritpb "go.chromium.org/luci/common/proto/gerrit"
"go.fuchsia.dev/infra/gerrit"
"go.fuchsia.dev/infra/gitiles"
)
func cmdCreateCL(authOpts auth.Options) *subcommands.Command {
return &subcommands.Command{
UsageLine: "create-cl -host <gerrit-host> -project <gerrit-project> -subject <cl-subject> -file-edit <filepath1>:<contents1>, ... -json-output <json-output> [-ref <ref>]",
ShortDesc: "Create a CL with file edits.",
LongDesc: "Create a CL with file edits.",
CommandRun: func() subcommands.CommandRun {
c := &createCLRun{}
c.Init(authOpts)
return c
},
}
}
type createCLRun struct {
commonFlags
edits fileEdits
subject string
jsonOutput string
ref string
}
func (c *createCLRun) Init(defaultAuthOpts auth.Options) {
c.commonFlags.Init(defaultAuthOpts)
c.Flags.StringVar(&c.subject, "subject", "", "CL subject.")
c.Flags.Var(&c.edits, "file-edit", "filepath:content pairs. Repeatable.")
c.Flags.StringVar(&c.jsonOutput, "json-output", "", "Path to write gerrit.ChangeInfo to.")
c.Flags.StringVar(&c.ref, "ref", "refs/heads/main", "Ref to author CL against.")
}
func (c *createCLRun) Parse(a subcommands.Application, args []string) error {
if err := c.commonFlags.Parse(); err != nil {
return err
}
if c.subject == "" {
return errors.New("-subject is required")
}
if len(c.edits) == 0 {
return errors.New("at least one -file-edit is required")
}
if c.jsonOutput == "" {
return errors.New("-json-output is required")
}
return nil
}
// fileEdit represents a file to edit in a repository.
type fileEdit struct {
filepath string
contents string
}
// String returns a string representation of the file edit.
func (e *fileEdit) String() string {
return fmt.Sprintf("%s:%s", e.filepath, e.contents)
}
// newFileEdit returns a fileEdit for a filepath:contents string.
func newFileEdit(editStr string) (*fileEdit, error) {
fp, c, found := strings.Cut(editStr, ":")
if !found {
return nil, fmt.Errorf("%q is not of format filepath:contents", editStr)
}
return &fileEdit{filepath: fp, contents: c}, nil
}
// fileEdits is a flag.Getter implementation representing a []*fileEdit.
type fileEdits []*fileEdit
// String returns a comma-separated string representation of the flag file edits.
func (f fileEdits) String() string {
strs := make([]string, len(f))
for i, edit := range f {
strs[i] = edit.String()
}
return strings.Join(strs, ", ")
}
// Set records seeing a flag value.
func (f *fileEdits) Set(val string) error {
fe, err := newFileEdit(val)
if err != nil {
return err
}
*f = append(*f, fe)
return nil
}
// Get retrieves the flag value.
func (f fileEdits) Get() any {
return []*fileEdit(f)
}
func (c *createCLRun) main(a subcommands.Application) error {
ctx := context.Background()
authClient, err := newAuthClient(ctx, c.parsedAuthOpts)
if err != nil {
return err
}
gitilesClient, err := gitiles.NewClient(strings.Replace(c.gerritHost, "-review", "", 1), c.gerritProject, authClient)
if err != nil {
return err
}
gerritClient, err := gerrit.NewClient(c.gerritHost, c.gerritProject, authClient)
if err != nil {
return err
}
commit, err := gitilesClient.LatestCommit(ctx, c.ref)
if err != nil {
return err
}
changeInfo, err := gerritClient.CreateChange(ctx, c.subject, commit, c.ref)
if err != nil {
return err
}
for _, edit := range c.edits {
if err := gerritClient.EditFile(ctx, changeInfo.Number, edit.filepath, edit.contents); err != nil {
return err
}
}
if err := gerritClient.PublishEdits(ctx, changeInfo.Number); err != nil {
return err
}
// Grab post-edit ChangeInfo prior to dumping to JSON.
changeInfo, err = gerritClient.GetChange(ctx, changeInfo.Number, gerritpb.QueryOption_ALL_REVISIONS)
if err != nil {
return err
}
out := os.Stdout
if c.jsonOutput != "-" {
out, err = os.Create(c.jsonOutput)
if err != nil {
return err
}
defer out.Close()
}
if err := json.NewEncoder(out).Encode(changeInfo); err != nil {
return fmt.Errorf("failed to encode: %w", err)
}
return nil
}
func (c *createCLRun) Run(a subcommands.Application, args []string, env subcommands.Env) int {
if err := c.Parse(a, args); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
return 1
}
if err := c.main(a); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
return 1
}
return 0
}