2018-09-03 08:43:00 +02:00
|
|
|
// Copyright 2018 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 cmd
|
|
|
|
|
|
|
|
import (
|
2020-09-23 21:56:34 +02:00
|
|
|
"fmt"
|
2018-09-03 08:43:00 +02:00
|
|
|
"log"
|
2020-09-23 21:56:34 +02:00
|
|
|
"net/http"
|
2019-03-11 17:32:09 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-09-23 21:56:34 +02:00
|
|
|
"strings"
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2018-10-21 20:52:57 +02:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
|
2020-01-04 18:44:25 +01:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-09-03 08:43:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdReleases represents to login a gitea server.
|
|
|
|
var CmdReleases = cli.Command{
|
2020-09-23 21:56:34 +02:00
|
|
|
Name: "release",
|
|
|
|
Aliases: []string{"releases"},
|
|
|
|
Usage: "Manage releases",
|
|
|
|
Description: "Manage releases",
|
2018-09-03 08:43:00 +02:00
|
|
|
Action: runReleases,
|
2020-01-04 18:44:25 +01:00
|
|
|
Subcommands: []*cli.Command{
|
2020-09-23 21:56:34 +02:00
|
|
|
&CmdReleaseList,
|
2020-01-04 18:44:25 +01:00
|
|
|
&CmdReleaseCreate,
|
2020-09-23 21:56:34 +02:00
|
|
|
&CmdReleaseDelete,
|
|
|
|
&CmdReleaseEdit,
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2019-10-23 17:58:18 +02:00
|
|
|
Flags: AllDefaultFlags,
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 21:56:34 +02:00
|
|
|
// CmdReleaseList represents a sub command of Release to list releases
|
|
|
|
var CmdReleaseList = cli.Command{
|
|
|
|
Name: "ls",
|
|
|
|
Usage: "List Releases",
|
|
|
|
Description: "List Releases",
|
|
|
|
Action: runReleases,
|
2020-09-27 14:07:46 +02:00
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&PaginationPageFlag,
|
|
|
|
&PaginationLimitFlag,
|
|
|
|
}, AllDefaultFlags...),
|
2020-09-23 21:56:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
func runReleases(ctx *cli.Context) error {
|
2019-10-23 17:58:18 +02:00
|
|
|
login, owner, repo := initCommand()
|
2018-09-03 08:43:00 +02:00
|
|
|
|
2020-09-27 14:07:46 +02:00
|
|
|
releases, _, err := login.Client().ListReleases(owner, repo, gitea.ListReleasesOptions{ListOptions: getListOptions(ctx)})
|
2018-09-03 08:43:00 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-15 10:38:18 +02:00
|
|
|
headers := []string{
|
|
|
|
"Tag-Name",
|
|
|
|
"Title",
|
|
|
|
"Published At",
|
2020-09-23 21:56:34 +02:00
|
|
|
"Status",
|
2019-09-15 10:38:18 +02:00
|
|
|
"Tar URL",
|
|
|
|
}
|
|
|
|
|
|
|
|
var values [][]string
|
|
|
|
|
2018-09-03 08:43:00 +02:00
|
|
|
if len(releases) == 0 {
|
2019-10-26 16:25:54 +02:00
|
|
|
Output(outputValue, headers, values)
|
2018-09-03 08:43:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
2020-09-23 21:56:34 +02:00
|
|
|
status := "released"
|
|
|
|
if release.IsDraft {
|
|
|
|
status = "draft"
|
|
|
|
} else if release.IsPrerelease {
|
|
|
|
status = "prerelease"
|
|
|
|
}
|
2019-09-15 10:38:18 +02:00
|
|
|
values = append(
|
|
|
|
values,
|
|
|
|
[]string{
|
|
|
|
release.TagName,
|
|
|
|
release.Title,
|
|
|
|
release.PublishedAt.Format("2006-01-02 15:04:05"),
|
2020-09-23 21:56:34 +02:00
|
|
|
status,
|
2019-09-15 10:38:18 +02:00
|
|
|
release.TarURL,
|
|
|
|
},
|
|
|
|
)
|
2018-09-03 08:43:00 +02:00
|
|
|
}
|
2019-10-26 16:25:54 +02:00
|
|
|
Output(outputValue, headers, values)
|
2018-09-03 08:43:00 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-21 20:52:57 +02:00
|
|
|
|
2020-09-23 21:56:34 +02:00
|
|
|
// CmdReleaseCreate represents a sub command of Release to create release
|
2018-10-21 20:52:57 +02:00
|
|
|
var CmdReleaseCreate = cli.Command{
|
|
|
|
Name: "create",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Create a release",
|
|
|
|
Description: `Create a release`,
|
2018-10-21 20:52:57 +02:00
|
|
|
Action: runReleaseCreate,
|
2019-10-23 17:58:18 +02:00
|
|
|
Flags: append([]cli.Flag{
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2018-10-21 20:52:57 +02:00
|
|
|
Name: "tag",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Tag name",
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2018-10-21 20:52:57 +02:00
|
|
|
Name: "target",
|
2019-11-03 21:34:41 +01:00
|
|
|
Usage: "Target refs, branch name or commit id",
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "title",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "Release title",
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "note",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Release notes",
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.BoolFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "draft",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "Is a draft",
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.BoolFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "prerelease",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "Is a pre-release",
|
2018-10-21 20:52:57 +02:00
|
|
|
},
|
2020-01-04 18:44:25 +01:00
|
|
|
&cli.StringSliceFlag{
|
2020-03-06 04:43:28 +01:00
|
|
|
Name: "asset",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "List of files to attach",
|
2019-03-11 17:32:09 +01:00
|
|
|
},
|
2020-09-23 21:56:34 +02:00
|
|
|
}, AllDefaultFlags...),
|
2018-10-21 20:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func runReleaseCreate(ctx *cli.Context) error {
|
2019-10-23 17:58:18 +02:00
|
|
|
login, owner, repo := initCommand()
|
2018-10-21 20:52:57 +02:00
|
|
|
|
2020-09-23 21:56:34 +02:00
|
|
|
release, resp, err := login.Client().CreateRelease(owner, repo, gitea.CreateReleaseOption{
|
2018-10-21 20:52:57 +02:00
|
|
|
TagName: ctx.String("tag"),
|
|
|
|
Target: ctx.String("target"),
|
|
|
|
Title: ctx.String("title"),
|
|
|
|
Note: ctx.String("note"),
|
|
|
|
IsDraft: ctx.Bool("draft"),
|
|
|
|
IsPrerelease: ctx.Bool("prerelease"),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2020-09-23 21:56:34 +02:00
|
|
|
if resp != nil && resp.StatusCode == http.StatusConflict {
|
|
|
|
fmt.Println("error: There already is a release for this tag")
|
|
|
|
return nil
|
2019-03-11 17:32:09 +01:00
|
|
|
}
|
2018-10-21 20:52:57 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:32:09 +01:00
|
|
|
for _, asset := range ctx.StringSlice("asset") {
|
|
|
|
var file *os.File
|
|
|
|
|
|
|
|
if file, err = os.Open(asset); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := filepath.Base(asset)
|
|
|
|
|
2020-09-16 04:01:41 +02:00
|
|
|
if _, _, err = login.Client().CreateReleaseAttachment(owner, repo, release.ID, file, filePath); err != nil {
|
2019-03-11 17:32:09 +01:00
|
|
|
file.Close()
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
2018-10-21 20:52:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-23 21:56:34 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|