From 9e398b334797adefd39c569d4dd64fd0db614546 Mon Sep 17 00:00:00 2001 From: Alberts Zemzale Date: Thu, 10 Feb 2022 14:30:00 +0200 Subject: [PATCH] feat(snippets): Add `files` array to snippet creation This adds the new `files` array for creating snippets that replaces the now deprecated `filename` and `contents` keys. This is needed for creating more then one file in snippets. Issue #1372 --- project_snippets.go | 1 + project_snippets_test.go | 17 ++++++++++++++++- snippets.go | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/project_snippets.go b/project_snippets.go index 6bbaeafb0..82db71931 100644 --- a/project_snippets.go +++ b/project_snippets.go @@ -94,6 +94,7 @@ type CreateProjectSnippetOptions struct { Description *string `url:"description,omitempty" json:"description,omitempty"` Content *string `url:"content,omitempty" json:"content,omitempty"` Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"` + Files *[]*SnippetFile `url:"files,omitempty" json:"files,omitempty"` } // CreateSnippet creates a new project snippet. The user must have permission diff --git a/project_snippets_test.go b/project_snippets_test.go index bc60d3c14..cc54edce4 100644 --- a/project_snippets_test.go +++ b/project_snippets_test.go @@ -172,7 +172,13 @@ func TestProjectSnippetsService_CreateSnippet(t *testing.T) { }, "project_id": 1, "web_url": "http://example.com/example/example/snippets/1", - "raw_url": "http://example.com/example/example/snippets/1/raw" + "raw_url": "http://example.com/example/example/snippets/1/raw", + "files": [ + { + "path": "add.rb", + "raw_url": "http://example.com/example/example/-/snippets/1/raw/main/add.rb" + } + ] } `) }) @@ -198,6 +204,15 @@ func TestProjectSnippetsService_CreateSnippet(t *testing.T) { }, WebURL: "http://example.com/example/example/snippets/1", RawURL: "http://example.com/example/example/snippets/1/raw", + Files: []struct { + Path string `json:"path"` + RawURL string `json:"raw_url"` + }{ + { + Path: "add.rb", + RawURL: "http://example.com/example/example/-/snippets/1/raw/main/add.rb", + }, + }, } s, resp, err := client.ProjectSnippets.CreateSnippet(1, nil, nil, nil) diff --git a/snippets.go b/snippets.go index 58794cf40..26b8d4c1d 100644 --- a/snippets.go +++ b/snippets.go @@ -51,6 +51,10 @@ type Snippet struct { CreatedAt *time.Time `json:"created_at"` WebURL string `json:"web_url"` RawURL string `json:"raw_url"` + Files []struct { + Path string `json:"path"` + RawURL string `json:"raw_url"` + } `json:"files"` } func (s Snippet) String() string { @@ -101,6 +105,15 @@ func (s *SnippetsService) GetSnippet(snippet int, options ...RequestOptionFunc) return ps, resp, err } +// SnippetFile represents the object that is used to create snippets +// +// GitLab API docs: +// https://docs.gitlab.com/ce/api/snippets.html#create-new-snippet +type SnippetFile struct { + FilePath *string `url:"file_path,omitempty" json:"file_path,omitempty"` + Content *string `url:"content,omitempty" json:"content,omitempty"` +} + // CreateSnippetOptions represents the available CreateSnippet() options. // // GitLab API docs: @@ -111,6 +124,7 @@ type CreateSnippetOptions struct { Description *string `url:"description,omitempty" json:"description,omitempty"` Content *string `url:"content,omitempty" json:"content,omitempty"` Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"` + Files *[]*SnippetFile `url:"files,omitempty" json:"files,omitempty"` } // CreateSnippet creates a new snippet. The user must have permission