diff --git a/README.md b/README.md index e302253..0fec12f 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ steps: image: sinlov/drone-gitea-cc-release:1.3.1 # https://hub.docker.com/r/sinlov/drone-gitea-cc-release/tags pull: if-not-exists settings: + # dry_run: false # default false prerelease: true # default true release_gitea_base_url: https://gitea.xxxx.com release_gitea_api_key: @@ -96,6 +97,7 @@ steps: pull: if-not-exists settings: # debug: true # plugin debug switch + # dry_run: false # default false prerelease: true # default true draft: true # default false release_gitea_base_url: https://gitea.xxxx.com @@ -138,6 +140,7 @@ steps: - name: notification-feishu-group-robot-exec # must has env EXEC_DRONE_GITEA-CC-RELEASE and exec tools environment: # PLUGIN_DEBUG: false + PLUGIN_DRY_RUN: false # default false PLUGIN_PRERELEASE: true # default true PLUGIN_DRAFT: true # default false PLUGIN_RELEASE_GITEA_BASE_URL: https://gitea.xxxx.com diff --git a/gitea_cc_release_plugin/configPlugin.go b/gitea_cc_release_plugin/configPlugin.go index 6e3a70a..5d34c95 100644 --- a/gitea_cc_release_plugin/configPlugin.go +++ b/gitea_cc_release_plugin/configPlugin.go @@ -11,6 +11,9 @@ const ( //msgTypePost = "post" //msgTypeInteractive = "interactive" + EnvDryRun = "PLUGIN_DRY_RUN" + NameDryRun = "config.dry_run" + EnvDraft = "PLUGIN_DRAFT" NameDraft = "config.draft" @@ -86,8 +89,11 @@ var ( type ( // Config plugin private config Config struct { - Debug bool - TimeoutSecond uint + Debug bool + TimeoutSecond uint + DryRun bool + GiteaDraft bool + GiteaPrerelease bool RootFolderPath string @@ -104,10 +110,8 @@ type ( PublishPackagePathGo string PublishGoRemovePaths []string - GiteaDraft bool - GiteaPrerelease bool - GiteaTitle string - GiteaNote string + GiteaTitle string + GiteaNote string NoteByConventionChange bool ReadChangeLogFile string diff --git a/gitea_cc_release_plugin/flag.go b/gitea_cc_release_plugin/flag.go index 526eb39..9dca1af 100644 --- a/gitea_cc_release_plugin/flag.go +++ b/gitea_cc_release_plugin/flag.go @@ -55,8 +55,11 @@ func BindCliFlag(c *cli.Context, cliVersion, cliName string, drone drone_info.Dr } config := Config{ - Debug: debug, - TimeoutSecond: c.Uint(NamePluginTimeOut), + Debug: debug, + TimeoutSecond: c.Uint(NamePluginTimeOut), + DryRun: c.Bool(NameDryRun), + GiteaDraft: c.Bool(NameDraft), + GiteaPrerelease: c.Bool(NamePrerelease), RootFolderPath: rootFolderPath, @@ -73,10 +76,8 @@ func BindCliFlag(c *cli.Context, cliVersion, cliName string, drone drone_info.Dr PublishPackagePathGo: publishPackagePathGo, PublishGoRemovePaths: c.StringSlice(NameGiteaPublishGoRemovePaths), - GiteaDraft: c.Bool(NameDraft), - GiteaPrerelease: c.Bool(NamePrerelease), - GiteaTitle: c.String(NameTitle), - GiteaNote: note, + GiteaTitle: c.String(NameTitle), + GiteaNote: note, NoteByConventionChange: c.Bool(NameNoteByConventionChange), ReadChangeLogFile: changeLogFullPath, @@ -248,6 +249,12 @@ func CommonFlag() []cli.Flag { EnvVars: []string{drone_info.EnvKeyPluginDebug}, }, + &cli.BoolFlag{ + Name: NameDryRun, + Usage: "dry run", + Value: false, + EnvVars: []string{EnvDryRun}, + }, &cli.BoolFlag{ Name: NameDraft, Usage: "draft release", diff --git a/gitea_cc_release_plugin/gitea_release.go b/gitea_cc_release_plugin/gitea_release.go index 0af2203..759a2b5 100644 --- a/gitea_cc_release_plugin/gitea_release.go +++ b/gitea_cc_release_plugin/gitea_release.go @@ -34,8 +34,10 @@ var ( // Release holds ties the drone env data and gitea client together. type releaseClient struct { - client *gitea.Client - debug bool + client *gitea.Client + debug bool + dryRun bool + url string ctx context.Context mutex *sync.RWMutex @@ -123,6 +125,11 @@ func (r *releaseClient) PackageGoUpload(rootPath string, removePath []string) (e }(fileBodyIO) uploadPath := fmt.Sprintf("/api/packages/%s/go/upload", r.owner) + if r.dryRun { + drone_log.Infof("PackageGoUpload dryRun: %s\n", outZipPath) + drone_log.Infof("PackageGoUpload uploadPath: %s%s\n", r.url, uploadPath) + return nil, res + } statusCode, errPutGoPackage := r.getApiStatusCode("PUT", uploadPath, nil, fileBodyIO) if errPutGoPackage != nil { return fmt.Errorf("do PackageGoUpload go package [ %s ] err: %v", uploadPath, errPutGoPackage), res @@ -211,6 +218,14 @@ func (r *releaseClient) BuildRelease() (*gitea.Release, error) { return release, nil } + if r.dryRun { + drone_log.Infof("try to create release %s/%s\n", r.owner, r.repo) + drone_log.Infof("dry run, not creating release\n") + return &gitea.Release{ + ID: -1, + }, nil + } + // if no release was found by that tag, create a new one release, err = r.newRelease() @@ -228,7 +243,16 @@ func (r *releaseClient) UploadFiles(releaseID int64) error { drone_log.Infof("%s\n", r.uploadDesc) return nil } - drone_log.Infof("not setting no upload files found\n") + drone_log.Infof("check settings no upload files found!\n") + return nil + } + + if r.dryRun { + drone_log.Infof("try to upload files to release %s/%s\n", r.owner, r.repo) + for _, filePath := range r.uploadFilePaths { + drone_log.Infof("-> try upload file: %s\n", filePath) + } + drone_log.Infof("dry run, not uploading files\n") return nil } @@ -419,8 +443,10 @@ func NewReleaseClientByDrone(drone drone_info.Drone, config Config) (PluginRelea } return &releaseClient{ - client: client, - debug: config.Debug, + client: client, + debug: config.Debug, + dryRun: config.DryRun, + url: strings.TrimSuffix(config.GiteaBaseUrl, "/"), ctx: context.Background(), mutex: &sync.RWMutex{}, diff --git a/gitea_cc_release_plugin/plugin.go b/gitea_cc_release_plugin/plugin.go index b2af59f..38ccc8f 100644 --- a/gitea_cc_release_plugin/plugin.go +++ b/gitea_cc_release_plugin/plugin.go @@ -103,7 +103,11 @@ func (p *Plugin) Exec() error { } drone_log.Warnf("package exists go package owner: %s [ %s/%s ], skip upload\n", p.Drone.Repo.OwnerName, goPkgInfo.ModVersion.Path, goPkgInfo.ModVersion.Version) } else { - drone_log.Infof("successfully uploaded go package owner: %s [ %s/%s ]\n", p.Drone.Repo.OwnerName, goPkgInfo.ModVersion.Path, goPkgInfo.ModVersion.Version) + if p.Config.DryRun { + drone_log.Infof("dry run uploaded go package owner: %s [ %s/%s ]\n", p.Drone.Repo.OwnerName, goPkgInfo.ModVersion.Path, goPkgInfo.ModVersion.Version) + } else { + drone_log.Infof("successfully uploaded go package owner: %s [ %s/%s ]\n", p.Drone.Repo.OwnerName, goPkgInfo.ModVersion.Path, goPkgInfo.ModVersion.Version) + } } } diff --git a/gitea_cc_release_plugin_test/init_test.go b/gitea_cc_release_plugin_test/init_test.go index 88a78b7..3ba5098 100644 --- a/gitea_cc_release_plugin_test/init_test.go +++ b/gitea_cc_release_plugin_test/init_test.go @@ -30,6 +30,7 @@ const ( var ( envDebug = false + envDryRun = false envRunTestFolderPath = "" envProjectRoot = "" envDroneProto = "https" @@ -100,6 +101,7 @@ func init() { envDebug = fetchOsEnvBool(drone_info.EnvKeyPluginDebug, false) || fetchOsEnvBool(drone_info.EnvDroneBuildDebug, false) + envDryRun = fetchOsEnvBool(gitea_cc_release_plugin.EnvDryRun, false) envDroneProto = fetchOsEnvStr("DRONE_PROTO", "https") envDroneHost = fetchOsEnvStr("DRONE_HOST", "") envDroneHostName = fetchOsEnvStr("DRONE_HOST_NAME", "") diff --git a/gitea_cc_release_plugin_test/plugin_test.go b/gitea_cc_release_plugin_test/plugin_test.go index 3f24b87..eeb501d 100644 --- a/gitea_cc_release_plugin_test/plugin_test.go +++ b/gitea_cc_release_plugin_test/plugin_test.go @@ -56,6 +56,7 @@ func TestPlugin(t *testing.T) { p.Config = gitea_cc_release_plugin.Config{ Debug: envDebug, TimeoutSecond: defTimeoutSecond, + DryRun: envDryRun, RootFolderPath: envProjectRoot,