1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 19:51:58 +01:00

Add attachment removal and change message (#36)

Add attachment removal and change message

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/36
This commit is contained in:
John Olheiser 2020-09-21 14:19:36 +00:00
parent cce2360f0f
commit 0bbe2e35ff
3 changed files with 108 additions and 3 deletions

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"gitea.com/jolheiser/sip/flag"
@ -20,6 +21,9 @@ var ReleaseAttach = cli.Command{
Name: "attach",
Usage: "Attach files to a release",
Action: doReleaseAttach,
Subcommands: []*cli.Command{
&ReleaseAttachRemove,
},
}
func doReleaseAttach(_ *cli.Context) error {
@ -71,7 +75,7 @@ func doReleaseAttach(_ *cli.Context) error {
info := color.Info
cyan := color.New(color.FgCyan)
fmt.Println(info.Format("Release"), cyan.Format(release.TagName), info.Format("updated!"))
fmt.Println(info.Format("Added"), cyan.Format(strconv.Itoa(len(files))), info.Format("attachments."))
fmt.Println(cyan.Format(fmt.Sprintf("%s/releases/tag/%s", flag.FullURL(), release.TagName)))
return nil
}
@ -91,7 +95,6 @@ func fileGlobs(globList string) ([]string, error) {
func attachFiles(client *gitea.Client, releaseID int64, files []string) error {
beaver.Infof("Attachments:\n\t%s", strings.Join(files, "\n\t"))
var confirm bool
if err := survey.AskOne(&survey.Confirm{Message: "The above files will be attached, is the list correct?"}, &confirm); err != nil {
return err

@ -0,0 +1,79 @@
package cmd
import (
"fmt"
"strconv"
"gitea.com/jolheiser/sip/flag"
"gitea.com/jolheiser/sip/sdk"
"code.gitea.io/sdk/gitea"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver/color"
)
var ReleaseAttachRemove = cli.Command{
Name: "remove",
Aliases: []string{"delete", "rm"},
Usage: "Remove attachments from a release",
Action: doReleaseAttachRemove,
}
func doReleaseAttachRemove(_ *cli.Context) error {
client, err := getClient(true)
if err != nil {
return err
}
releases, err := sdk.GetReleases(client, flag.Owner, flag.Repo, gitea.ListReleasesOptions{})
if err != nil {
return err
}
releaseNames := make([]string, len(releases))
releaseMap := make(map[string]*gitea.Release)
for idx, rel := range releases {
releaseNames[idx] = rel.TagName
releaseMap[rel.TagName] = rel
}
releaseQ := &survey.Select{Message: "Release", Options: releaseNames}
var releaseA string
if err := survey.AskOne(releaseQ, &releaseA); err != nil {
return err
}
release := releaseMap[releaseA]
attachments, err := sdk.GetReleaseAttachments(client, flag.Owner, flag.Repo, release.ID, gitea.ListReleaseAttachmentsOptions{})
if err != nil {
return err
}
attachmentMap := make(map[string]*gitea.Attachment)
attachmentOptions := make([]string, len(attachments))
for idx, attachment := range attachments {
display := fmt.Sprintf("%s (%s)", attachment.Name, attachment.UUID)
attachmentOptions[idx] = display
attachmentMap[display] = attachment
}
attachmentQ := &survey.MultiSelect{Message: "Delete Attachments", Options: attachmentOptions}
var attachmentA []string
if err := survey.AskOne(attachmentQ, &attachmentA); err != nil {
return err
}
for _, a := range attachmentA {
attachment := attachmentMap[a]
if _, err := client.DeleteReleaseAttachment(flag.Owner, flag.Repo, release.ID, attachment.ID); err != nil {
return err
}
}
info := color.Info
cyan := color.New(color.FgCyan)
fmt.Println(info.Format("Removed"), cyan.Format(strconv.Itoa(len(attachmentA))), info.Format("attachments."))
fmt.Println(cyan.Format(fmt.Sprintf("%s/releases/tag/%s", flag.FullURL(), release.TagName)))
return nil
}

@ -1,6 +1,8 @@
package sdk
import "code.gitea.io/sdk/gitea"
import (
"code.gitea.io/sdk/gitea"
)
// GetReleases returns all matching Releases from a Gitea instance
func GetReleases(client *gitea.Client, owner, repo string, opts gitea.ListReleasesOptions) ([]*gitea.Release, error) {
@ -21,3 +23,24 @@ func GetReleases(client *gitea.Client, owner, repo string, opts gitea.ListReleas
}
return releases, nil
}
// GetReleaseAttachments returns all attachments from a release
func GetReleaseAttachments(client *gitea.Client, owner, repo string, releaseID int64, opts gitea.ListReleaseAttachmentsOptions) ([]*gitea.Attachment, error) {
attachments := make([]*gitea.Attachment, 0)
p := 1
for {
opts.Page = p
list, _, err := client.ListReleaseAttachments(owner, repo, releaseID, opts)
if err != nil {
return attachments, err
}
p++
attachments = append(attachments, list...)
// FIXME Seems paging doesn't want to work with release attachments
if len(list) == 0 || len(list) < 50 {
break
}
}
return attachments, nil
}