Skip to content

Commit

Permalink
feat: add dry_run support, this mode can check relase
Browse files Browse the repository at this point in the history
  • Loading branch information
sinlov committed Aug 8, 2023
1 parent e39856b commit d5859fd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions gitea_cc_release_plugin/configPlugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const (
//msgTypePost = "post"
//msgTypeInteractive = "interactive"

EnvDryRun = "PLUGIN_DRY_RUN"
NameDryRun = "config.dry_run"

EnvDraft = "PLUGIN_DRAFT"
NameDraft = "config.draft"

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
19 changes: 13 additions & 6 deletions gitea_cc_release_plugin/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
36 changes: 31 additions & 5 deletions gitea_cc_release_plugin/gitea_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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
}

Expand Down Expand Up @@ -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{},
Expand Down
6 changes: 5 additions & 1 deletion gitea_cc_release_plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions gitea_cc_release_plugin_test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (

var (
envDebug = false
envDryRun = false
envRunTestFolderPath = ""
envProjectRoot = ""
envDroneProto = "https"
Expand Down Expand Up @@ -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", "")
Expand Down
1 change: 1 addition & 0 deletions gitea_cc_release_plugin_test/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestPlugin(t *testing.T) {
p.Config = gitea_cc_release_plugin.Config{
Debug: envDebug,
TimeoutSecond: defTimeoutSecond,
DryRun: envDryRun,

RootFolderPath: envProjectRoot,

Expand Down

0 comments on commit d5859fd

Please sign in to comment.