0
0
mirror of https://github.com/thegeeklab/wp-gitea-release.git synced 2024-11-24 23:10:39 +00:00
wp-gitea-release/plugin/release.go

144 lines
3.3 KiB
Go
Raw Normal View History

2024-01-04 22:26:33 +00:00
package plugin
import (
2024-01-04 22:26:33 +00:00
"errors"
"fmt"
"os"
"path"
"code.gitea.io/sdk/gitea"
2024-01-04 22:26:33 +00:00
"github.com/rs/zerolog/log"
)
var (
ErrReleaseNotFound = errors.New("release not found")
ErrFileExists = errors.New("asset file already exist")
)
2024-01-04 22:47:02 +00:00
// Release holds ties the Woodpecker env data and gitea client together.
type releaseClient struct {
*gitea.Client
Owner string
Repo string
Tag string
Draft bool
2017-03-13 23:07:52 +00:00
Prerelease bool
FileExists string
Title string
Note string
}
func (rc *releaseClient) buildRelease() (*gitea.Release, error) {
// 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 {
2024-01-04 22:26:33 +00:00
return nil, fmt.Errorf("failed to retrieve or create a release: %w", err)
}
return release, nil
}
func (rc *releaseClient) getRelease() (*gitea.Release, error) {
2020-09-16 06:01:14 +00:00
releases, _, err := rc.Client.ListReleases(rc.Owner, rc.Repo, gitea.ListReleasesOptions{})
if err != nil {
return nil, err
}
for _, release := range releases {
if release.TagName == rc.Tag {
2024-01-04 22:26:33 +00:00
log.Info().Msgf("successfully retrieved %s release", rc.Tag)
return release, nil
}
}
2024-01-04 22:26:33 +00:00
return nil, fmt.Errorf("%w: %s", ErrReleaseNotFound, rc.Tag)
}
func (rc *releaseClient) newRelease() (*gitea.Release, error) {
r := gitea.CreateReleaseOption{
TagName: rc.Tag,
IsDraft: rc.Draft,
IsPrerelease: rc.Prerelease,
Title: rc.Title,
Note: rc.Note,
}
2020-09-16 06:01:14 +00:00
release, _, err := rc.Client.CreateRelease(rc.Owner, rc.Repo, r)
if err != nil {
2024-01-04 22:26:33 +00:00
return nil, fmt.Errorf("failed to create release: %w", err)
}
2024-01-04 22:26:33 +00:00
log.Info().Msgf("successfully created %s release", rc.Tag)
return release, nil
}
func (rc *releaseClient) uploadFiles(releaseID int64, files []string) error {
2024-01-04 22:26:33 +00:00
attachments, _, err := rc.Client.ListReleaseAttachments(
rc.Owner,
rc.Repo,
releaseID,
gitea.ListReleaseAttachmentsOptions{},
)
if err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("failed to fetch existing assets: %w", err)
}
var uploadFiles []string
files:
for _, file := range files {
for _, attachment := range attachments {
if attachment.Name == path.Base(file) {
switch rc.FileExists {
case "overwrite":
// do nothing
case "fail":
2024-01-04 22:26:33 +00:00
return fmt.Errorf("%w: %s", ErrFileExists, path.Base(file))
case "skip":
2024-01-04 22:26:33 +00:00
log.Warn().Msgf("skipping pre-existing %s artifact", attachment.Name)
continue files
}
}
}
uploadFiles = append(uploadFiles, file)
}
for _, file := range uploadFiles {
handle, err := os.Open(file)
if err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("failed to read %s artifact: %w", file, err)
}
for _, attachment := range attachments {
if attachment.Name == path.Base(file) {
2020-09-16 06:01:14 +00:00
if _, err := rc.Client.DeleteReleaseAttachment(rc.Owner, rc.Repo, releaseID, attachment.ID); err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("failed to delete %s artifact: %w", file, err)
}
2024-01-04 22:26:33 +00:00
log.Info().Msgf("successfully deleted old %s artifact", attachment.Name)
}
}
2020-09-16 06:01:14 +00:00
if _, _, err = rc.Client.CreateReleaseAttachment(rc.Owner, rc.Repo, releaseID, handle, path.Base(file)); err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("failed to upload %s artifact: %w", file, err)
}
2024-01-04 22:26:33 +00:00
log.Info().Msgf("successfully uploaded %s artifact", file)
}
return nil
}