2023-04-16 03:02:22 -04:00
|
|
|
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 {
|
2023-05-15 01:49:11 -04:00
|
|
|
gha.Fatalf("failed to get context: %v", err)
|
2023-04-16 03:02:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(ctx.Ref, "refs/tags/") {
|
|
|
|
gha.Fatalf("ref %s is not a tag", ctx.Ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
files := gha.GetInput("files")
|
2023-10-16 21:14:07 -04:00
|
|
|
title := strings.Replace(gha.GetInput("title"), "refs/tags/", "", 1)
|
|
|
|
body := strings.Replace(gha.GetInput("body"), "refs/tags/", "", 1)
|
2023-04-16 03:02:22 -04:00
|
|
|
apiKey := gha.GetInput("api_key")
|
|
|
|
preRelease, _ := strconv.ParseBool(gha.GetInput("pre_release"))
|
|
|
|
draft, _ := strconv.ParseBool(gha.GetInput("draft"))
|
2023-10-16 21:06:51 -04:00
|
|
|
tagName := strings.Replace(ctx.RefName, "refs/tags/", "", 1)
|
2023-04-16 03:02:22 -04:00
|
|
|
if title == "" {
|
2023-10-16 21:06:51 -04:00
|
|
|
title = tagName
|
2023-04-16 03:02:22 -04:00
|
|
|
}
|
|
|
|
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]
|
|
|
|
|
2023-05-04 04:35:41 -04:00
|
|
|
rel, err := createOrGetRelease(ctx, c, owner, repo, gitea.CreateReleaseOption{
|
2023-10-16 21:06:51 -04:00
|
|
|
TagName: tagName,
|
2023-04-16 03:02:22 -04:00
|
|
|
IsDraft: draft,
|
|
|
|
IsPrerelease: preRelease,
|
|
|
|
Title: title,
|
2023-04-16 10:19:30 -04:00
|
|
|
Target: ctx.SHA,
|
2023-07-06 22:36:39 -04:00
|
|
|
Note: body,
|
2023-04-16 03:02:22 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-05-15 01:49:11 -04:00
|
|
|
gha.Fatalf("failed to create release: %v", err)
|
2023-04-16 03:02:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-05-15 01:49:11 -04:00
|
|
|
gha.Fatalf("Failed to upload files: %v", err)
|
2023-04-16 03:02:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-04 04:35:41 -04:00
|
|
|
func createOrGetRelease(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, opts gitea.CreateReleaseOption) (*gitea.Release, error) {
|
|
|
|
// Get the release by tag
|
2023-05-04 09:02:03 -04:00
|
|
|
release, resp, err := c.GetReleaseByTag(owner, repo, opts.TagName)
|
2023-05-04 04:35:41 -04:00
|
|
|
if err == nil {
|
|
|
|
return release, nil
|
|
|
|
}
|
|
|
|
errMessage := fmt.Errorf("failed to get release for tag: %s with error: %w", opts.TagName, err)
|
2023-05-04 09:02:03 -04:00
|
|
|
if resp.StatusCode != 404 {
|
|
|
|
return nil, errMessage
|
|
|
|
}
|
2023-05-04 13:52:51 -04:00
|
|
|
fmt.Printf("%s trying to create it\n", errMessage)
|
2023-04-16 03:02:22 -04:00
|
|
|
// Create the release
|
2023-05-04 04:35:41 -04:00
|
|
|
release, _, err = c.CreateRelease(owner, repo, opts)
|
2023-04-16 03:02:22 -04:00
|
|
|
if err != nil {
|
2023-05-04 04:35:41 -04:00
|
|
|
return nil, fmt.Errorf("failed to create release: %w and %s", err, errMessage)
|
2023-04-16 03:02:22 -04:00
|
|
|
}
|
|
|
|
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 {
|
2023-05-04 04:32:01 -04:00
|
|
|
if attachment.Name == filepath.Base(file) {
|
2023-04-16 03:02:22 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 04:32:01 -04:00
|
|
|
if _, _, err = c.CreateReleaseAttachment(owner, repo, releaseID, f, filepath.Base(file)); err != nil {
|
2023-04-16 03:02:22 -04:00
|
|
|
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
|
|
|
|
}
|