From e0e6271f6206e394b73baad45172652b1b650e82 Mon Sep 17 00:00:00 2001 From: Thorsten Klein Date: Thu, 24 Jun 2021 13:54:29 +0200 Subject: [PATCH 1/2] Allow creating/updating the release discussion category - bump GitHub dep to v35.3.0 as this was introduced in v35.1.0 - add new string flag "discussion-category" (+ env vars) - update release: only set category, if not set - NOTE: Currently it looks like release drafts with discussion category set do not "persist" the category when being released, so when publishing a release draft, without explicitly setting the category, it won't have a release discussion. Nothing we can do here I suppose. Signed-off-by: Thorsten Klein --- cmd/drone-github-release/config.go | 6 ++++ go.mod | 4 +-- go.sum | 5 +++ plugin/impl.go | 50 ++++++++++++++++-------------- plugin/release.go | 44 +++++++++++++++++--------- 5 files changed, 68 insertions(+), 41 deletions(-) diff --git a/cmd/drone-github-release/config.go b/cmd/drone-github-release/config.go index 86efcf3..7ad5d18 100644 --- a/cmd/drone-github-release/config.go +++ b/cmd/drone-github-release/config.go @@ -63,6 +63,12 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { EnvVars: []string{"PLUGIN_PRERELEASE", "GITHUB_RELEASE_PRERELEASE"}, Destination: &settings.Prerelease, }, + &cli.StringFlag{ + Name: "discussion-category", + Usage: "create a discussion in the given category", + EnvVars: []string{"PLUGIN_DISCUSSION_CATEGORY", "GITHUB_RELEASE_DISCUSSION_CATEGORY"}, + Destination: &settings.DiscussionCategory, + }, &cli.StringFlag{ Name: "base-url", Usage: "api url, needs to be changed for ghe", diff --git a/go.mod b/go.mod index 8191bfa..383f004 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.15 require ( github.com/drone-plugins/drone-plugin-lib v0.4.0 - github.com/google/go-github/v35 v35.0.0 + github.com/google/go-github/v35 v35.3.0 github.com/joho/godotenv v1.3.0 github.com/urfave/cli/v2 v2.3.0 - golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78 + golang.org/x/oauth2 v0.0.0-20210622215436-a8dc77f794b6 ) diff --git a/go.sum b/go.sum index 7163232..5385fb5 100644 --- a/go.sum +++ b/go.sum @@ -86,8 +86,11 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github/v35 v35.0.0 h1:oLrHdYkSQvbhN4gJihpEkTFKAZnIFgTCj1p/OlE4Os4= github.com/google/go-github/v35 v35.0.0/go.mod h1:s0515YVTI+IMrDoy9Y4pHt9ShGpzHvHO8rZ7L7acgvs= +github.com/google/go-github/v35 v35.3.0 h1:fU+WBzuukn0VssbayTT+Zo3/ESKX9JYWjbZTLOTEyho= +github.com/google/go-github/v35 v35.3.0/go.mod h1:yWB7uCcVWaUbUP74Aq3whuMySRMatyRmq5U9FTNlbio= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -210,6 +213,8 @@ golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4Iltr golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78 h1:rPRtHfUb0UKZeZ6GH4K4Nt4YRbE9V1u+QZX5upZXqJQ= golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= +golang.org/x/oauth2 v0.0.0-20210622215436-a8dc77f794b6 h1:pERGha6IgvMUdN6oJbwjZTt1ai5/O855Qmv1Bsc0v18= +golang.org/x/oauth2 v0.0.0-20210622215436-a8dc77f794b6/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/plugin/impl.go b/plugin/impl.go index 7eca18b..aec377e 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -19,19 +19,20 @@ import ( // Settings for the plugin. type Settings struct { - APIKey string - Files cli.StringSlice - FileExists string - Checksum cli.StringSlice - ChecksumFile string - ChecksumFlatten bool - Draft bool - Prerelease bool - BaseURL string - UploadURL string - Title string - Note string - Overwrite bool + APIKey string + Files cli.StringSlice + FileExists string + Checksum cli.StringSlice + ChecksumFile string + ChecksumFlatten bool + Draft bool + Prerelease bool + DiscussionCategory string + BaseURL string + UploadURL string + Title string + Note string + Overwrite bool baseURL *url.URL uploadURL *url.URL @@ -125,17 +126,18 @@ func (p *Plugin) Execute() error { client.UploadURL = p.settings.uploadURL rc := releaseClient{ - Client: client, - Context: p.network.Context, - Owner: p.pipeline.Repo.Owner, - Repo: p.pipeline.Repo.Name, - Tag: strings.TrimPrefix(p.pipeline.Commit.Ref, "refs/tags/"), - Draft: p.settings.Draft, - Prerelease: p.settings.Prerelease, - FileExists: p.settings.FileExists, - Title: p.settings.Title, - Note: p.settings.Note, - Overwrite: p.settings.Overwrite, + Client: client, + Context: p.network.Context, + Owner: p.pipeline.Repo.Owner, + Repo: p.pipeline.Repo.Name, + Tag: strings.TrimPrefix(p.pipeline.Commit.Ref, "refs/tags/"), + Draft: p.settings.Draft, + Prerelease: p.settings.Prerelease, + DiscussionCategory: p.settings.DiscussionCategory, + FileExists: p.settings.FileExists, + Title: p.settings.Title, + Note: p.settings.Note, + Overwrite: p.settings.Overwrite, } release, err := rc.buildRelease() diff --git a/plugin/release.go b/plugin/release.go index b070935..430782d 100644 --- a/plugin/release.go +++ b/plugin/release.go @@ -18,15 +18,16 @@ import ( type releaseClient struct { *github.Client context.Context - Owner string - Repo string - Tag string - Draft bool - Prerelease bool - FileExists string - Title string - Note string - Overwrite bool + Owner string + Repo string + Tag string + Draft bool + Prerelease bool + DiscussionCategory string + FileExists string + Title string + Note string + Overwrite bool } func (rc *releaseClient) buildRelease() (*github.RepositoryRelease, error) { @@ -95,13 +96,19 @@ func (rc *releaseClient) editRelease(targetRelease github.RepositoryRelease) (*g // only potentially change the draft value, if it's a draft right now // i.e. a drafted release will be published, but a release won't be unpublished if targetRelease.GetDraft() { - fmt.Printf("DRAFT: %+v\n", rc.Draft) if !rc.Draft { fmt.Println("Publishing a release draft") } sourceRelease.Draft = &rc.Draft } + // do not overwrite the discussion category + if targetRelease.GetDiscussionCategoryName() == "" { + if rc.DiscussionCategory != "" { + sourceRelease.DiscussionCategoryName = &rc.DiscussionCategory + } + } + modifiedRelease, _, err := rc.Client.Repositories.EditRelease(rc.Context, rc.Owner, rc.Repo, targetRelease.GetID(), sourceRelease) if err != nil { @@ -114,11 +121,12 @@ func (rc *releaseClient) editRelease(targetRelease github.RepositoryRelease) (*g func (rc *releaseClient) newRelease() (*github.RepositoryRelease, error) { rr := &github.RepositoryRelease{ - TagName: github.String(rc.Tag), - Draft: &rc.Draft, - Prerelease: &rc.Prerelease, - Name: &rc.Title, - Body: &rc.Note, + TagName: github.String(rc.Tag), + Draft: &rc.Draft, + Prerelease: &rc.Prerelease, + DiscussionCategoryName: &rc.DiscussionCategory, + Name: &rc.Title, + Body: &rc.Note, } if *rr.Prerelease { @@ -133,6 +141,12 @@ func (rc *releaseClient) newRelease() (*github.RepositoryRelease, error) { fmt.Printf("Release %s will be created and published\n", rc.Tag) } + if *rr.DiscussionCategoryName != "" { + fmt.Printf("Release discussion in category %s\n", *rr.DiscussionCategoryName) + } else { + fmt.Println("Not creating a discussion") + } + release, _, err := rc.Client.Repositories.CreateRelease(rc.Context, rc.Owner, rc.Repo, rr) if err != nil { From 8d8dbcca67325d7ab2a010670b04a3299ec93c55 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 31 Jan 2022 18:54:49 +0100 Subject: [PATCH 2/2] gofumpt --- cmd/drone-github-release/main.go | 2 +- plugin/impl.go | 2 -- plugin/plugin_test.go | 2 -- plugin/release.go | 7 ------- plugin/utils.go | 16 +++++----------- 5 files changed, 6 insertions(+), 23 deletions(-) diff --git a/cmd/drone-github-release/main.go b/cmd/drone-github-release/main.go index c415bc7..fe4b69e 100644 --- a/cmd/drone-github-release/main.go +++ b/cmd/drone-github-release/main.go @@ -10,11 +10,11 @@ package main import ( "os" + "github.com/drone-plugins/drone-github-release/plugin" "github.com/drone-plugins/drone-plugin-lib/errors" "github.com/drone-plugins/drone-plugin-lib/urfave" "github.com/joho/godotenv" "github.com/urfave/cli/v2" - "github.com/drone-plugins/drone-github-release/plugin" ) var version = "unknown" diff --git a/plugin/impl.go b/plugin/impl.go index aec377e..f146de5 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -86,7 +86,6 @@ func (p *Plugin) Validate() error { files := p.settings.Files.Value() for _, glob := range files { globed, err := filepath.Glob(glob) - if err != nil { return fmt.Errorf("failed to glob %s: %w", glob, err) } @@ -141,7 +140,6 @@ func (p *Plugin) Execute() error { } release, err := rc.buildRelease() - if err != nil { return fmt.Errorf("failed to create the release: %w", err) } diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 511ebdb..f9a2781 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -11,7 +11,6 @@ import ( func TestReadStringOrFileSelf(t *testing.T) { contents, err := readStringOrFile("./plugin/plugin_test.go") - if err != nil { t.Error(err) return @@ -24,7 +23,6 @@ func TestReadStringOrFileSelf(t *testing.T) { func TestReadStringOrFileLongString(t *testing.T) { s := "if the string is extremely long it will still try to ask the OS to read this as a file which in some cases will not be allowed because of the length of the file name however the plugin might try this anyways but most file systems only allow a maximum of 255 chars for a file name but up to 4096 for a full path thats a lot of characters" contents, err := readStringOrFile(s) - if err != nil { t.Error(err) return diff --git a/plugin/release.go b/plugin/release.go index 430782d..b731da7 100644 --- a/plugin/release.go +++ b/plugin/release.go @@ -33,7 +33,6 @@ type releaseClient struct { func (rc *releaseClient) buildRelease() (*github.RepositoryRelease, error) { // first attempt to get a release by that tag release, err := rc.getRelease() - if err != nil { return nil, fmt.Errorf("failed to retrieve a release: %w", err) } @@ -54,7 +53,6 @@ func (rc *releaseClient) buildRelease() (*github.RepositoryRelease, error) { } func (rc *releaseClient) getRelease() (*github.RepositoryRelease, error) { - listOpts := &github.ListOptions{PerPage: 10} for { @@ -66,7 +64,6 @@ func (rc *releaseClient) getRelease() (*github.RepositoryRelease, error) { // browse through current release page for _, release := range releases { - // return release associated to the given tag (can only be one) if release.GetTagName() == rc.Tag { fmt.Printf("Found release %d for tag %s\n", release.GetID(), release.GetTagName()) @@ -110,7 +107,6 @@ func (rc *releaseClient) editRelease(targetRelease github.RepositoryRelease) (*g } modifiedRelease, _, err := rc.Client.Repositories.EditRelease(rc.Context, rc.Owner, rc.Repo, targetRelease.GetID(), sourceRelease) - if err != nil { return nil, fmt.Errorf("failed to update release: %w", err) } @@ -148,7 +144,6 @@ func (rc *releaseClient) newRelease() (*github.RepositoryRelease, error) { } release, _, err := rc.Client.Repositories.CreateRelease(rc.Context, rc.Owner, rc.Repo, rr) - if err != nil { return nil, fmt.Errorf("failed to create release: %w", err) } @@ -159,7 +154,6 @@ func (rc *releaseClient) newRelease() (*github.RepositoryRelease, error) { func (rc *releaseClient) uploadFiles(id int64, files []string) error { assets, _, err := rc.Client.Repositories.ListReleaseAssets(rc.Context, rc.Owner, rc.Repo, id, &github.ListOptions{}) - if err != nil { return fmt.Errorf("failed to fetch existing assets: %w", err) } @@ -189,7 +183,6 @@ files: for _, file := range uploadFiles { handle, err := os.Open(file) - if err != nil { return fmt.Errorf("failed to read %s artifact: %w", file, err) } diff --git a/plugin/utils.go b/plugin/utils.go index 9d680af..dcdaf36 100644 --- a/plugin/utils.go +++ b/plugin/utils.go @@ -21,13 +21,11 @@ import ( "strings" ) -var ( - fileExistsValues = map[string]bool{ - "overwrite": true, - "fail": true, - "skip": true, - } -) +var fileExistsValues = map[string]bool{ + "overwrite": true, + "fail": true, + "skip": true, +} func readStringOrFile(input string) (string, error) { if len(input) > 255 { @@ -49,7 +47,6 @@ func readStringOrFile(input string) (string, error) { func checksum(r io.Reader, method string) (string, error) { b, err := ioutil.ReadAll(r) - if err != nil { return "", err } @@ -78,13 +75,11 @@ func writeChecksums(files, methods []string, format string, flatten bool) ([]str for _, method := range methods { for _, file := range files { handle, err := os.Open(file) - if err != nil { return nil, fmt.Errorf("failed to read %s artifact: %w", file, err) } hash, err := checksum(handle, method) - if err != nil { return nil, err } @@ -96,7 +91,6 @@ func writeChecksums(files, methods []string, format string, flatten bool) ([]str for method, results := range checksums { filename := strings.Replace(format, "CHECKSUM", method, -1) f, err := os.Create(filename) - if err != nil { return nil, err }