2015-11-23 00:07:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/drone/drone-go/drone"
|
|
|
|
"github.com/drone/drone-go/plugin"
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2015-12-23 14:18:22 +00:00
|
|
|
var (
|
|
|
|
build string
|
|
|
|
buildDate string
|
|
|
|
)
|
2015-11-23 00:07:57 +00:00
|
|
|
|
|
|
|
func main() {
|
2015-12-23 14:18:22 +00:00
|
|
|
fmt.Printf("Drone GitHub Release Plugin built at %s\n", buildDate)
|
|
|
|
|
2015-12-07 08:01:55 +00:00
|
|
|
workspace := drone.Workspace{}
|
2015-11-23 21:46:49 +00:00
|
|
|
repo := drone.Repo{}
|
|
|
|
build := drone.Build{}
|
|
|
|
vargs := Params{}
|
2015-11-23 00:07:57 +00:00
|
|
|
|
2015-12-23 14:18:22 +00:00
|
|
|
plugin.Param("workspace", &workspace)
|
2015-11-23 21:46:49 +00:00
|
|
|
plugin.Param("repo", &repo)
|
|
|
|
plugin.Param("build", &build)
|
|
|
|
plugin.Param("vargs", &vargs)
|
2015-11-23 00:07:57 +00:00
|
|
|
plugin.MustParse()
|
|
|
|
|
2015-11-23 21:46:49 +00:00
|
|
|
if build.Event != "tag" {
|
2015-11-23 00:07:57 +00:00
|
|
|
fmt.Printf("The GitHub Release plugin is only available for tags\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
if vargs.BaseURL == "" {
|
|
|
|
vargs.BaseURL = "https://api.github.com/"
|
|
|
|
} else if !strings.HasSuffix(vargs.BaseURL, "/") {
|
|
|
|
vargs.BaseURL = vargs.BaseURL + "/"
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
if vargs.UploadURL == "" {
|
|
|
|
vargs.UploadURL = "https://uploads.github.com/"
|
|
|
|
} else if !strings.HasSuffix(vargs.UploadURL, "/") {
|
|
|
|
vargs.UploadURL = vargs.UploadURL + "/"
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
if vargs.APIKey == "" {
|
2015-11-23 00:07:57 +00:00
|
|
|
fmt.Printf("You must provide an API key\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
if workspace.Path != "" {
|
2015-12-23 14:18:22 +00:00
|
|
|
os.Chdir(workspace.Path)
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
var files []string
|
2015-11-23 21:46:49 +00:00
|
|
|
for _, glob := range vargs.Files {
|
2015-11-23 00:07:57 +00:00
|
|
|
globed, err := filepath.Glob(glob)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to glob %s\n", glob)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if globed != nil {
|
|
|
|
files = append(files, globed...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:46:00 +00:00
|
|
|
if len(vargs.Checksums) > 0 {
|
|
|
|
var err error
|
|
|
|
files, err = writeChecksums(files, vargs.Checksums)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
baseURL, err := url.Parse(vargs.BaseURL)
|
2015-11-23 00:07:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse base URL\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
uploadURL, err := url.Parse(vargs.BaseURL)
|
2015-11-23 00:07:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse upload URL\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: vargs.APIKey})
|
|
|
|
tc := oauth2.NewClient(oauth2.NoContext, ts)
|
2015-11-23 00:07:57 +00:00
|
|
|
|
|
|
|
client := github.NewClient(tc)
|
2016-01-12 04:33:03 +00:00
|
|
|
client.BaseURL = baseURL
|
|
|
|
client.UploadURL = uploadURL
|
2015-11-23 00:07:57 +00:00
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
release, err := buildRelease(client, repo.Owner, repo.Name, filepath.Base(build.Ref))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
2015-11-23 00:07:57 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
if err := uploadFiles(client, repo.Owner, repo.Name, *release.ID, files); err != nil {
|
|
|
|
fmt.Println(err)
|
2015-11-23 00:07:57 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
func buildRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
2015-11-23 00:07:57 +00:00
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
// first attempt to get a release by that tag
|
|
|
|
release, err := getRelease(client, owner, repo, tag)
|
|
|
|
if err != nil && release == nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else if release != nil {
|
2015-11-23 00:07:57 +00:00
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
|
2016-01-12 12:44:01 +00:00
|
|
|
// if no release was found by that tag, create a new one
|
2016-01-12 04:33:03 +00:00
|
|
|
release, err = newRelease(client, owner, repo, tag)
|
2015-12-23 14:18:22 +00:00
|
|
|
if err != nil {
|
2016-01-12 04:40:06 +00:00
|
|
|
return nil, fmt.Errorf("Failed to retrieve or create a release: %s", err)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 04:40:06 +00:00
|
|
|
return release, nil
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
func getRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
|
|
|
release, _, err := client.Repositories.GetReleaseByTag(owner, repo, tag)
|
2015-11-23 00:07:57 +00:00
|
|
|
if err != nil {
|
2016-01-12 04:33:03 +00:00
|
|
|
return nil, fmt.Errorf("Release %s not found", tag)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Successfully retrieved %s release\n", tag)
|
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
func newRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
|
|
|
rr := &github.RepositoryRelease{TagName: github.String(tag)}
|
|
|
|
release, _, err := client.Repositories.CreateRelease(owner, repo, rr)
|
2015-11-23 00:07:57 +00:00
|
|
|
if err != nil {
|
2016-01-12 04:33:03 +00:00
|
|
|
return nil, fmt.Errorf("Failed to create release: %s", err)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Successfully created %s release\n", tag)
|
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
func uploadFiles(client *github.Client, owner string, repo string, id int, files []string) error {
|
|
|
|
assets, _, err := client.Repositories.ListReleaseAssets(owner, repo, id, &github.ListOptions{})
|
2015-11-23 00:07:57 +00:00
|
|
|
if err != nil {
|
2016-01-12 04:33:03 +00:00
|
|
|
return fmt.Errorf("Failed to fetch existing assets: %s", err)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
handle, err := os.Open(file)
|
|
|
|
if err != nil {
|
2016-01-12 04:33:03 +00:00
|
|
|
return fmt.Errorf("Failed to read %s artifact: %s", file, err)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, asset := range assets {
|
|
|
|
if *asset.Name == path.Base(file) {
|
2016-01-12 04:33:03 +00:00
|
|
|
if _, err := client.Repositories.DeleteReleaseAsset(owner, repo, *asset.ID); err != nil {
|
|
|
|
return fmt.Errorf("Failed to delete %s artifact: %s", file, err)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
2016-01-12 04:33:03 +00:00
|
|
|
fmt.Printf("Successfully deleted old %s artifact\n", *asset.Name)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 04:33:03 +00:00
|
|
|
uo := &github.UploadOptions{Name: path.Base(file)}
|
|
|
|
if _, _, err = client.Repositories.UploadReleaseAsset(owner, repo, id, uo, handle); err != nil {
|
|
|
|
return fmt.Errorf("Failed to upload %s artifact: %s", file, err)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
2016-01-12 04:33:03 +00:00
|
|
|
|
|
|
|
fmt.Printf("Successfully uploaded %s artifact\n", file)
|
2015-11-23 00:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-15 16:46:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(file)
|
|
|
|
fmt.Println(method)
|
|
|
|
fmt.Println(hash)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|