0
0
mirror of https://github.com/thegeeklab/wp-gitea-release.git synced 2024-11-27 12:00:36 +00:00
wp-gitea-release/plugin/impl.go

149 lines
3.5 KiB
Go
Raw Normal View History

2024-01-04 22:26:33 +00:00
package plugin
import (
"context"
"errors"
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/thegeeklab/wp-gitea-release/gitea"
plugin_file "github.com/thegeeklab/wp-plugin-go/v3/file"
2024-01-04 22:26:33 +00:00
)
var (
ErrPluginEventNotSupported = errors.New("event not supported")
ErrFileExistInvalid = errors.New("invalid file_exist value")
)
//nolint:revive
func (p *Plugin) run(ctx context.Context) error {
if err := p.FlagsFromContext(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
if err := p.Validate(); err != nil {
return fmt.Errorf("validation failed: %w", err)
}
if err := p.Execute(); err != nil {
return fmt.Errorf("execution failed: %w", err)
}
return nil
}
// Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error {
var err error
fileExistsValues := map[string]bool{
"overwrite": true,
"fail": true,
"skip": true,
}
if p.Settings.Event != "tag" {
return fmt.Errorf("%w: %s", ErrPluginEventNotSupported, p.Metadata.Pipeline.Event)
}
if !fileExistsValues[p.Settings.FileExists] {
return ErrFileExistInvalid
}
if p.Settings.Note != "" {
if p.Settings.Note, _, err = plugin_file.ReadStringOrFile(p.Settings.Note); err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("error while reading %s: %w", p.Settings.Note, err)
}
}
if p.Settings.Title != "" {
if p.Settings.Title, _, err = plugin_file.ReadStringOrFile(p.Settings.Title); err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("error while reading %s: %w", p.Settings.Title, err)
}
}
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
client, err := gitea.NewClient(p.Settings.baseURL.String(), p.Settings.APIKey, p.Network.Client)
2024-01-04 22:26:33 +00:00
if err != nil {
return fmt.Errorf("failed to create Gitea client: %w", err)
2024-01-04 22:26:33 +00:00
}
2024-05-11 22:03:07 +00:00
client.Release.Opt = gitea.ReleaseOptions{
2024-01-04 22:47:02 +00:00
Owner: p.Metadata.Repository.Owner,
Repo: p.Metadata.Repository.Name,
2024-01-04 22:26:33 +00:00
Tag: strings.TrimPrefix(p.Settings.CommitRef, "refs/tags/"),
Draft: p.Settings.Draft,
Prerelease: p.Settings.PreRelease,
FileExists: p.Settings.FileExists,
Title: p.Settings.Title,
Note: p.Settings.Note,
}
release, err := client.Release.Find()
if err != nil && !errors.Is(err, gitea.ErrReleaseNotFound) {
return fmt.Errorf("failed to retrieve release: %w", err)
}
// If no release was found by that tag, create a new one.
if release == nil {
release, err = client.Release.Create()
if err != nil {
return fmt.Errorf("failed to create release: %w", err)
}
2024-01-04 22:26:33 +00:00
}
if err := client.Release.AddAttachments(release.ID, p.Settings.files); err != nil {
2024-01-04 22:26:33 +00:00
return fmt.Errorf("failed to upload the files: %w", err)
}
return nil
}
func (p *Plugin) FlagsFromContext() error {
var err error
baseURL := p.Context.String("base-url")
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
p.Settings.baseURL, err = url.Parse(baseURL)
if err != nil {
return fmt.Errorf("failed to parse base url: %w", err)
}
var files []string
rawFiles := p.Context.StringSlice("files")
for _, glob := range rawFiles {
globed, err := filepath.Glob(glob)
if err != nil {
return fmt.Errorf("failed to glob %s: %w", glob, err)
}
if globed != nil {
files = append(files, globed...)
}
}
if len(p.Settings.Checksum.Value()) > 0 {
var err error
files, err = WriteChecksums(files, p.Settings.Checksum.Value(), "")
2024-01-04 22:26:33 +00:00
if err != nil {
return fmt.Errorf("failed to write checksums: %w", err)
}
}
p.Settings.files = files
return nil
}