2016-08-26 08:38:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2016-08-26 08:38:22 +00:00
|
|
|
)
|
|
|
|
|
2018-03-02 22:52:28 +00:00
|
|
|
// Release holds ties the drone env data and gitea client together.
|
2016-08-26 08:38:22 +00:00
|
|
|
type releaseClient struct {
|
2018-03-02 22:41:45 +00:00
|
|
|
*gitea.Client
|
2016-08-26 08:38:22 +00:00
|
|
|
Owner string
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Draft bool
|
2017-03-13 23:07:52 +00:00
|
|
|
Prerelease bool
|
2016-08-26 08:38:22 +00:00
|
|
|
FileExists string
|
2018-03-03 11:09:01 +00:00
|
|
|
Title string
|
|
|
|
Note string
|
2020-05-08 00:24:56 +00:00
|
|
|
Event string
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
func (rc *releaseClient) buildRelease() (*gitea.Release, error) {
|
2016-08-26 08:38:22 +00:00
|
|
|
// first attempt to get a release by that tag
|
|
|
|
release, err := rc.getRelease()
|
|
|
|
|
|
|
|
if err != nil && release == nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else if release != nil {
|
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no release was found by that tag, create a new one
|
|
|
|
release, err = rc.newRelease()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to retrieve or create a release: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
func (rc *releaseClient) getRelease() (*gitea.Release, error) {
|
|
|
|
releases, err := rc.Client.ListReleases(rc.Owner, rc.Repo)
|
2016-08-26 08:38:22 +00:00
|
|
|
if err != nil {
|
2018-03-02 22:41:45 +00:00
|
|
|
return nil, err
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:24:56 +00:00
|
|
|
var r *gitea.Release
|
2018-03-02 22:41:45 +00:00
|
|
|
for _, release := range releases {
|
|
|
|
if release.TagName == rc.Tag {
|
|
|
|
fmt.Printf("Successfully retrieved %s release\n", rc.Tag)
|
2020-05-08 00:24:56 +00:00
|
|
|
r = release
|
2018-03-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-08 00:24:56 +00:00
|
|
|
|
|
|
|
if r == nil {
|
|
|
|
return nil, fmt.Errorf("Release %s not found", rc.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.Event != "tag" {
|
|
|
|
if err := rc.Client.DeleteRelease(rc.Owner, rc.Repo, r.ID); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to delete a release: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Successfully deleted %s release", rc.Tag)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
func (rc *releaseClient) newRelease() (*gitea.Release, error) {
|
|
|
|
r := gitea.CreateReleaseOption{
|
2018-03-03 11:09:01 +00:00
|
|
|
TagName: rc.Tag,
|
2018-03-02 22:41:45 +00:00
|
|
|
IsDraft: rc.Draft,
|
|
|
|
IsPrerelease: rc.Prerelease,
|
2018-03-03 11:09:01 +00:00
|
|
|
Title: rc.Title,
|
|
|
|
Note: rc.Note,
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
release, err := rc.Client.CreateRelease(rc.Owner, rc.Repo, r)
|
2016-08-26 08:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create release: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Successfully created %s release\n", rc.Tag)
|
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
func (rc *releaseClient) uploadFiles(releaseID int64, files []string) error {
|
|
|
|
attachments, err := rc.Client.ListReleaseAttachments(rc.Owner, rc.Repo, releaseID)
|
2016-08-26 08:38:22 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to fetch existing assets: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploadFiles []string
|
|
|
|
|
|
|
|
files:
|
|
|
|
for _, file := range files {
|
2018-03-02 22:41:45 +00:00
|
|
|
for _, attachment := range attachments {
|
|
|
|
if attachment.Name == path.Base(file) {
|
2016-08-26 08:38:22 +00:00
|
|
|
switch rc.FileExists {
|
|
|
|
case "overwrite":
|
|
|
|
// do nothing
|
|
|
|
case "fail":
|
|
|
|
return fmt.Errorf("Asset file %s already exists", path.Base(file))
|
|
|
|
case "skip":
|
2018-03-02 22:41:45 +00:00
|
|
|
fmt.Printf("Skipping pre-existing %s artifact\n", attachment.Name)
|
2016-08-26 08:38:22 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to read %s artifact: %s", file, err)
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
for _, attachment := range attachments {
|
|
|
|
if attachment.Name == path.Base(file) {
|
|
|
|
if err := rc.Client.DeleteReleaseAttachment(rc.Owner, rc.Repo, releaseID, attachment.ID); err != nil {
|
2016-08-26 08:38:22 +00:00
|
|
|
return fmt.Errorf("Failed to delete %s artifact: %s", file, err)
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
fmt.Printf("Successfully deleted old %s artifact\n", attachment.Name)
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
if _, err = rc.Client.CreateReleaseAttachment(rc.Owner, rc.Repo, releaseID, handle, path.Base(file)); err != nil {
|
2016-08-26 08:38:22 +00:00
|
|
|
return fmt.Errorf("Failed to upload %s artifact: %s", file, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Successfully uploaded %s artifact\n", file)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|