feat: add getRelease before createRelease to allow adding to existing release

This commit is contained in:
myrqs 2023-05-04 16:35:41 +08:00
parent da7d4a02be
commit e5d338f4c5

20
main.go

@ -53,7 +53,7 @@ func main() {
owner := ctx.RepositoryOwner owner := ctx.RepositoryOwner
repo := strings.Split(ctx.Repository, "/")[1] 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, TagName: ctx.RefName,
IsDraft: draft, IsDraft: draft,
IsPrerelease: preRelease, IsPrerelease: preRelease,
@ -130,13 +130,19 @@ func getFiles(parentDir, files string) ([]string, error) {
return fileList, nil return fileList, nil
} }
func createRelease(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, opts gitea.CreateReleaseOption) (*gitea.Release, error) { func createOrGetRelease(ctx *gha.GitHubContext, c *gitea.Client, owner, repo string, opts gitea.CreateReleaseOption) (*gitea.Release, error) {
// Create the release // Get the release by tag
release, _, err := c.CreateRelease(owner, repo, opts) release, _, err := c.GetReleaseByTag(owner, repo, opts.TagName)
if err != nil { if err == nil {
return nil, fmt.Errorf("failed to create release: %w", err) return release, nil
}
errMessage := fmt.Errorf("failed to get release for tag: %s with error: %w", opts.TagName, err)
fmt.Printf("%s trying to create it", 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 return release, nil
} }