2020-09-30 05:11:33 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 01:40:02 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 05:11:33 +00:00
|
|
|
|
|
|
|
package releases
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"code.gitea.io/tea/cmd/flags"
|
2020-12-15 17:38:22 +00:00
|
|
|
"code.gitea.io/tea/modules/context"
|
2020-09-30 05:11:33 +00:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdReleaseCreate represents a sub command of Release to create release
|
|
|
|
var CmdReleaseCreate = cli.Command{
|
|
|
|
Name: "create",
|
2020-12-16 16:47:40 +00:00
|
|
|
Aliases: []string{"c"},
|
2020-09-30 05:11:33 +00:00
|
|
|
Usage: "Create a release",
|
2022-09-13 18:14:02 +00:00
|
|
|
Description: `Create a release for a new or existing git tag`,
|
|
|
|
ArgsUsage: "[<tag>]",
|
2020-09-30 05:11:33 +00:00
|
|
|
Action: runReleaseCreate,
|
|
|
|
Flags: append([]cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "tag",
|
2021-12-02 18:33:56 +00:00
|
|
|
Usage: "Tag name. If the tag does not exist yet, it will be created by Gitea",
|
2020-09-30 05:11:33 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "target",
|
2021-12-02 18:33:56 +00:00
|
|
|
Usage: "Target branch name or commit hash. Defaults to the default branch of the repo",
|
2020-09-30 05:11:33 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "title",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "Release title",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "note",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Usage: "Release notes",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "draft",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "Is a draft",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "prerelease",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "Is a pre-release",
|
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "asset",
|
|
|
|
Aliases: []string{"a"},
|
2021-12-02 18:33:56 +00:00
|
|
|
Usage: "Path to file attachment. Can be specified multiple times",
|
2020-09-30 05:11:33 +00:00
|
|
|
},
|
|
|
|
}, flags.AllDefaultFlags...),
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
func runReleaseCreate(cmd *cli.Context) error {
|
|
|
|
ctx := context.InitCommand(cmd)
|
|
|
|
ctx.Ensure(context.CtxRequirement{RemoteRepo: true})
|
2020-09-30 05:11:33 +00:00
|
|
|
|
2022-09-13 18:14:02 +00:00
|
|
|
tag := ctx.String("tag")
|
|
|
|
if cmd.Args().Present() {
|
|
|
|
if len(tag) != 0 {
|
|
|
|
return fmt.Errorf("ambiguous arguments: provide tagname via --tag or argument, but not both")
|
|
|
|
}
|
|
|
|
tag = cmd.Args().First()
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
release, resp, err := ctx.Login.Client().CreateRelease(ctx.Owner, ctx.Repo, gitea.CreateReleaseOption{
|
2022-09-13 18:14:02 +00:00
|
|
|
TagName: tag,
|
2020-09-30 05:11:33 +00:00
|
|
|
Target: ctx.String("target"),
|
|
|
|
Title: ctx.String("title"),
|
|
|
|
Note: ctx.String("note"),
|
|
|
|
IsDraft: ctx.Bool("draft"),
|
|
|
|
IsPrerelease: ctx.Bool("prerelease"),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if resp != nil && resp.StatusCode == http.StatusConflict {
|
2020-12-16 16:18:10 +00:00
|
|
|
return fmt.Errorf("There already is a release for this tag")
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
2020-12-16 16:18:10 +00:00
|
|
|
return err
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, asset := range ctx.StringSlice("asset") {
|
|
|
|
var file *os.File
|
|
|
|
|
|
|
|
if file, err = os.Open(asset); err != nil {
|
2020-12-16 16:18:10 +00:00
|
|
|
return err
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filePath := filepath.Base(asset)
|
|
|
|
|
2020-12-15 17:38:22 +00:00
|
|
|
if _, _, err = ctx.Login.Client().CreateReleaseAttachment(ctx.Owner, ctx.Repo, release.ID, file, filePath); err != nil {
|
2020-09-30 05:11:33 +00:00
|
|
|
file.Close()
|
2020-12-16 16:18:10 +00:00
|
|
|
return err
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|