-
Notifications
You must be signed in to change notification settings - Fork 21
/
plugin.go
171 lines (142 loc) · 3.34 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/cookiejar"
"os"
"path/filepath"
"strings"
"code.gitea.io/sdk/gitea"
)
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
PreRelease bool
Insecure bool
BaseURL string
Title string
Note string
}
Plugin struct {
Repo Repo
Build Build
Commit Commit
Config Config
}
)
func (p Plugin) Exec() error {
var (
files []string
)
if p.Build.Event != "tag" {
return fmt.Errorf("The Gitea Release plugin is only available for tags")
}
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")
}
if p.Config.BaseURL == "" {
return fmt.Errorf("You must provide a base url.")
}
if !strings.HasSuffix(p.Config.BaseURL, "/") {
p.Config.BaseURL = p.Config.BaseURL + "/"
}
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)
}
}
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)
}
}
httpClient := &http.Client{}
if p.Config.Insecure {
cookieJar, _ := cookiejar.New(nil)
httpClient = &http.Client{
Jar: cookieJar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
client, err := gitea.NewClient(p.Config.BaseURL, gitea.SetToken(p.Config.APIKey), gitea.SetHTTPClient(httpClient))
if err != nil {
return err
}
rc := releaseClient{
Client: client,
Owner: p.Repo.Owner,
Repo: p.Repo.Name,
Tag: strings.TrimPrefix(p.Commit.Ref, "refs/tags/"),
Draft: p.Config.Draft,
Prerelease: p.Config.PreRelease,
FileExists: p.Config.FileExists,
Title: p.Config.Title,
Note: p.Config.Note,
}
// 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
}
release, err := rc.buildRelease()
if err != nil {
return fmt.Errorf("Failed to create the release. %s", err)
}
if err := rc.uploadFiles(release.ID, files); err != nil {
return fmt.Errorf("Failed to upload the files. %s", err)
}
return nil
}
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 := os.ReadFile(input)
if err != nil {
return "", err
}
return string(result), nil
}