2016-08-26 08:38:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-28 01:55:21 +00:00
|
|
|
"crypto/tls"
|
2016-08-26 08:38:22 +00:00
|
|
|
"fmt"
|
2018-03-28 01:55:21 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"os"
|
2016-08-26 08:38:22 +00:00
|
|
|
"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
|
2018-03-03 11:09:01 +00:00
|
|
|
PreRelease bool
|
2018-03-28 01:55:21 +00:00
|
|
|
Insecure bool
|
2016-08-26 08:38:22 +00:00
|
|
|
BaseURL string
|
2018-03-03 11:09:01 +00:00
|
|
|
Title string
|
|
|
|
Note string
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-03 11:09:01 +00:00
|
|
|
var err error
|
|
|
|
if p.Config.Note != "" {
|
|
|
|
if p.Config.Note, err = readStringOrFile(p.Config.Note); err != nil {
|
|
|
|
return fmt.Errorf("error while reading %s: %v", p.Config.Note, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Config.Title != "" {
|
|
|
|
if p.Config.Title, err = readStringOrFile(p.Config.Title); err != nil {
|
|
|
|
return fmt.Errorf("error while reading %s: %v", p.Config.Note, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 06:01:14 +00:00
|
|
|
httpClient := &http.Client{}
|
2018-03-28 01:55:21 +00:00
|
|
|
if p.Config.Insecure {
|
|
|
|
cookieJar, _ := cookiejar.New(nil)
|
2020-09-16 06:01:14 +00:00
|
|
|
httpClient = &http.Client{
|
2018-03-28 01:55:21 +00:00
|
|
|
Jar: cookieJar,
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
}
|
2020-09-16 06:01:14 +00:00
|
|
|
}
|
|
|
|
client, err := gitea.NewClient(p.Config.BaseURL, gitea.SetToken(p.Config.APIKey), gitea.SetHTTPClient(httpClient))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-28 01:55:21 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2018-03-03 11:09:01 +00:00
|
|
|
Prerelease: p.Config.PreRelease,
|
2016-08-26 08:38:22 +00:00
|
|
|
FileExists: p.Config.FileExists,
|
2018-03-03 11:09:01 +00:00
|
|
|
Title: p.Config.Title,
|
|
|
|
Note: p.Config.Note,
|
2016-08-26 08:38:22 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 06:31:25 +00:00
|
|
|
// if the title was not provided via .drone.yml we use the tag instead
|
|
|
|
// fixes https://github.com/drone-plugins/drone-gitea-release/issues/26
|
|
|
|
if rc.Title == "" {
|
|
|
|
rc.Title = rc.Tag
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:38:22 +00:00
|
|
|
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
|
|
|
|
}
|
2018-03-03 11:09:01 +00:00
|
|
|
|
|
|
|
func readStringOrFile(input string) (string, error) {
|
|
|
|
// Check if input is a file path
|
|
|
|
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
|
|
|
|
// No file found => use input as result
|
|
|
|
return input, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
result, err := ioutil.ReadFile(input)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(result), nil
|
|
|
|
}
|