Merge pull request #6 from jackspirou/master

Add Checksum Options
This commit is contained in:
Jack Spirou 2016-01-15 16:05:57 -06:00
commit 07222c719c
6 changed files with 130 additions and 7 deletions

19
DOCS.md
View File

@ -3,15 +3,34 @@ can override the default configuration with the following parameters:
* `api_key` - GitHub oauth token with public_repo or repo permission
* `files` - Files to upload to GitHub Release, globs are allowed
* `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.
* `base_url` - GitHub base URL, only required for GHE
* `upload_url` - GitHub upload URL, only required for GHE
Sample configuration:
```yaml
publish:
github_release:
api_key: my_github_api_key
files: dist/*
checksum: sha1
```
or
```yaml
publish:
github_release:
api_key: my_github_api_key
files:
- dist/*
- bin/binary.exe
checksum:
- md5
- sha1
- sha256
- sha512
- adler32
- crc32
```

View File

@ -31,6 +31,14 @@ Drone plugin for publishing GitHub releases
"files": [
"dist/*.txt",
"dist/other-file"
],
"checksum": [
"md5",
"sha1",
"sha256",
"sha512",
"adler32",
"crc32"
]
}
}
@ -71,6 +79,14 @@ docker run -i plugins/drone-github-release <<EOF
"files": [
"dist/*.txt",
"dist/other-file"
],
"checksum": [
"md5",
"sha1",
"sha256",
"sha512",
"adler32",
"crc32"
]
}
}

38
checksum.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash/adler32"
"hash/crc32"
"io"
"io/ioutil"
"strconv"
)
func checksum(r io.Reader, method string) (string, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
switch method {
case "md5":
return fmt.Sprintf("%x", md5.Sum(b)), nil
case "sha1":
return fmt.Sprintf("%x", sha1.Sum(b)), nil
case "sha256":
return fmt.Sprintf("%x", sha256.Sum256(b)), nil
case "sha512":
return fmt.Sprintf("%x", sha512.Sum512(b)), nil
case "adler32":
return strconv.FormatUint(uint64(adler32.Checksum(b)), 10), nil
case "crc32":
return strconv.FormatUint(uint64(crc32.ChecksumIEEE(b)), 10), nil
default:
return "", fmt.Errorf("hashing method %s is not supported", method)
}
}

51
main.go
View File

@ -60,7 +60,7 @@ func main() {
}
var files []string
for _, glob := range vargs.Files {
for _, glob := range vargs.Files.Slice() {
globed, err := filepath.Glob(glob)
if err != nil {
fmt.Printf("Failed to glob %s\n", glob)
@ -71,13 +71,22 @@ func main() {
}
}
if vargs.Checksum.Len() > 0 {
var err error
files, err = writeChecksums(files, vargs.Checksum.Slice())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
baseURL, err := url.Parse(vargs.BaseURL)
if err != nil {
fmt.Printf("Failed to parse base URL\n")
os.Exit(1)
}
uploadURL, err := url.Parse(vargs.BaseURL)
uploadURL, err := url.Parse(vargs.UploadURL)
if err != nil {
fmt.Printf("Failed to parse upload URL\n")
os.Exit(1)
@ -173,3 +182,41 @@ func uploadFiles(client *github.Client, owner string, repo string, id int, files
return nil
}
func writeChecksums(files, methods []string) ([]string, error) {
checksums := make(map[string][]string)
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: %s", file, err)
}
hash, err := checksum(handle, method)
if err != nil {
return nil, err
}
checksums[method] = append(checksums[method], hash, file)
}
}
for method, results := range checksums {
filename := method + "sum.txt"
f, err := os.Create(filename)
if err != nil {
return nil, err
}
for i := 0; i < len(results); i += 2 {
hash := results[i]
file := results[i+1]
if _, err := f.WriteString(fmt.Sprintf("%s %s\n", hash, file)); err != nil {
return nil, err
}
}
files = append(files, filename)
}
return files, nil
}

View File

@ -1,9 +1,12 @@
package main
import "github.com/drone/drone-go/drone"
// Params are the parameters that the GitHub Release plugin can parse.
type Params struct {
BaseURL string `json:"base_url"`
UploadURL string `json:"upload_url"`
APIKey string `json:"api_key"`
Files []string `json:"files"`
BaseURL string `json:"base_url"`
UploadURL string `json:"upload_url"`
APIKey string `json:"api_key"`
Files drone.StringSlice `json:"files"`
Checksum drone.StringSlice `json:"checksum"`
}