Merge branch 'main' into edit-issue

This commit is contained in:
6543 2022-09-27 17:48:26 +02:00
commit 40b68a02a8
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE
10 changed files with 170 additions and 39 deletions

2
.gitignore vendored
View file

@ -7,3 +7,5 @@ dist/
.vscode/
vendor/
coverage.out

View file

@ -33,7 +33,7 @@ var RemoteFlag = cli.StringFlag{
var OutputFlag = cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output format. (csv, simple, table, tsv, yaml)",
Usage: "Output format. (simple, table, csv, tsv, yaml, json)",
}
// PaginationPageFlag provides flag for pagination options

View file

@ -30,6 +30,12 @@ var CmdPullsCreate = cli.Command{
Aliases: []string{"b"},
Usage: "Branch name of the PR target (default is repos default branch)",
},
&cli.BoolFlag{
Name: "allow-maintainer-edits",
Aliases: []string{"edits"},
Usage: "Enable maintainers to push to the base branch of created pull",
Value: true,
},
}, flags.IssuePRCreateFlags...),
}
@ -51,6 +57,7 @@ func runPullsCreate(cmd *cli.Context) error {
ctx,
ctx.String("base"),
ctx.String("head"),
ctx.Bool("allow-maintainer-edits"),
opts,
)
}

6
go.mod
View file

