-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (163 loc) · 4.69 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
gha "github.com/sethvargo/go-githubactions"
)
func main() {
ctx, err := gha.Context()
if err != nil {
gha.Fatalf("failed to get context: %v", err)
}
//if !strings.HasPrefix(ctx.Ref, "refs/tags/") {
// gha.Fatalf("ref %s is not a tag", ctx.Ref)
//}
files := gha.GetInput("files")
title := gha.GetInput("title")
body := gha.GetInput("body")
apiKey := gha.GetInput("api_key")
preRelease, _ := strconv.ParseBool(gha.GetInput("pre_release"))
draft, _ := strconv.ParseBool(gha.GetInput("draft"))
if title == "" {
title = ctx.RefName
}
if apiKey == "" {
apiKey = os.Getenv("GITHUB_TOKEN")
}
insecure, _ := strconv.ParseBool(gha.GetInput("insecure"))
client := http.DefaultClient
if insecure {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
}
c, err := gitea.NewClient(ctx.ServerURL, gitea.SetToken(apiKey), gitea.SetHTTPClient(client))
if err != nil {
gha.Fatalf("failed to create gitea client: %v", err)
}
owner := ctx.RepositoryOwner
repo := strings.Split(ctx.Repository, "/")[1]
rel, err := createOrGetRelease(ctx, c, owner, repo, gitea.CreateReleaseOption{
TagName: ctx.RefName,
IsDraft: draft,
IsPrerelease: preRelease,
Title: title,
Target: ctx.SHA,
Note: body,
})
if err != nil {
gha.Fatalf("failed to create release: %v", err)
}
matchedFiles, err := getFiles(ctx.Workspace, files)
if err != nil {
gha.Fatalf("failed to get files: %v", err)
}
if err := uploadFiles(ctx, c, owner, repo, rel.ID, matchedFiles); err != nil {
gha.Fatalf("Failed to upload files: %v", err)
}
gha.SetOutput("status", "success")
}
func getDirFiles(dir string) ([]string, error) {
d, err := os.Open(dir)
if err != nil {
return nil, err
}
defer d.Close()
info, err := d.Stat()
if err != nil {
return nil, err
}
if !info.IsDir() {
return []string{dir}, nil
}
list, err := d.Readdirnames(0)
if err != nil {
return nil, err
}
res := make([]string, 0, len(list))
for _, f := range list {
subs, err := getDirFiles(filepath.Join(dir, f))
if err != nil {
return nil, err
}
res = append(res, subs...)
}
return res, nil
}
func getFiles(parentDir, files string) ([]string, error) {
var fileList []string
lines := strings.Split(files, "\n")
for _, line := range lines {
line = strings.Trim(line, "'")
line = strings.Trim(line, `"`)
if filepath.IsAbs(line) {
return nil, fmt.Errorf("file path %s is absolute", line)
}
line = filepath.Join(parentDir, line)
matches, err := filepath.Glob(line)
if err != nil {
return nil, err
}
for _, match := range matches {
files, err := getDirFiles(match)
if err != nil {
return nil, err
}
fileList = append(fileList, files...)
}
}
return fileList, nil
}
func createOrGetRelease(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, opts gitea.CreateReleaseOption) (*gitea.Release, error) {
// Get the release by tag
release, resp, err := c.GetReleaseByTag(owner, repo, opts.TagName)
if err == nil {
return release, nil
}
errMessage := fmt.Errorf("failed to get release for tag: %s with error: %w", opts.TagName, err)
if resp.StatusCode != 404 {
return nil, errMessage
}
fmt.Printf("%s trying to create it\n", errMessage)
// Create the release
release, _, err = c.CreateRelease(owner, repo, opts)
if err != nil {
return nil, fmt.Errorf("failed to create release: %w and %s", err, errMessage)
}
return release, nil
}
func uploadFiles(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, releaseID int64, files []string) error {
attachments, _, err := c.ListReleaseAttachments(owner, repo, releaseID, gitea.ListReleaseAttachmentsOptions{})
if err != nil {
return fmt.Errorf("failed to fetch existing release attachments: %w", err)
}
for _, file := range files {
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to open release attachment %s: %w", file, err)
}
for _, attachment := range attachments {
if attachment.Name == filepath.Base(file) {
if _, err := c.DeleteReleaseAttachment(owner, repo, releaseID, attachment.ID); err != nil {
f.Close()
return fmt.Errorf("failed to delete release attachment %s: %w", file, err)
}
fmt.Printf("Successfully deleted old release attachment %s\n", attachment.Name)
}
}
if _, _, err = c.CreateReleaseAttachment(owner, repo, releaseID, f, filepath.Base(file)); err != nil {
f.Close()
return fmt.Errorf("failed to upload release attachment %s: %w", file, err)
}
f.Close()
fmt.Printf("Successfully uploaded release attachment %s\n", file)
}
return nil
}