Add Release Subcomands (#195)

rm release flag

fix msg's

Releases: add "ls" and more

Add Edit a release

Release Create: better error handling

Add: "Delete a release"

Release List: add Status

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/195
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
6543 2020-09-23 19:56:34 +00:00
parent 2e701ee8a2
commit 288a8574c3
1 changed files with 169 additions and 9 deletions

View File

@ -5,9 +5,12 @@
package cmd package cmd
import ( import (
"fmt"
"log" "log"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
@ -16,16 +19,29 @@ import (
// CmdReleases represents to login a gitea server. // CmdReleases represents to login a gitea server.
var CmdReleases = cli.Command{ var CmdReleases = cli.Command{
Name: "releases", Name: "release",
Usage: "Create releases", Aliases: []string{"releases"},
Description: `Create releases`, Usage: "Manage releases",
Description: "Manage releases",
Action: runReleases, Action: runReleases,
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
&CmdReleaseList,
&CmdReleaseCreate, &CmdReleaseCreate,
&CmdReleaseDelete,
&CmdReleaseEdit,
}, },
Flags: AllDefaultFlags, Flags: AllDefaultFlags,
} }
// CmdReleaseList represents a sub command of Release to list releases
var CmdReleaseList = cli.Command{
Name: "ls",
Usage: "List Releases",
Description: "List Releases",
Action: runReleases,
Flags: AllDefaultFlags,
}
func runReleases(ctx *cli.Context) error { func runReleases(ctx *cli.Context) error {
login, owner, repo := initCommand() login, owner, repo := initCommand()
@ -38,6 +54,7 @@ func runReleases(ctx *cli.Context) error {
"Tag-Name", "Tag-Name",
"Title", "Title",
"Published At", "Published At",
"Status",
"Tar URL", "Tar URL",
} }
@ -49,12 +66,19 @@ func runReleases(ctx *cli.Context) error {
} }
for _, release := range releases { for _, release := range releases {
status := "released"
if release.IsDraft {
status = "draft"
} else if release.IsPrerelease {
status = "prerelease"
}
values = append( values = append(
values, values,
[]string{ []string{
release.TagName, release.TagName,
release.Title, release.Title,
release.PublishedAt.Format("2006-01-02 15:04:05"), release.PublishedAt.Format("2006-01-02 15:04:05"),
status,
release.TarURL, release.TarURL,
}, },
) )
@ -64,7 +88,7 @@ func runReleases(ctx *cli.Context) error {
return nil return nil
} }
// CmdReleaseCreate represents a sub command of Release to create release. // CmdReleaseCreate represents a sub command of Release to create release
var CmdReleaseCreate = cli.Command{ var CmdReleaseCreate = cli.Command{
Name: "create", Name: "create",
Usage: "Create a release", Usage: "Create a release",
@ -104,13 +128,13 @@ var CmdReleaseCreate = cli.Command{
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "List of files to attach", Usage: "List of files to attach",
}, },
}, LoginRepoFlags...), }, AllDefaultFlags...),
} }
func runReleaseCreate(ctx *cli.Context) error { func runReleaseCreate(ctx *cli.Context) error {
login, owner, repo := initCommand() login, owner, repo := initCommand()
release, _, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{ release, resp, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
TagName: ctx.String("tag"), TagName: ctx.String("tag"),
Target: ctx.String("target"), Target: ctx.String("target"),
Title: ctx.String("title"), Title: ctx.String("title"),
@ -120,10 +144,10 @@ func runReleaseCreate(ctx *cli.Context) error {
}) })
if err != nil { if err != nil {
if err.Error() == "409 Conflict" { if resp != nil && resp.StatusCode == http.StatusConflict {
log.Fatal("error: There already is a release for this tag") fmt.Println("error: There already is a release for this tag")
return nil
} }
log.Fatal(err) log.Fatal(err)
} }
@ -146,3 +170,139 @@ func runReleaseCreate(ctx *cli.Context) error {
return nil return nil
} }
// CmdReleaseDelete represents a sub command of Release to delete a release
var CmdReleaseDelete = cli.Command{
Name: "delete",
Usage: "Delete a release",
Description: `Delete a release`,
ArgsUsage: "<release tag>",
Action: runReleaseDelete,
Flags: AllDefaultFlags,
}
func runReleaseDelete(ctx *cli.Context) error {
login, owner, repo := initCommand()
client := login.Client()
tag := ctx.Args().First()
if len(tag) == 0 {
fmt.Println("Release tag needed to delete")
return nil
}
release, err := getReleaseByTag(owner, repo, tag, client)
if err != nil {
return err
}
if release == nil {
return nil
}
_, err = client.DeleteRelease(owner, repo, release.ID)
return err
}
func getReleaseByTag(owner, repo, tag string, client *gitea.Client) (*gitea.Release, error) {
rl, _, err := client.ListReleases(owner, repo, gitea.ListReleasesOptions{})
if err != nil {
return nil, err
}
if len(rl) == 0 {
fmt.Println("Repo does not have any release")
return nil, nil
}
for _, r := range rl {
if r.TagName == tag {
return r, nil
}
}
fmt.Println("Release tag does not exist")
return nil, nil
}
// CmdReleaseEdit represents a sub command of Release to edit releases
var CmdReleaseEdit = cli.Command{
Name: "edit",
Usage: "Edit a release",
Description: `Edit a release`,
ArgsUsage: "<release tag>",
Action: runReleaseEdit,
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "tag",
Usage: "Change Tag",
},
&cli.StringFlag{
Name: "target",
Usage: "Change Target",
},
&cli.StringFlag{
Name: "title",
Aliases: []string{"t"},
Usage: "Change Title",
},
&cli.StringFlag{
Name: "note",
Aliases: []string{"n"},
Usage: "Change Notes",
},
&cli.StringFlag{
Name: "draft",
Aliases: []string{"d"},
Usage: "Mark as Draft [True/false]",
DefaultText: "true",
},
&cli.StringFlag{
Name: "prerelease",
Aliases: []string{"p"},
Usage: "Mark as Pre-Release [True/false]",
DefaultText: "true",
},
}, AllDefaultFlags...),
}
func runReleaseEdit(ctx *cli.Context) error {
login, owner, repo := initCommand()
client := login.Client()
tag := ctx.Args().First()
if len(tag) == 0 {
fmt.Println("Release tag needed to edit")
return nil
}
release, err := getReleaseByTag(owner, repo, tag, client)
if err != nil {
return err
}
if release == nil {
return nil
}
var isDraft, isPre *bool
bTrue := true
bFalse := false
if ctx.IsSet("draft") {
isDraft = &bFalse
if strings.ToLower(ctx.String("draft"))[:1] == "t" {
isDraft = &bTrue
}
}
if ctx.IsSet("prerelease") {
isPre = &bFalse
if strings.ToLower(ctx.String("prerelease"))[:1] == "t" {
isPre = &bTrue
}
}
_, _, err = client.EditRelease(owner, repo, release.ID, gitea.EditReleaseOption{
TagName: ctx.String("tag"),
Target: ctx.String("target"),
Title: ctx.String("title"),
Note: ctx.String("note"),
IsDraft: isDraft,
IsPrerelease: isPre,
})
return err
}