comments, cleanup, and some restructure
This commit is contained in:
parent
2d1ef8960a
commit
7869f1ba85
@ -3,7 +3,7 @@
|
|||||||
# cd $GOPATH/src/github.com/drone-plugins/drone-github-release
|
# cd $GOPATH/src/github.com/drone-plugins/drone-github-release
|
||||||
# make deps build docker
|
# make deps build docker
|
||||||
|
|
||||||
FROM alpine:3.2
|
FROM alpine:3.3
|
||||||
|
|
||||||
RUN apk update && \
|
RUN apk update && \
|
||||||
apk add \
|
apk add \
|
||||||
|
177
main.go
177
main.go
@ -37,222 +37,143 @@ func main() {
|
|||||||
if build.Event != "tag" {
|
if build.Event != "tag" {
|
||||||
fmt.Printf("The GitHub Release plugin is only available for tags\n")
|
fmt.Printf("The GitHub Release plugin is only available for tags\n")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(vargs.BaseUrl) == 0 {
|
if vargs.BaseURL == "" {
|
||||||
vargs.BaseUrl = "https://api.github.com/"
|
vargs.BaseURL = "https://api.github.com/"
|
||||||
} else {
|
} else if !strings.HasSuffix(vargs.BaseURL, "/") {
|
||||||
if !strings.HasSuffix(vargs.BaseUrl, "/") {
|
vargs.BaseURL = vargs.BaseURL + "/"
|
||||||
vargs.BaseUrl = vargs.BaseUrl + "/"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(vargs.UploadUrl) == 0 {
|
if vargs.UploadURL == "" {
|
||||||
vargs.UploadUrl = "https://uploads.github.com/"
|
vargs.UploadURL = "https://uploads.github.com/"
|
||||||
} else {
|
} else if !strings.HasSuffix(vargs.UploadURL, "/") {
|
||||||
if !strings.HasSuffix(vargs.UploadUrl, "/") {
|
vargs.UploadURL = vargs.UploadURL + "/"
|
||||||
vargs.UploadUrl = vargs.UploadUrl + "/"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(vargs.APIKey) == 0 {
|
if vargs.APIKey == "" {
|
||||||
fmt.Printf("You must provide an API key\n")
|
fmt.Printf("You must provide an API key\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(workspace.Path) != 0 {
|
if workspace.Path != "" {
|
||||||
os.Chdir(workspace.Path)
|
os.Chdir(workspace.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
files := make([]string, 0)
|
var files []string
|
||||||
|
|
||||||
for _, glob := range vargs.Files {
|
for _, glob := range vargs.Files {
|
||||||
globed, err := filepath.Glob(glob)
|
globed, err := filepath.Glob(glob)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to glob %s\n", glob)
|
fmt.Printf("Failed to glob %s\n", glob)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if globed != nil {
|
if globed != nil {
|
||||||
files = append(files, globed...)
|
files = append(files, globed...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
baseUrl, err := url.Parse(vargs.BaseUrl)
|
baseURL, err := url.Parse(vargs.BaseURL)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to parse base URL\n")
|
fmt.Printf("Failed to parse base URL\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadUrl, err := url.Parse(vargs.UploadUrl)
|
uploadURL, err := url.Parse(vargs.BaseURL)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to parse upload URL\n")
|
fmt.Printf("Failed to parse upload URL\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ts := oauth2.StaticTokenSource(
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: vargs.APIKey})
|
||||||
&oauth2.Token{
|
tc := oauth2.NewClient(oauth2.NoContext, ts)
|
||||||
AccessToken: vargs.APIKey,
|
|
||||||
})
|
|
||||||
|
|
||||||
tc := oauth2.NewClient(
|
|
||||||
oauth2.NoContext,
|
|
||||||
ts)
|
|
||||||
|
|
||||||
client := github.NewClient(tc)
|
client := github.NewClient(tc)
|
||||||
client.BaseURL = baseUrl
|
client.BaseURL = baseURL
|
||||||
client.UploadURL = uploadUrl
|
client.UploadURL = uploadURL
|
||||||
|
|
||||||
release, releaseErr := prepareRelease(
|
release, err := buildRelease(client, repo.Owner, repo.Name, filepath.Base(build.Ref))
|
||||||
client,
|
if err != nil {
|
||||||
repo.Owner,
|
fmt.Println(err)
|
||||||
repo.Name,
|
|
||||||
filepath.Base(build.Ref))
|
|
||||||
|
|
||||||
if releaseErr != nil {
|
|
||||||
fmt.Println(releaseErr)
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadErr := appendFiles(
|
if err := uploadFiles(client, repo.Owner, repo.Name, *release.ID, files); err != nil {
|
||||||
client,
|
fmt.Println(err)
|
||||||
repo.Owner,
|
|
||||||
repo.Name,
|
|
||||||
*release.ID,
|
|
||||||
files)
|
|
||||||
|
|
||||||
if uploadErr != nil {
|
|
||||||
fmt.Println(uploadErr)
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
func buildRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
||||||
release, _ := retrieveRelease(
|
|
||||||
client,
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
tag)
|
|
||||||
|
|
||||||
if release != nil {
|
// 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 {
|
||||||
return release, nil
|
return release, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := createRelease(
|
// if not release was found by that tag, create a new one
|
||||||
client,
|
release, err = newRelease(client, owner, repo, tag)
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
tag)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if release != nil {
|
if release != nil {
|
||||||
return release, nil
|
return release, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New(
|
return nil, errors.New("Failed to retrieve or create a release")
|
||||||
"Failed to retrieve or create a release")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func retrieveRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
func getRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
||||||
release, _, err := client.Repositories.GetReleaseByTag(
|
release, _, err := client.Repositories.GetReleaseByTag(owner, repo, tag)
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
tag)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(
|
return nil, fmt.Errorf("Release %s not found", tag)
|
||||||
"Failed to retrieve release")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Successfully retrieved %s release\n", tag)
|
fmt.Printf("Successfully retrieved %s release\n", tag)
|
||||||
return release, nil
|
return release, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
func newRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
|
||||||
release, _, err := client.Repositories.CreateRelease(
|
rr := &github.RepositoryRelease{TagName: github.String(tag)}
|
||||||
owner,
|
release, _, err := client.Repositories.CreateRelease(owner, repo, rr)
|
||||||
repo,
|
|
||||||
&github.RepositoryRelease{TagName: github.String(tag)})
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(
|
return nil, fmt.Errorf("Failed to create release: %s", err)
|
||||||
"Failed to create release")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Successfully created %s release\n", tag)
|
fmt.Printf("Successfully created %s release\n", tag)
|
||||||
return release, nil
|
return release, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendFiles(client *github.Client, owner string, repo string, id int, files []string) error {
|
func uploadFiles(client *github.Client, owner string, repo string, id int, files []string) error {
|
||||||
assets, _, err := client.Repositories.ListReleaseAssets(
|
assets, _, err := client.Repositories.ListReleaseAssets(owner, repo, id, &github.ListOptions{})
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
id,
|
|
||||||
&github.ListOptions{})
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(
|
return fmt.Errorf("Failed to fetch existing assets: %s", err)
|
||||||
"Failed to fetch existing assets")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
handle, err := os.Open(file)
|
handle, err := os.Open(file)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(
|
return fmt.Errorf("Failed to read %s artifact: %s", file, err)
|
||||||
fmt.Sprintf("Failed to read %s artifact", file))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, asset := range assets {
|
for _, asset := range assets {
|
||||||
if *asset.Name == path.Base(file) {
|
if *asset.Name == path.Base(file) {
|
||||||
_, deleteErr := client.Repositories.DeleteReleaseAsset(
|
if _, err := client.Repositories.DeleteReleaseAsset(owner, repo, *asset.ID); err != nil {
|
||||||
owner,
|
return fmt.Errorf("Failed to delete %s artifact: %s", file, err)
|
||||||
repo,
|
}
|
||||||
*asset.ID)
|
|
||||||
|
|
||||||
if deleteErr != nil {
|
|
||||||
return errors.New(
|
|
||||||
fmt.Sprintf("Failed to delete %s artifact", file))
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Successfully deleted old %s artifact\n", *asset.Name)
|
fmt.Printf("Successfully deleted old %s artifact\n", *asset.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, uploadErr := client.Repositories.UploadReleaseAsset(
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
id,
|
|
||||||
&github.UploadOptions{Name: path.Base(file)},
|
|
||||||
handle)
|
|
||||||
|
|
||||||
if uploadErr != nil {
|
|
||||||
return errors.New(
|
|
||||||
fmt.Sprintf("Failed to upload %s artifact", file))
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Successfully uploaded %s artifact\n", file)
|
fmt.Printf("Successfully uploaded %s artifact\n", file)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
5
types.go
5
types.go
@ -1,8 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
// 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 []string `json:"files"`
|
Files []string `json:"files"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user