Merge pull request 'feat: add getRelease before createRelease to allow adding to existing release' (#2) from myrqs/release-action:add-attachments into main

Reviewed-on: https://gitea.com/actions/release-action/pulls/2
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Lunny Xiao 2023-05-05 10:42:22 +08:00
commit 489f83257d

23
main.go
View file

@ -52,7 +52,7 @@ func main() {
owner := ctx.RepositoryOwner
repo := strings.Split(ctx.Repository, "/")[1]
rel, err := createRelease(ctx, c, owner, repo, gitea.CreateReleaseOption{
rel, err := createOrGetRelease(ctx, c, owner, repo, gitea.CreateReleaseOption{
TagName: ctx.RefName,
IsDraft: draft,
IsPrerelease: preRelease,
@ -129,13 +129,22 @@ func getFiles(parentDir, files string) ([]string, error) {
return fileList, nil
}
func createRelease(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, opts gitea.CreateReleaseOption) (*gitea.Release, error) {
// Create the release
release, _, err := c.CreateRelease(owner, repo, opts)
if err != nil {
return nil, fmt.Errorf("failed to create release: %w", err)
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
}