feat: add attachment shortcut group
This commit is contained in:
parent
52b7093846
commit
8219c0b6dc
13
README.md
13
README.md
|
|
@ -285,6 +285,19 @@ gitlink-cli member +role --owner Gitlink --repo forgeplus --user-id 101 --role D
|
|||
gitlink-cli member +invite-link --owner Gitlink --repo forgeplus --role developer --apply true
|
||||
```
|
||||
|
||||
### Attachment Operations
|
||||
|
||||
```bash
|
||||
# Upload a local file
|
||||
gitlink-cli attachment +upload -f ./build.log -d "CI build log"
|
||||
|
||||
# Upload a file and attach container metadata
|
||||
gitlink-cli attachment +upload -f ./release-notes.md --container-id 42 --container-type VersionRelease
|
||||
|
||||
# Delete an uploaded attachment
|
||||
gitlink-cli attachment +delete -i 791eccbf-2e35-4301-ad95-8c937a117f40
|
||||
```
|
||||
|
||||
### Issue Management
|
||||
|
||||
```bash
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
# Attachment Shortcut
|
||||
|
||||
## Summary
|
||||
|
||||
This change adds a new `attachment` shortcut group to `gitlink-cli` so users can upload and delete standalone attachments without dropping down to raw API calls.
|
||||
|
||||
## Commands
|
||||
|
||||
```bash
|
||||
gitlink-cli attachment +upload -f ./build.log -d "CI build log"
|
||||
gitlink-cli attachment +upload -f ./release-notes.md --container-id 42 --container-type VersionRelease
|
||||
gitlink-cli attachment +delete -i 791eccbf-2e35-4301-ad95-8c937a117f40
|
||||
```
|
||||
|
||||
## API Coverage
|
||||
|
||||
- `POST /api/attachments.json`
|
||||
- `DELETE /api/attachments/{uuid}.json`
|
||||
|
||||
## Notes
|
||||
|
||||
- Upload uses multipart form data and works with the same `GITLINK_TOKEN` access token flow already used by the CLI.
|
||||
- Delete accepts the attachment UUID returned by the upload API.
|
||||
|
|
@ -5,8 +5,11 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/auth"
|
||||
|
|
@ -64,7 +67,6 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
|
|||
fullURL += sep + query.Encode()
|
||||
}
|
||||
|
||||
// Replace path params
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
data, err := json.Marshal(body)
|
||||
|
|
@ -79,8 +81,59 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return c.doRequest(req)
|
||||
}
|
||||
|
||||
func (c *Client) PostMultipart(path, fileField, filePath string, fields map[string]string) (*output.Envelope, error) {
|
||||
path = normalizeAPIPath(c.BaseURL, path)
|
||||
if idx := strings.Index(path, "?"); idx != -1 {
|
||||
basePath := path[:idx]
|
||||
queryStr := path[idx:]
|
||||
if shouldAppendJSONSuffix(basePath) {
|
||||
path = basePath + ".json" + queryStr
|
||||
}
|
||||
} else if shouldAppendJSONSuffix(path) {
|
||||
path += ".json"
|
||||
}
|
||||
fullURL := c.BaseURL + path
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
|
||||
part, err := writer.CreateFormFile(fileField, filepath.Base(filePath))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create form file: %w", err)
|
||||
}
|
||||
if _, err := io.Copy(part, file); err != nil {
|
||||
return nil, fmt.Errorf("copy file: %w", err)
|
||||
}
|
||||
for key, value := range fields {
|
||||
if err := writer.WriteField(key, value); err != nil {
|
||||
return nil, fmt.Errorf("write form field %s: %w", key, err)
|
||||
}
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return nil, fmt.Errorf("close multipart writer: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", fullURL, &body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
return c.doRequest(req)
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(req *http.Request) (*output.Envelope, error) {
|
||||
if c.Debug {
|
||||
fmt.Printf("→ %s %s\n", method, fullURL)
|
||||
fmt.Printf("-> %s %s\n", req.Method, req.URL.String())
|
||||
}
|
||||
|
||||
resp, err := c.HTTP.Do(req)
|
||||
|
|
@ -95,10 +148,9 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
|
|||
}
|
||||
|
||||
if c.Debug {
|
||||
fmt.Printf("← %d %s\n", resp.StatusCode, string(respData[:min(len(respData), 200)]))
|
||||
fmt.Printf("<- %d %s\n", resp.StatusCode, string(respData[:min(len(respData), 200)]))
|
||||
}
|
||||
|
||||
// Check HTTP-level errors
|
||||
if resp.StatusCode >= 400 {
|
||||
return nil, &APIError{
|
||||
StatusCode: resp.StatusCode,
|
||||
|
|
@ -107,10 +159,8 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
|
|||
}
|
||||
}
|
||||
|
||||
// Parse JSON
|
||||
var raw map[string]interface{}
|
||||
if err := json.Unmarshal(respData, &raw); err != nil {
|
||||
// Not JSON, return as-is
|
||||
return output.SuccessEnvelope(string(respData), nil), nil
|
||||
}
|
||||
|
||||
|
|
@ -142,7 +192,6 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
|
|||
}
|
||||
}
|
||||
|
||||
// Build meta from pagination info
|
||||
var meta *output.Meta
|
||||
if tc, ok := raw["total_count"]; ok {
|
||||
meta = &output.Meta{}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
package attachment
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/output"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||||
)
|
||||
|
||||
func Shortcuts() []*common.Shortcut {
|
||||
return []*common.Shortcut{
|
||||
{
|
||||
Name: "upload",
|
||||
Description: "Upload an attachment",
|
||||
Flags: []common.Flag{
|
||||
{Name: "file", Short: "f", Usage: "File path to upload", Required: true},
|
||||
{Name: "description", Short: "d", Usage: "Attachment description"},
|
||||
{Name: "container-id", Usage: "Container model ID"},
|
||||
{Name: "container-type", Usage: "Container model type"},
|
||||
},
|
||||
Run: runUploadAttachment,
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Description: "Delete an attachment",
|
||||
Flags: []common.Flag{
|
||||
{Name: "id", Short: "i", Usage: "Attachment UUID", Required: true},
|
||||
},
|
||||
Run: runDeleteAttachment,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func runUploadAttachment(ctx *common.RuntimeContext) error {
|
||||
filePath, err := ctx.RequireArg("file")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stat file: %w", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return fmt.Errorf("file path points to a directory: %s", filePath)
|
||||
}
|
||||
|
||||
fields := map[string]string{}
|
||||
if v := ctx.Arg("description"); v != "" {
|
||||
fields["description"] = v
|
||||
}
|
||||
if v := ctx.Arg("container-id"); v != "" {
|
||||
fields["container_id"] = v
|
||||
}
|
||||
if v := ctx.Arg("container-type"); v != "" {
|
||||
fields["container_type"] = v
|
||||
}
|
||||
|
||||
env, err := ctx.PostMultipart("/attachments", "file", filePath, fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if data, ok := env.Data.(map[string]interface{}); ok {
|
||||
data["filename"] = filepath.Base(filePath)
|
||||
}
|
||||
|
||||
return ctx.Output(env)
|
||||
}
|
||||
|
||||
func runDeleteAttachment(ctx *common.RuntimeContext) error {
|
||||
id, err := ctx.RequireArg("id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/attachments/%s", id), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if env == nil {
|
||||
return output.Print(output.SuccessEnvelope(map[string]interface{}{
|
||||
"id": id,
|
||||
"deleted": true,
|
||||
}, nil), ctx.Format)
|
||||
}
|
||||
return ctx.Output(env)
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
package attachment
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/client"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||||
)
|
||||
|
||||
func TestAttachmentUploadSendsMultipartForm(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "sample.txt")
|
||||
if err := os.WriteFile(filePath, []byte("hello attachment"), 0o644); err != nil {
|
||||
t.Fatalf("write temp file: %v", err)
|
||||
}
|
||||
|
||||
called := false
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" || r.URL.Path != "/attachments.json" {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
called = true
|
||||
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
||||
t.Fatalf("parse multipart: %v", err)
|
||||
}
|
||||
if got := r.FormValue("description"); got != "release asset" {
|
||||
t.Fatalf("description = %q, want %q", got, "release asset")
|
||||
}
|
||||
if got := r.FormValue("container_id"); got != "42" {
|
||||
t.Fatalf("container_id = %q, want %q", got, "42")
|
||||
}
|
||||
if got := r.FormValue("container_type"); got != "VersionRelease" {
|
||||
t.Fatalf("container_type = %q, want %q", got, "VersionRelease")
|
||||
}
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
t.Fatalf("read form file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
if header.Filename != "sample.txt" {
|
||||
t.Fatalf("filename = %q, want %q", header.Filename, "sample.txt")
|
||||
}
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.Fatalf("read uploaded file: %v", err)
|
||||
}
|
||||
if string(content) != "hello attachment" {
|
||||
t.Fatalf("content = %q, want %q", string(content), "hello attachment")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"att-1","title":"sample.txt","url":"https://example.com/a/att-1"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runAttachmentShortcut(t, server, "upload", map[string]string{
|
||||
"file": filePath,
|
||||
"description": "release asset",
|
||||
"container-id": "42",
|
||||
"container-type": "VersionRelease",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upload shortcut failed: %v", err)
|
||||
}
|
||||
if !called {
|
||||
t.Fatal("upload endpoint was not called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentDeleteCallsAPI(t *testing.T) {
|
||||
called := false
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "DELETE" || r.URL.Path != "/attachments/att-1.json" {
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
called = true
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"status":0,"message":"deleted"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
err := runAttachmentShortcut(t, server, "delete", map[string]string{
|
||||
"id": "att-1",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("delete shortcut failed: %v", err)
|
||||
}
|
||||
if !called {
|
||||
t.Fatal("delete endpoint was not called")
|
||||
}
|
||||
}
|
||||
|
||||
func runAttachmentShortcut(t *testing.T, server *httptest.Server, name string, args map[string]string) error {
|
||||
t.Helper()
|
||||
shortcut := findAttachmentShortcut(t, name)
|
||||
ctx := &common.RuntimeContext{
|
||||
Client: &client.Client{
|
||||
HTTP: server.Client(),
|
||||
BaseURL: server.URL,
|
||||
},
|
||||
Format: "json",
|
||||
Args: args,
|
||||
}
|
||||
return shortcut.Run(ctx)
|
||||
}
|
||||
|
||||
func findAttachmentShortcut(t *testing.T, name string) *common.Shortcut {
|
||||
t.Helper()
|
||||
for _, shortcut := range Shortcuts() {
|
||||
if shortcut.Name == name {
|
||||
return shortcut
|
||||
}
|
||||
}
|
||||
t.Fatalf("shortcut %q not found", name)
|
||||
return nil
|
||||
}
|
||||
|
|
@ -90,6 +90,11 @@ func (ctx *RuntimeContext) CallAPIWithQuery(method, path string, query url.Value
|
|||
return ctx.Client.Do(method, path, nil, query)
|
||||
}
|
||||
|
||||
// PostMultipart uploads a file with multipart/form-data through the shared client.
|
||||
func (ctx *RuntimeContext) PostMultipart(path, fileField, filePath string, fields map[string]string) (*output.Envelope, error) {
|
||||
return ctx.Client.PostMultipart(path, fileField, filePath, fields)
|
||||
}
|
||||
|
||||
// PaginateAll fetches all pages.
|
||||
func (ctx *RuntimeContext) PaginateAll(path string, params url.Values) ([]json.RawMessage, error) {
|
||||
return ctx.Client.PaginateAll(path, params)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/gitlink-org/gitlink-cli/internal/i18n"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/attachment"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/branch"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/ci"
|
||||
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
||||
|
|
@ -32,45 +33,47 @@ func RegisterAll(root *cobra.Command, translators ...*i18n.Translator) {
|
|||
tr = translators[0]
|
||||
}
|
||||
groups := map[string][]*common.Shortcut{
|
||||
"repo": repo.Shortcuts(tr),
|
||||
"issue": issue.Shortcuts(tr),
|
||||
"label": label.Shortcuts(),
|
||||
"license": license.Shortcuts(),
|
||||
"member": member.Shortcuts(),
|
||||
"milestone": milestone.Shortcuts(),
|
||||
"pipeline": pipeline.Shortcuts(),
|
||||
"pr": pr.Shortcuts(tr),
|
||||
"release": release.Shortcuts(tr),
|
||||
"branch": branch.Shortcuts(tr),
|
||||
"org": org.Shortcuts(tr),
|
||||
"user": user.Shortcuts(tr),
|
||||
"search": search.Shortcuts(tr),
|
||||
"ci": ci.Shortcuts(tr),
|
||||
"compare": compare.Shortcuts(),
|
||||
"webhook": webhook.Shortcuts(tr),
|
||||
"health": health.Shortcuts(tr),
|
||||
"workflow": workflow.Shortcuts(),
|
||||
"attachment": attachment.Shortcuts(),
|
||||
"repo": repo.Shortcuts(tr),
|
||||
"issue": issue.Shortcuts(tr),
|
||||
"label": label.Shortcuts(),
|
||||
"license": license.Shortcuts(),
|
||||
"member": member.Shortcuts(),
|
||||
"milestone": milestone.Shortcuts(),
|
||||
"pipeline": pipeline.Shortcuts(),
|
||||
"pr": pr.Shortcuts(tr),
|
||||
"release": release.Shortcuts(tr),
|
||||
"branch": branch.Shortcuts(tr),
|
||||
"org": org.Shortcuts(tr),
|
||||
"user": user.Shortcuts(tr),
|
||||
"search": search.Shortcuts(tr),
|
||||
"ci": ci.Shortcuts(tr),
|
||||
"compare": compare.Shortcuts(),
|
||||
"webhook": webhook.Shortcuts(tr),
|
||||
"health": health.Shortcuts(tr),
|
||||
"workflow": workflow.Shortcuts(),
|
||||
}
|
||||
|
||||
descriptions := map[string]string{
|
||||
"repo": tr.T("cmd.repo.short"),
|
||||
"issue": tr.T("cmd.issue.short"),
|
||||
"label": "Issue label operations",
|
||||
"license": "License operations",
|
||||
"member": "Repository member operations",
|
||||
"milestone": "Milestone operations",
|
||||
"pipeline": "Pipeline operations",
|
||||
"pr": tr.T("cmd.pr.short"),
|
||||
"release": tr.T("cmd.release.short"),
|
||||
"branch": tr.T("cmd.branch.short"),
|
||||
"org": tr.T("cmd.org.short"),
|
||||
"user": tr.T("cmd.user.short"),
|
||||
"search": tr.T("cmd.search.short"),
|
||||
"ci": tr.T("cmd.ci.short"),
|
||||
"compare": "Compare branches, tags, or commits",
|
||||
"webhook": tr.T("cmd.webhook.short"),
|
||||
"health": "Project health data collection",
|
||||
"workflow": "AI agent workflow analysis",
|
||||
"attachment": "Attachment operations",
|
||||
"repo": tr.T("cmd.repo.short"),
|
||||
"issue": tr.T("cmd.issue.short"),
|
||||
"label": "Issue label operations",
|
||||
"license": "License operations",
|
||||
"member": "Repository member operations",
|
||||
"milestone": "Milestone operations",
|
||||
"pipeline": "Pipeline operations",
|
||||
"pr": tr.T("cmd.pr.short"),
|
||||
"release": tr.T("cmd.release.short"),
|
||||
"branch": tr.T("cmd.branch.short"),
|
||||
"org": tr.T("cmd.org.short"),
|
||||
"user": tr.T("cmd.user.short"),
|
||||
"search": tr.T("cmd.search.short"),
|
||||
"ci": tr.T("cmd.ci.short"),
|
||||
"compare": "Compare branches, tags, or commits",
|
||||
"webhook": tr.T("cmd.webhook.short"),
|
||||
"health": "Project health data collection",
|
||||
"workflow": "AI agent workflow analysis",
|
||||
}
|
||||
|
||||
for name, shortcuts := range groups {
|
||||
|
|
|
|||
Loading…
Reference in New Issue