2016-08-26 08:38:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2016-08-26 08:38:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Repo struct {
|
|
|
|
Owner string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
Build struct {
|
|
|
|
Event string
|
|
|
|
}
|
|
|
|
|
|
|
|
Commit struct {
|
|
|
|
Ref string
|
|
|
|
}
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
APIKey string
|
|
|
|
Files []string
|
|
|
|
FileExists string
|
|
|
|
Checksum []string
|
|
|
|
Draft bool
|
2017-03-13 23:07:52 +00:00
|
|
|
Prerelease bool
|
2016-08-26 08:38:22 +00:00
|
|
|
BaseURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
Plugin struct {
|
|
|
|
Repo Repo
|
|
|
|
Build Build
|
|
|
|
Commit Commit
|
|
|
|
Config Config
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p Plugin) Exec() error {
|
|
|
|
var (
|
|
|
|
files []string
|
|
|
|
)
|
|
|
|
|
|
|
|
if p.Build.Event != "tag" {
|
2018-03-02 22:41:45 +00:00
|
|
|
return fmt.Errorf("The Gitea Release plugin is only available for tags")
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.Config.APIKey == "" {
|
|
|
|
return fmt.Errorf("You must provide an API key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fileExistsValues[p.Config.FileExists] {
|
|
|
|
return fmt.Errorf("Invalid value for file_exists")
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
if p.Config.BaseURL == "" {
|
|
|
|
return fmt.Errorf("You must provide a base url.")
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
if !strings.HasSuffix(p.Config.BaseURL, "/") {
|
|
|
|
p.Config.BaseURL = p.Config.BaseURL + "/"
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, glob := range p.Config.Files {
|
|
|
|
globed, err := filepath.Glob(glob)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to glob %s. %s", glob, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if globed != nil {
|
|
|
|
files = append(files, globed...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.Config.Checksum) > 0 {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
files, err = writeChecksums(files, p.Config.Checksum)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to write checksums. %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
client := gitea.NewClient(p.Config.BaseURL, p.Config.APIKey)
|
2016-08-26 08:38:22 +00:00
|
|
|
|
|
|
|
rc := releaseClient{
|
|
|
|
Client: client,
|
|
|
|
Owner: p.Repo.Owner,
|
|
|
|
Repo: p.Repo.Name,
|
2017-05-22 14:44:49 +00:00
|
|
|
Tag: strings.TrimPrefix(p.Commit.Ref, "refs/tags/"),
|
2016-08-26 08:38:22 +00:00
|
|
|
Draft: p.Config.Draft,
|
2017-03-13 23:07:52 +00:00
|
|
|
Prerelease: p.Config.Prerelease,
|
2016-08-26 08:38:22 +00:00
|
|
|
FileExists: p.Config.FileExists,
|
|
|
|
}
|
|
|
|
|
|
|
|
release, err := rc.buildRelease()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create the release. %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:41:45 +00:00
|
|
|
if err := rc.uploadFiles(release.ID, files); err != nil {
|
2016-08-26 08:38:22 +00:00
|
|
|
return fmt.Errorf("Failed to upload the files. %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|