@ -4,7 +4,7 @@ go 1.18
require (
code.gitea.io/gitea-vet v0.2.1
code.gitea.io/sdk/gitea v0.15.1-0.20220831004139-a0127ed0e7fe
code.gitea.io/sdk/gitea v0.15.1-0.20220927025037-8f846bdb9bbe
gitea.com/noerw/unidiff-comments v0.0.0-20220822113322-50f4daa0e35c
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/adrg/xdg v0.4.0
@ -17,7 +17,7 @@ require (
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.16.3
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be
gopkg.in/yaml.v2 v2.4.0
)
@ -59,7 +59,7 @@ require (
github.com/yuin/goldmark v1.4.14 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/sys v0.0.0-20220913153101-76c7481b5158 // indirect
golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.12 // indirect

12
go.sum
View file

@ -1,7 +1,7 @@
code.gitea.io/gitea-vet v0.2.1 h1:b30by7+3SkmiftK0RjuXqFvZg2q4p68uoPGuxhzBN0s=
code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE=
code.gitea.io/sdk/gitea v0.15.1-0.20220831004139-a0127ed0e7fe h1:PeLyxnUZE85QuJtBZ4P8qCQcgWG5Ked67mlNgr0WkCQ=
code.gitea.io/sdk/gitea v0.15.1-0.20220831004139-a0127ed0e7fe/go.mod h1:aRmrQC3CAHdJAU1LQt0C9zqzqI8tUB/5oQtNE746aYE=
code.gitea.io/sdk/gitea v0.15.1-0.20220927025037-8f846bdb9bbe h1:3mI5Ea4NmdFIu4yhENz5YT3FsVGntP47ErcZlb3nPPE=
code.gitea.io/sdk/gitea v0.15.1-0.20220927025037-8f846bdb9bbe/go.mod h1:aRmrQC3CAHdJAU1LQt0C9zqzqI8tUB/5oQtNE746aYE=
gitea.com/noerw/unidiff-comments v0.0.0-20220822113322-50f4daa0e35c h1:8fTkq2UaVkLHZCF+iB4wTxINmVAToe2geZGayk9LMbA=
gitea.com/noerw/unidiff-comments v0.0.0-20220822113322-50f4daa0e35c/go.mod h1:Fc8iyPm4NINRWujeIk2bTfcbGc4ZYY29/oMAAGcr4qI=
github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw=
@ -182,8 +182,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be h1:fmw3UbQh+nxngCAHrDCCztao/kbYFnWjoqop8dHx05A=
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@ -221,8 +221,8 @@ golang.org/x/sys v0.0.0-20220315194320-039c03cc5b86/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220913153101-76c7481b5158 h1:XQphkCZeKYaMRSo28HqvvNYuLOoM5CIOOvTZfthvTgI=
golang.org/x/sys v0.0.0-20220913153101-76c7481b5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25 h1:nwzwVf0l2Y/lkov/+IYgMMbFyI+QypZDds9RxlSmsFQ=
golang.org/x/sys v0.0.0-20220926163933-8cfa568d3c25/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=

View file

@ -14,7 +14,10 @@ import (
// CreatePull interactively creates a PR
func CreatePull(ctx *context.TeaContext) (err error) {
var base, head string
var (
base, head string
allowMaintainerEdits bool
)
// owner, repo
if ctx.Owner, ctx.Repo, err = promptRepoSlug(ctx.Owner, ctx.Repo); err != nil {
@ -49,6 +52,11 @@ func CreatePull(ctx *context.TeaContext) (err error) {
return err
}
promptC := &survey.Confirm{Message: "Allow Maintainers to push to the base branch", Default: true}
if err := survey.AskOne(promptC, &allowMaintainerEdits); err != nil {
return err
}
head = task.GetHeadSpec(headOwner, headBranch, ctx.Owner)
opts := gitea.CreateIssueOption{Title: task.GetDefaultPRTitle(head)}
@ -60,5 +68,6 @@ func CreatePull(ctx *context.TeaContext) (err error) {
ctx,
base,
head,
allowMaintainerEdits,
&opts)
}

View file

@ -64,6 +64,10 @@ func PullDetails(pr *gitea.PullRequest, reviews []*gitea.PullReview, ciStatus *g
}
}
if pr.AllowMaintainerEdit {
out += "- Maintainers are allowed to edit\n"
}
outputMarkdown(out, getRepoURL(pr.HTMLURL))
}

View file

@ -6,7 +6,9 @@ package print
import (
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
@ -72,25 +74,39 @@ func (t table) Less(i, j int) bool {
}
func (t *table) print(output string) {
t.fprint(os.Stdout, output)
}
func (t *table) fprint(f io.Writer, output string) {
switch output {
case "", "table":
outputtable(t.headers, t.values)
outputTable(f, t.headers, t.values)
case "csv":
outputdsv(t.headers, t.values, ",")
outputDsv(f, t.headers, t.values, ",")
case "simple":
outputsimple(t.headers, t.values)
outputSimple(f, t.headers, t.values)
case "tsv":
outputdsv(t.headers, t.values, "\t")
outputDsv(f, t.headers, t.values, "\t")
case "yml", "yaml":
outputyaml(t.headers, t.values)
outputYaml(f, t.headers, t.values)
case "json":
outputJSON(f, t.headers, t.values)
default:
fmt.Printf("unknown output type '" + output + "', available types are:\n- csv: comma-separated values\n- simple: space-separated values\n- table: auto-aligned table format (default)\n- tsv: tab-separated values\n- yaml: YAML format\n")
fmt.Fprintf(f, `"unknown output type '%s', available types are:
- csv: comma-separated values
- simple: space-separated values
- table: auto-aligned table format (default)
- tsv: tab-separated values
- yaml: YAML format
- json: JSON format
`, output)
os.Exit(1)
}
}
// outputtable prints structured data as table
func outputtable(headers []string, values [][]string) {
table := tablewriter.NewWriter(os.Stdout)
// outputTable prints structured data as table
func outputTable(f io.Writer, headers []string, values [][]string) {
table := tablewriter.NewWriter(f)
if len(headers) > 0 {
table.SetHeader(headers)
}
@ -100,47 +116,89 @@ func outputtable(headers []string, values [][]string) {
table.Render()
}
// outputsimple prints structured data as space delimited value
func outputsimple(headers []string, values [][]string) {
// outputSimple prints structured data as space delimited value
func outputSimple(f io.Writer, headers []string, values [][]string) {
for _, value := range values {
fmt.Printf(strings.Join(value, " "))
fmt.Printf("\n")
fmt.Fprint(f, strings.Join(value, " "))
fmt.Fprintf(f, "\n")
}
}
// outputdsv prints structured data as delimiter separated value format
func outputdsv(headers []string, values [][]string, delimiterOpt ...string) {
// outputDsv prints structured data as delimiter separated value format
func outputDsv(f io.Writer, headers []string, values [][]string, delimiterOpt ...string) {
delimiter := ","
if len(delimiterOpt) > 0 {
delimiter = delimiterOpt[0]
}
fmt.Println("\"" + strings.Join(headers, "\""+delimiter+"\"") + "\"")
fmt.Fprintln(f, "\""+strings.Join(headers, "\""+delimiter+"\"")+"\"")
for _, value := range values {
fmt.Printf("\"")
fmt.Printf(strings.Join(value, "\""+delimiter+"\""))
fmt.Printf("\"")
fmt.Printf("\n")
fmt.Fprintf(f, "\"")
fmt.Fprint(f, strings.Join(value, "\""+delimiter+"\""))
fmt.Fprintf(f, "\"")
fmt.Fprintf(f, "\n")
}
}
// outputyaml prints structured data as yaml
func outputyaml(headers []string, values [][]string) {
// outputYaml prints structured data as yaml
func outputYaml(f io.Writer, headers []string, values [][]string) {
for _, value := range values {
fmt.Println("-")
fmt.Fprintln(f, "-")
for j, val := range value {
intVal, _ := strconv.Atoi(val)
if strconv.Itoa(intVal) == val {
fmt.Printf(" %s: %s\n", headers[j], val)
fmt.Fprintf(f, " %s: %s\n", headers[j], val)
} else {
fmt.Printf(" %s: '%s'\n", headers[j], val)
fmt.Fprintf(f, " %s: '%s'\n", headers[j], val)
}
}
}
}
var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)
func toSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
// outputJSON prints structured data as json
func outputJSON(f io.Writer, headers []string, values [][]string) {
fmt.Fprintln(f, "[")
itemCount := len(values)
headersCount := len(headers)
const space = " "
for i, value := range values {
fmt.Fprintf(f, "%s{\n", space)
for j, val := range value {
intVal, _ := strconv.Atoi(val)
if strconv.Itoa(intVal) == val {
fmt.Fprintf(f, "%s%s\"%s\": %s", space, space, toSnakeCase(headers[j]), val)
} else {
fmt.Fprintf(f, "%s%s\"%s\": \"%s\"", space, space, toSnakeCase(headers[j]), val)
}
if j != headersCount-1 {
fmt.Fprintln(f, ",")
} else {
fmt.Fprintln(f)
}
}
if i != itemCount-1 {
fmt.Fprintf(f, "%s},\n", space)
} else {
fmt.Fprintf(f, "%s}\n", space)
}
}
fmt.Fprintln(f, "]")
}
func isMachineReadable(outputFormat string) bool {
switch outputFormat {
case "yml", "yaml", "csv", "tsv":
case "yml", "yaml", "csv", "tsv", "json":
return true
}
return false

View file

@ -0,0 +1,40 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package print
import (
"bytes"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestToSnakeCase(t *testing.T) {
assert.EqualValues(t, "some_test_var_at2d", toSnakeCase("SomeTestVarAt2d"))
}
func TestPrint(t *testing.T) {
tData := &table{
headers: []string{"A", "B"},
values: [][]string{
{"new a", "some bbbb"},
{"AAAAA", "b2"},
},
}
buf := &bytes.Buffer{}
tData.fprint(buf, "json")
result := []struct {
A string
B string
}{}
assert.NoError(t, json.NewDecoder(buf).Decode(&result))
if assert.Len(t, result, 2) {
assert.EqualValues(t, "new a", result[0].A)
}
}

View file

@ -17,7 +17,7 @@ import (
)
// CreatePull creates a PR in the given repo and prints the result
func CreatePull(ctx *context.TeaContext, base, head string, opts *gitea.CreateIssueOption) (err error) {
func CreatePull(ctx *context.TeaContext, base, head string, allowMaintainerEdits bool, opts *gitea.CreateIssueOption) (err error) {
// default is default branch
if len(base) == 0 {
base, err = GetDefaultPRBase(ctx.Login, ctx.Owner, ctx.Repo)
@ -53,7 +53,9 @@ func CreatePull(ctx *context.TeaContext, base, head string, opts *gitea.CreateIs
return fmt.Errorf("title is required")
}
pr, _, err := ctx.Login.Client().CreatePullRequest(ctx.Owner, ctx.Repo, gitea.CreatePullRequestOption{
client := ctx.Login.Client()
pr, _, err := client.CreatePullRequest(ctx.Owner, ctx.Repo, gitea.CreatePullRequestOption{
Head: head,
Base: base,
Title: opts.Title,
@ -68,6 +70,15 @@ func CreatePull(ctx *context.TeaContext, base, head string, opts *gitea.CreateIs
return fmt.Errorf("could not create PR from %s to %s:%s: %s", head, ctx.Owner, base, err)
}
if pr.AllowMaintainerEdit != allowMaintainerEdits {
pr, _, err = client.EditPullRequest(ctx.Owner, ctx.Repo, pr.Index, gitea.EditPullRequestOption{
AllowMaintainerEdit: gitea.OptionalBool(allowMaintainerEdits),
})
if err != nil {
return fmt.Errorf("could not enable maintainer edit on pull: %v", err)
}
}
print.PullDetails(pr, nil, nil)
fmt.Println(pr.HTMLURL)