From 4d04a5e239c30f5f68ef9f21989f98f06a4ff76e Mon Sep 17 00:00:00 2001 From: chmanie Date: Mon, 12 Jun 2023 19:07:41 +0200 Subject: [PATCH] Add possibility to add custom string to filename --- action.yml | 3 +++ main.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index e2edd01..6e443d3 100644 --- a/action.yml +++ b/action.yml @@ -19,6 +19,9 @@ inputs: insecure: description: 'Whether allow insecure Gitea instance' required: false + file_key: + description: 'An extra key that gets inserted into your filename (before the extension)' + required: false outputs: status: description: 'The upload status' diff --git a/main.go b/main.go index 93d3370..eb92f4a 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { preRelease, _ := strconv.ParseBool(gha.GetInput("pre_release")) draft, _ := strconv.ParseBool(gha.GetInput("draft")) if title == "" { - title = ctx.RefName + title = strings.Replace(ctx.RefName, "refs/tags/", "", 1) } if apiKey == "" { apiKey = os.Getenv("GITHUB_TOKEN") @@ -148,6 +148,14 @@ func createOrGetRelease(ctx *gha.GitHubContext, c *gitea.Client, owner, repo str return release, nil } +func getFilename(ctx *gha.GitHubContext, path string) string { + fileKey := gha.GetInput("file_key") + fileName := filepath.Base(path) + ext := filepath.Ext(fileName) + filePath := strings.TrimSuffix(fileName, ext) + return filePath + fileKey + ext +} + 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 { @@ -171,7 +179,7 @@ func uploadFiles(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, re } } - if _, _, err = c.CreateReleaseAttachment(owner, repo, releaseID, f, filepath.Base(file)); err != nil { + if _, _, err = c.CreateReleaseAttachment(owner, repo, releaseID, f, getFilename(ctx, file)); err != nil { f.Close() return fmt.Errorf("failed to upload release attachment %s: %w", file, err) }