0
0
mirror of https://github.com/thegeeklab/wp-gitea-release.git synced 2024-11-09 17:30:39 +00:00
wp-gitea-release/plugin/impl.go

149 lines
3.2 KiB
Go
Raw Normal View History

2024-01-04 22:26:33 +00:00
package plugin
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path/filepath"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/thegeeklab/wp-plugin-go/v2/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 = 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 = 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 {
httpClient := &http.Client{}
client, err := gitea.NewClient(
p.Settings.baseURL.String(),
gitea.SetToken(p.Settings.APIKey),
gitea.SetHTTPClient(httpClient),
)
if err != nil {
return err
}
r := &Release{
2024-01-04 22:26:33 +00:00
Client: client,
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 := r.buildRelease()
2024-01-04 22:26:33 +00:00
if err != nil {
return fmt.Errorf("failed to create the release: %w", err)
}
if err := r.uploadFiles(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
}