Add file_exists option: overwrite/skip/fail

Closes #9
release/0.4
Thomas Frössman 9 years ago
parent cc6a93ab03
commit 00b0b14fc2
  1. 1
      DOCS.md
  2. 46
      main.go
  3. 13
      types.go

@ -3,6 +3,7 @@ can override the default configuration with the following parameters:
* `api_key` - GitHub oauth token with public_repo or repo permission * `api_key` - GitHub oauth token with public_repo or repo permission
* `files` - Files to upload to GitHub Release, globs are allowed * `files` - Files to upload to GitHub Release, globs are allowed
* `file_exists` - What to do if an file asset already exists, supported values: **overwrite** (default), **skip** and **fail**
* `checksum` - Checksum takes hash methods to include in your GitHub release for the files specified. Supported hash methods include md5, sha1, sha256, sha512, adler32, and crc32. * `checksum` - Checksum takes hash methods to include in your GitHub release for the files specified. Supported hash methods include md5, sha1, sha256, sha512, adler32, and crc32.
* `draft` - create a draft release if set to true * `draft` - create a draft release if set to true
* `base_url` - GitHub base URL, only required for GHE * `base_url` - GitHub base URL, only required for GHE

@ -37,6 +37,13 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if vargs.FileExists == "" {
vargs.FileExists = "overwrite"
}
if !fileExistsValues[vargs.FileExists] {
fmt.Printf("invalid value for file_exists: use [empty], overwrite, skip or fail")
}
if vargs.BaseURL == "" { if vargs.BaseURL == "" {
vargs.BaseURL = "https://api.github.com/" vargs.BaseURL = "https://api.github.com/"
} else if !strings.HasSuffix(vargs.BaseURL, "/") { } else if !strings.HasSuffix(vargs.BaseURL, "/") {
@ -104,6 +111,7 @@ func main() {
Repo: repo.Name, Repo: repo.Name,
Tag: filepath.Base(build.Ref), Tag: filepath.Base(build.Ref),
Draft: vargs.Draft, Draft: vargs.Draft,
FileExists: vargs.FileExists,
} }
release, err := rc.buildRelease() release, err := rc.buildRelease()
@ -118,13 +126,20 @@ func main() {
} }
} }
var fileExistsValues = map[string]bool{
"overwrite": true,
"fail": true,
"skip": true,
}
// Release holds ties the drone env data and github client together. // Release holds ties the drone env data and github client together.
type releaseClient struct { type releaseClient struct {
*github.Client *github.Client
Owner string Owner string
Repo string Repo string
Tag string Tag string
Draft bool Draft bool
FileExists string
} }
func (rc *releaseClient) buildRelease() (*github.RepositoryRelease, error) { func (rc *releaseClient) buildRelease() (*github.RepositoryRelease, error) {
@ -159,7 +174,7 @@ func (rc *releaseClient) getRelease() (*github.RepositoryRelease, error) {
func (rc *releaseClient) newRelease() (*github.RepositoryRelease, error) { func (rc *releaseClient) newRelease() (*github.RepositoryRelease, error) {
rr := &github.RepositoryRelease{ rr := &github.RepositoryRelease{
TagName: github.String(rc.Tag), TagName: github.String(rc.Tag),
Draft: &rc.Draft, Draft: &rc.Draft,
} }
release, _, err := rc.Client.Repositories.CreateRelease(rc.Owner, rc.Repo, rr) release, _, err := rc.Client.Repositories.CreateRelease(rc.Owner, rc.Repo, rr)
if err != nil { if err != nil {
@ -176,7 +191,28 @@ func (rc *releaseClient) uploadFiles(id int, files []string) error {
return fmt.Errorf("Failed to fetch existing assets: %s", err) return fmt.Errorf("Failed to fetch existing assets: %s", err)
} }
var uploadFiles []string
files:
for _, file := range files { for _, file := range files {
for _, asset := range assets {
if *asset.Name == path.Base(file) {
switch rc.FileExists {
case "overwrite":
// do nothing
case "fail":
return fmt.Errorf("Asset file %s already exists", path.Base(file))
case "skip":
fmt.Printf("Skipping pre-existing %s artifact\n", *asset.Name)
continue files
default:
return fmt.Errorf("Internal error, unkown file_exist value %s", rc.FileExists)
}
}
}
uploadFiles = append(uploadFiles, file)
}
for _, file := range uploadFiles {
handle, err := os.Open(file) handle, err := os.Open(file)
if err != nil { if err != nil {
return fmt.Errorf("Failed to read %s artifact: %s", file, err) return fmt.Errorf("Failed to read %s artifact: %s", file, err)

@ -4,10 +4,11 @@ import "github.com/drone/drone-go/drone"
// Params are the parameters that the GitHub Release plugin can parse. // Params are the parameters that the GitHub Release plugin can parse.
type Params struct { type Params struct {
BaseURL string `json:"base_url"` BaseURL string `json:"base_url"`
UploadURL string `json:"upload_url"` UploadURL string `json:"upload_url"`
APIKey string `json:"api_key"` APIKey string `json:"api_key"`
Files drone.StringSlice `json:"files"` Files drone.StringSlice `json:"files"`
Checksum drone.StringSlice `json:"checksum"` Checksum drone.StringSlice `json:"checksum"`
Draft bool `json:"draft"` Draft bool `json:"draft"`
FileExists string `json:"file_exists"`
} }

Loading…
Cancel
Save