0
0
mirror of https://github.com/thegeeklab/wp-gitea-release.git synced 2024-06-03 04:39:45 +02:00

use helper methods to improve readability of AddAttachments

This commit is contained in:
Robert Kaussow 2024-05-08 12:25:11 +02:00
parent 5c1163efb0
commit 168de3404a
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
2 changed files with 61 additions and 36 deletions

View File

@ -107,51 +107,75 @@ func (r *GiteaRelease) AddAttachments(releaseID int64, files []string) error {
return fmt.Errorf("failed to fetch attachments: %w", err)
}
var uploadFiles []string
files:
for _, file := range files {
for _, attachment := range attachments {
if attachment.Name == path.Base(file) {
switch r.Opt.FileExists {
case "overwrite":
// do nothing
case "fail":
return fmt.Errorf("%w: %s", ErrFileExists, path.Base(file))
case "skip":
log.Warn().Msgf("skip existing artifact: %s", path.Base(file))
continue files
}
}
}
uploadFiles = append(uploadFiles, file)
existingAttachments := make(map[string]bool)
for _, attachment := range attachments {
existingAttachments[attachment.Name] = true
}
for _, file := range uploadFiles {
handle, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to read artifact: %s: %w", file, err)
}
for _, attachment := range attachments {
if attachment.Name == path.Base(file) {
if _, err := r.client.DeleteReleaseAttachment(r.Opt.Owner, r.Opt.Repo, releaseID, attachment.ID); err != nil {
return fmt.Errorf("failed to delete artifact: %s: %w", file, err)
for _, file := range files {
fileName := path.Base(file)
if existingAttachments[fileName] {
switch r.Opt.FileExists {
case "overwrite":
if err := r.deleteAttachment(releaseID, fileName); err != nil {
return err
}
case "fail":
return fmt.Errorf("%w: %s", ErrFileExists, fileName)
case "skip":
log.Warn().Msgf("skip existing artifact: %s", fileName)
log.Info().Msgf("deleted artifact: %s", attachment.Name)
continue
}
}
_, _, err = r.client.CreateReleaseAttachment(r.Opt.Owner, r.Opt.Repo, releaseID, handle, path.Base(file))
if err != nil {
return fmt.Errorf("failed to upload artifact: %s: %w", file, err)
if err := r.uploadFile(releaseID, file); err != nil {
return err
}
log.Info().Msgf("uploaded artifact: %s", path.Base(file))
}
return nil
}
func (r *GiteaRelease) deleteAttachment(releaseID int64, fileName string) error {
attachments, _, err := r.client.ListReleaseAttachments(
r.Opt.Owner,
r.Opt.Repo,
releaseID,
gitea.ListReleaseAttachmentsOptions{},
)
if err != nil {
return fmt.Errorf("failed to fetch attachments: %w", err)
}
for _, attachment := range attachments {
if attachment.Name == fileName {
if _, err := r.client.DeleteReleaseAttachment(r.Opt.Owner, r.Opt.Repo, releaseID, attachment.ID); err != nil {
return fmt.Errorf("failed to delete artifact: %s: %w", fileName, err)
}
log.Info().Msgf("deleted artifact: %s", fileName)
return nil
}
}
return nil
}
func (r *GiteaRelease) uploadFile(releaseID int64, file string) error {
handle, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to read artifact: %s: %w", file, err)
}
defer handle.Close()
_, _, err = r.client.CreateReleaseAttachment(r.Opt.Owner, r.Opt.Repo, releaseID, handle, path.Base(file))
if err != nil {
return fmt.Errorf("failed to upload artifact: %s: %w", file, err)
}
log.Info().Msgf("uploaded artifact: %s", path.Base(file))
return nil
}

View File

@ -319,6 +319,7 @@ func TestGiteaReleaseAddAttachments(t *testing.T) {
defer ts.Close()
logBuffer.Reset()
g, _ := gitea.NewClient(ts.URL)
client := NewGiteaClient(g)