From 27873f6980159de35bbee28d0077f9800aabc0fc Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 3 Mar 2018 12:09:01 +0100 Subject: [PATCH] Add title to release Add note to release Signed-off-by: Jonas Franz --- main.go | 16 +++++++++++++++- plugin.go | 38 ++++++++++++++++++++++++++++++++++++-- release.go | 6 +++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 56f1947..0db72b0 100644 --- a/main.go +++ b/main.go @@ -54,6 +54,18 @@ func main() { Usage: "url of the gitea instance", EnvVar: "PLUGIN_BASE_URL,GITEA_RELEASE_BASE_URL", }, + cli.StringFlag{ + Name: "note", + Value: "", + Usage: "file or string with notes for the release (example: changelog)", + EnvVar: "PLUGIN_NOTE,GITEA_RELEASE_NOTE", + }, + cli.StringFlag{ + Name: "title", + Value: "", + Usage: "file or string for the title shown in the gitea release", + EnvVar: "PLUGIN_TITLE,GITEA_RELEASE_TITLE", + }, cli.StringFlag{ Name: "repo.owner", Usage: "repository owner", @@ -109,8 +121,10 @@ func run(c *cli.Context) error { FileExists: c.String("file-exists"), Checksum: c.StringSlice("checksum"), Draft: c.Bool("draft"), - Prerelease: c.Bool("prerelease"), + PreRelease: c.Bool("prerelease"), BaseURL: c.String("base-url"), + Title: c.String("title"), + Note: c.String("note"), }, } diff --git a/plugin.go b/plugin.go index 695b889..ac0da4f 100644 --- a/plugin.go +++ b/plugin.go @@ -6,6 +6,8 @@ import ( "strings" "code.gitea.io/sdk/gitea" + "io/ioutil" + "os" ) type ( @@ -28,8 +30,10 @@ type ( FileExists string Checksum []string Draft bool - Prerelease bool + PreRelease bool BaseURL string + Title string + Note string } Plugin struct { @@ -65,6 +69,19 @@ func (p Plugin) Exec() error { p.Config.BaseURL = p.Config.BaseURL + "/" } + var err error + if p.Config.Note != "" { + if p.Config.Note, err = readStringOrFile(p.Config.Note); err != nil { + return fmt.Errorf("error while reading %s: %v", p.Config.Note, err) + } + } + + if p.Config.Title != "" { + if p.Config.Title, err = readStringOrFile(p.Config.Title); err != nil { + return fmt.Errorf("error while reading %s: %v", p.Config.Note, err) + } + } + for _, glob := range p.Config.Files { globed, err := filepath.Glob(glob) @@ -97,8 +114,10 @@ func (p Plugin) Exec() error { Repo: p.Repo.Name, Tag: strings.TrimPrefix(p.Commit.Ref, "refs/tags/"), Draft: p.Config.Draft, - Prerelease: p.Config.Prerelease, + Prerelease: p.Config.PreRelease, FileExists: p.Config.FileExists, + Title: p.Config.Title, + Note: p.Config.Note, } release, err := rc.buildRelease() @@ -113,3 +132,18 @@ func (p Plugin) Exec() error { return nil } + +func readStringOrFile(input string) (string, error) { + // Check if input is a file path + if _, err := os.Stat(input); err != nil && os.IsNotExist(err) { + // No file found => use input as result + return input, nil + } else if err != nil { + return "", err + } + result, err := ioutil.ReadFile(input) + if err != nil { + return "", err + } + return string(result), nil +} diff --git a/release.go b/release.go index 43c109d..c0f61f0 100644 --- a/release.go +++ b/release.go @@ -17,6 +17,8 @@ type releaseClient struct { Draft bool Prerelease bool FileExists string + Title string + Note string } func (rc *releaseClient) buildRelease() (*gitea.Release, error) { @@ -56,9 +58,11 @@ func (rc *releaseClient) getRelease() (*gitea.Release, error) { func (rc *releaseClient) newRelease() (*gitea.Release, error) { r := gitea.CreateReleaseOption{ - TagName: rc.Tag, + TagName: rc.Tag, IsDraft: rc.Draft, IsPrerelease: rc.Prerelease, + Title: rc.Title, + Note: rc.Note, } release, err := rc.Client.CreateRelease(rc.Owner, rc.Repo, r)