From 288a8574c3c1a8550e77f9721fff06178044cd71 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Wed, 23 Sep 2020 19:56:34 +0000 Subject: [PATCH] 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 Reviewed-by: Norwin --- cmd/releases.go | 178 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 169 insertions(+), 9 deletions(-) diff --git a/cmd/releases.go b/cmd/releases.go index daafd3b..6d55df3 100644 --- a/cmd/releases.go +++ b/cmd/releases.go @@ -5,9 +5,12 @@ package cmd import ( + "fmt" "log" + "net/http" "os" "path/filepath" + "strings" "code.gitea.io/sdk/gitea" @@ -16,16 +19,29 @@ import ( // CmdReleases represents to login a gitea server. var CmdReleases = cli.Command{ - Name: "releases", - Usage: "Create releases", - Description: `Create releases`, + Name: "release", + Aliases: []string{"releases"}, + Usage: "Manage releases", + Description: "Manage releases", Action: runReleases, Subcommands: []*cli.Command{ + &CmdReleaseList, &CmdReleaseCreate, + &CmdReleaseDelete, + &CmdReleaseEdit, }, 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 { login, owner, repo := initCommand() @@ -38,6 +54,7 @@ func runReleases(ctx *cli.Context) error { "Tag-Name", "Title", "Published At", + "Status", "Tar URL", } @@ -49,12 +66,19 @@ func runReleases(ctx *cli.Context) error { } for _, release := range releases { + status := "released" + if release.IsDraft { + status = "draft" + } else if release.IsPrerelease { + status = "prerelease" + } values = append( values, []string{ release.TagName, release.Title, release.PublishedAt.Format("2006-01-02 15:04:05"), + status, release.TarURL, }, ) @@ -64,7 +88,7 @@ func runReleases(ctx *cli.Context) error { 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{ Name: "create", Usage: "Create a release", @@ -104,13 +128,13 @@ var CmdReleaseCreate = cli.Command{ Aliases: []string{"a"}, Usage: "List of files to attach", }, - }, LoginRepoFlags...), + }, AllDefaultFlags...), } func runReleaseCreate(ctx *cli.Context) error { 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"), Target: ctx.String("target"), Title: ctx.String("title"), @@ -120,10 +144,10 @@ func runReleaseCreate(ctx *cli.Context) error { }) if err != nil { - if err.Error() == "409 Conflict" { - log.Fatal("error: There already is a release for this tag") + if resp != nil && resp.StatusCode == http.StatusConflict { + fmt.Println("error: There already is a release for this tag") + return nil } - log.Fatal(err) } @@ -146,3 +170,139 @@ func runReleaseCreate(ctx *cli.Context) error { 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: "", + 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: "", + 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 +}